Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Unix TimeStamp into day:month:date:year format in C?

How do we convert a Unix time stamp in C to day:month:date:year? For eg. if my unix time stamp is 1230728833(int), how we convert this value into this-> Thu Aug 21 2008?

Thanks,

like image 943
user2430771 Avatar asked Sep 02 '13 23:09

user2430771


People also ask

How do I convert a timestamp to a date?

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

Why is January 1 1970 the epoch?

January 1st, 1970 at 00:00:00 UTC is referred to as the Unix epoch. Early Unix engineers picked that date arbitrarily because they needed to set a uniform date for the start of time, and New Year's Day, 1970, seemed most convenient.

What is the format of Unix timestamp?

Unix epoch timestamps are supported in the following formats: 10 digit epoch time format surrounded by brackets (or followed by a comma). The digits must be at the very start of the message. For example, [1234567890] or [1234567890, other] followed by the rest of the message.


2 Answers

Per @H2CO3's proper suggestion to use strftime(3), here's an example program.

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

static const time_t default_time = 1230728833;
static const char default_format[] = "%a %b %d %Y";

int
main(int argc, char *argv[])
{
        time_t t = default_time;
        const char *format = default_format;

        struct tm lt;
        char res[32];

        if (argc >= 2) {
                t = (time_t) atoi(argv[1]);
        }

        if (argc >= 3) {
                format = argv[2];
        }

        (void) localtime_r(&t, &lt);

        if (strftime(res, sizeof(res), format, &lt) == 0) {
                (void) fprintf(stderr,  "strftime(3): cannot format supplied "
                                        "date/time into buffer of size %u "
                                        "using: '%s'\n",
                                        sizeof(res), format);
                return 1;
        }

        (void) printf("%u -> '%s'\n", (unsigned) t, res);

        return 0;
}
like image 192
sjnarv Avatar answered Sep 21 '22 11:09

sjnarv


This code helps you to convert timestamp from system time into UTC and TAI human readable format.

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

int main(void)
{
    time_t     now, now1, now2;
    struct tm  ts;
    char       buf[80];

 
        // Get current time
        time(&now);
        ts = *localtime(&now);
        strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S", &ts);
        printf("Local Time %s\n", buf);

        //UTC time
        now2 = now - 19800;  //from local time to UTC time
        ts = *localtime(&now2);
        strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S", &ts);
        printf("UTC time %s\n", buf);

        //TAI time valid upto next Leap second added
        now1 = now + 37;    //from local time to TAI time
        ts = *localtime(&now1);
        strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S", &ts);
        printf("TAI time %s\n", buf);
        return 0;
}
like image 25
Varun P Avatar answered Sep 20 '22 11:09

Varun P