Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a time into epoch time?

Tags:

c++

time

Say I have a specific instant in time where I know the hour, minute, day, second, month, year, etc; how can I convert this epoch time (seconds since 1970)?

I can't use Boost, so please don't suggest a Boost solution.

like image 413
user788171 Avatar asked Aug 16 '12 01:08

user788171


People also ask

How is epoch time difference calculated?

For example, you could convert the time to an epoch and subtract it from another epoch value to quickly determine the difference. If the difference was 176,400 and you used the above chart or a math formula to find the difference is 2 days and 1 hour (86400 + 86400 + 3600 = 176,400).

What is epoch time format?

In a computing context, an epoch is the date and time relative to which a computer's clock and timestamp values are determined. The epoch traditionally corresponds to 0 hours, 0 minutes, and 0 seconds (00:00:00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system.

How do I convert timestamp to epoch in Excel?

Select a blank cell, suppose Cell C2, and type this formula =(C2-DATE(1970,1,1))*86400 into it and press Enter key, if you need, you can apply a range with this formula by dragging the autofill handle. Now a range of date cells have been converted to Unix timestamps.


1 Answers

Use the mktime(3) function. For example:

struct tm t = {0};  // Initalize to all 0's
t.tm_year = 112;  // This is year-1900, so 112 = 2012
t.tm_mon = 8;
t.tm_mday = 15;
t.tm_hour = 21;
t.tm_min = 54;
t.tm_sec = 13;
time_t timeSinceEpoch = mktime(&t);
// Result: 1347764053
like image 61
Adam Rosenfield Avatar answered Oct 28 '22 21:10

Adam Rosenfield