dimanche 26 juillet 2015

Why negative value (-2147483648) is coming from my method?

I am seeing pretty weird issue. Somehow with my below code, I am seeing negative number getting printed out as shown below. I am not sure why it is happening.

-2147483648 days -2147483648 hours -2147483648 minutes ago

Here is my timestamp (current_unix_timestamp) value 1437935280 which is getting passed to my below method and then afterwards holder value is coming as shown above everything as negative.

char holder[100];
get_timestamp_value(current_unix_timestamp, holder);

inline void get_timestamp_value(long sec_since_epoch_time, char* holder) {
    double delta = current_timestamp()/1000000 - sec_since_epoch_time;
    int days = floor(delta/60/60/24);
    int hours = floor((delta - days * 60 * 60 * 24)/60/60);
    int minutes = floor((delta - days * 60 * 60 * 24 - hours * 60 * 60)/60);
    holder[0] = 0;
    if (days) sprintf(holder, "%d days ", days);
    if (hours) sprintf(holder, "%s%d hours ", holder, hours);
    sprintf(holder, "%s%d minutes ago", holder, minutes);
}

// get current system time in microseconds since epoch
inline uint64_t current_timestamp()
{
    std::chrono::time_point<std::chrono::steady_clock> ts = std::chrono::steady_clock::now();
    return std::chrono::duration_cast<std::chrono::microseconds>(ts.time_since_epoch()).count();
}

Is there anything wrong happening in the above code? Any suggestions will be of great help.

I am running this code on Ubuntu 14.04

Aucun commentaire:

Enregistrer un commentaire