Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to convert a given time into epoch(time_t) and vice versa. can anyone tell what is the routine or algorithm for this?

Thanks

Update

 epoch_strt.tm_sec = 0;
 epoch_strt.tm_min = 0;
 epoch_strt.tm_hour = 0;
epoch_strt.tm_mday = 1;
epoch_strt.tm_mon = 1;
epoch_strt.tm_year = 70;
 epoch_strt.tm_isdst = -1;

double nsecs = difftime(curtime, basetime);//current time from system, I converrting it to struct tm

but this always return 32399 for some reason.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
730 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should cast it to a long int instead of int.

long int t = static_cast<long int> (time(NULL));

An int might not be enough to hold the time, for example, on my platform, time_t is a typedef of __int64.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...