Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert datetime to unix timestamp in c?

Tags:

the scenario is: I get datetime in format "YYYY-MM-DD HH:MM:SS" with libexif. To minimize the saving cost, I wanna convert the datetime to unix timestamp or alike which only cost 64bit or 32bit. Is there any explicit way with c?

like image 495
liuliu Avatar asked Jun 16 '09 16:06

liuliu


People also ask

How do I manually convert a date to a timestamp in unix?

To easily convert UNIX timestamp to date in the . csv file, do the following: 1. =R2/86400000+DATE(1970,1,1), press Enter key.

How do I get the current unix timestamp in C#?

You get a unix timestamp in C# by using DateTime. UtcNow and subtracting the epoch time of 1970-01-01. e.g. Int32 unixTimestamp = (int)DateTime.

How do you convert human readable time to epoch time?

Convert from human-readable date to epochlong epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.

How do I read a unix timestamp?

To find the unix current timestamp use the %s option in the date command. The %s option calculates unix timestamp by finding the number of seconds between the current date and unix epoch.


1 Answers

You could try a combination of strptime and mktime

struct tm tm; time_t epoch; if ( strptime(timestamp, "%Y-%m-%d %H:%M:%S", &tm) != NULL )   epoch = mktime(&tm); else   // badness 
like image 93
Kjetil Joergensen Avatar answered Oct 05 '22 11:10

Kjetil Joergensen