user1068636
user1068636

Reputation: 1939

How do I convert "2012-03-02" into unix epoch time in C?

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?

Upvotes: 8

Views: 21947

Answers (5)

fpiette
fpiette

Reputation: 12292

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)

Upvotes: 0

Matteo Italia
Matteo Italia

Reputation: 126777

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);
}

Upvotes: 3

Aditya Naidu
Aditya Naidu

Reputation: 1380

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().

Upvotes: 2

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215193

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.

Upvotes: 4

ldav1s
ldav1s

Reputation: 16305

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

Upvotes: 0

Related Questions