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

Categories

0 votes
726 views
in Technique[技术] by (71.8m points)

c++ - Converting time_t to int

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...