8

A string "2012-03-02" representing March 2nd, 2012 is given to me as an input variable (char *).

How do I convert this date into unix epoch time in C programming language?

user1068636
  • 1,871
  • 7
  • 33
  • 57

5 Answers5

4

Local time or UTC? If it's UTC, the easiest way to do the conversion is to avoid the C time API entirely and use the formula in POSIX for seconds since the epoch:

tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
    (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
    ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400

Source: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_15

If it's local time, the problem turns into hell due to the fact that time_t is not guaranteed to be represented as seconds since the epoch except on POSIX systems, and the fact that it's difficult to compute a time_t value corresponding to the epoch (mktime will not work because it uses local time). Once you compute the time_t for the epoch, though, it's just a matter of using mktime for the time value you parsed and then calling difftime.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
3

Extract the pieces with an sscanf, populate struct tm (from <time.h>) with the data extracted, and finally use mktime to convert it to time_t.

time_t ParseDate(const char * str)
{
    struct tm ti={0};
    if(sscanf(str, "%d-%d-%d", &ti.tm_year, &ti.tm_mon, &ti.tm_day)!=3)
    {
        /* ... error parsing ... */
    }
    ti.tm_year-=1900
    ti.tm_mon-=1
    return mktime(&ti);
}
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Can you tell why we need to subtract 1900 and 1 from tm_year and tm_mon ? – Gow. Aug 28 '21 at 09:29
  • Because in `struct tm` years start with 1900, and the month is 0-based. You find all the relevant information about this in the documentation. – Matteo Italia Aug 28 '21 at 18:55
2

C (POSIX) provides a function for this. Use strptime() to convert the string into a struct tm value. You can then convert the struct tm into time_t using mktime().

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Aditya Naidu
  • 1,362
  • 9
  • 12
0

Here is my solution:

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

int main(void) {
    time_t epoch;
    char timeString[80] = { "05 07 2021 00 33 51" }; 
    struct tm my_tm;
    char buffer[80];

    memset(&my_tm, 0, sizeof(my_tm));
    if (sscanf(timeString, "%d %d %d %d %d %d", &my_tm.tm_mon, &my_tm.tm_mday, &my_tm.tm_year, &my_tm.tm_hour, &my_tm.tm_min, &my_tm.tm_sec) != 6)
    {
        /* ... error parsing ... */
        printf(" sscanf failed");
        return 1;
    }
    my_tm.tm_isdst = -1;
    my_tm.tm_year -= 1900;
    my_tm.tm_mon -= 1;

    epoch = mktime(&my_tm);
    if (epoch == -1) {
        printf("Error: unable to make time using mktime\n");
    }
    else {
        strftime(buffer, sizeof(buffer), "%c", &my_tm);
        printf("%s  (epoch=%ld)", buffer, (long)epoch);
    }

    return(0);
}

Note: This code convert the input you provided to "Fry May 7 00:33:51" and epoch=1620340431 which is not the one you mention in comment (Running under Win10 with MSVC)

fpiette
  • 11,983
  • 1
  • 24
  • 46
0

Read it into a struct tm and call mktime to get your time_t.

ldav1s
  • 15,885
  • 2
  • 53
  • 56