Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ convert string to time_t

Tags:

c++

c

time

stat

I'm using stat.st_mtime to get my last modification time of a directory and then store it into a file.txt (stored string is something like that : 1467035651)

Later when I retrieved data from my file.txt, I tried to convert my string from my file.txt to int type since the string contains just seconds but I don't know if it's a good idea to do that.

Is there any way to convert directly to time_t?

like image 206
Youssef Korchi Avatar asked Aug 13 '26 12:08

Youssef Korchi


2 Answers

According to the reference documentation time_t is just an unspecified numeric format.

Just directly read back the numeric value from the file, there's no need for special conversions:

time_t t;
std::ifstream input("File.txt");
// ...
input >> t;
like image 78
πάντα ῥεῖ Avatar answered Aug 16 '26 21:08

πάντα ῥεῖ


The function atoll in stdlib.h should work. Example:

time_t t;
char *time_string = //...

t = (time_t) atoll(time_string);
like image 43
sighingnow Avatar answered Aug 16 '26 21:08

sighingnow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!