1

I am trying to calculate the time, in seconds, for a certain timestamp. How can I input a date and time into time_t so I can calculate the time from time(null)? (time(null) is 01/01/1970 if I understand it right)

  • If you're lucky your platform has a [`strptime`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html) function. – Some programmer dude Aug 23 '19 at 03:13
  • The usual way is to fill in an instance of `struct tm`, then call [`mktime()`](https://linux.die.net/man/3/mktime). – Steve Summit Aug 23 '19 at 03:19
  • Possible duplicate of [Date/time conversion: string representation to time\_t](https://stackoverflow.com/questions/321793/date-time-conversion-string-representation-to-time-t) – phuclv Aug 23 '19 at 05:16
  • [How to convert a string variable containing time to time_t type](https://stackoverflow.com/q/11213326/995714), [Converting a Datetime String to Epoch Cleanly](https://stackoverflow.com/q/856551/995714), [How to convert date string to time_t](https://stackoverflow.com/q/48800745/995714) – phuclv Aug 23 '19 at 05:17

1 Answers1

2

Here is a simple example that computes the time more or less now, where I am:

#include <stdio.h>
#include <time.h>

int main()
{
    time_t t;
    struct tm tm;

    /* fill in values for 2019-08-22 23:22:26 */
    tm.tm_year = 2019 - 1900;
    tm.tm_mon = 8 - 1;
    tm.tm_mday = 22;
    tm.tm_hour = 23;
    tm.tm_min = 22;
    tm.tm_sec = 26;
    tm.tm_isdst = -1;
    t = mktime(&tm);
    printf("%ld\n", t);
}

As you can see, the values in some of the fields use mildly strange conventions: tm_mon is 0-based, and tm_year counts from 1900. Setting tm_isdst to -1 means "I'm not sure if DST applied on August 22; you figure it out".

When I run it, this program prints 1566530546, which is indeed the number of seconds since midnight UTC on January 1, 1970. (If you run the program, though, you'll likely get a slightly different number, because it will work from 23:22:26 in your time zone.)

As the preceding paragraphs suggest, the mktime function does take your time zone into account, as well as any necessary DST correction. If you want to do a similar conversion without these corrections, there's an analogous function called timegm, although it's not Standard and not present on all systems.

The tm_wday and tm_yday (day of week and day of year) fields are ignored when you call mktime, although they will have been filled in with their correct values when the function returns.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • 1
    As `struct tm` may have other members then the usual 9, best to initialize all members with `struct tm tm = { 0 };` to have consistent results from `mktime(&tm)`. – chux - Reinstate Monica Aug 23 '19 at 03:42
  • Thanks! this helped. I am pretty knew to C so I think some of the Documentation I read talked about structures but this cleared that up. – Timothy Harmon Aug 23 '19 at 13:20
  • mktime() calculates the seconds of epoch to the current timezone. I needed to use timegm() to completely solve the issue because the timezone of the input data is also UTC. – Timothy Harmon Aug 23 '19 at 15:06