HI All,
Ant Unix method or C++ method is there to convert unix time stamp to system time stamp.The input is ULONG and out put should be in string.


u can intimate to <email snipped>
Any help is appreciated in advance.

Dani AI

Generated

A Unix timestamp like 1227806311 is the count of seconds since 1970-01-01 00:00:00 UTC. To print it as a string, convert to struct tm and format with strftime. Use gmtime* for UTC/GMT (what you asked in post #6) or localtime* for your machine’s timezone. For your example, 1227806311 -> 27/11/2008 17 18 31 UTC. was right that the C standard does not mandate an epoch, but on POSIX and Windows you can treat time_t as seconds since the Unix epoch in practice.

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

static int unix_to_string(unsigned long secs, int use_utc,
                          char *out, size_t outsz)
{
    time_t t = (time_t)secs;
    struct tm tmv;

#if defined(_WIN32)
    if (use_utc) { if (gmtime_s(&tmv, &t)) return -1; }
    else         { if (localtime_s(&tmv, &t)) return -1; }
#else
    if (use_utc) { if (!gmtime_r(&t, &tmv)) return -1; }
    else         { if (!localtime_r(&t, &tmv)) return -1; }
#endif

    return strftime(out, outsz, "%d/%m/%Y %H %M %S", &tmv) ? 0 : -1;
}

int main(void) {
    char buf[32];
    if (unix_to_string(1227806311UL, 1, buf, sizeof buf) == 0)  // UTC/GMT
        puts(buf);
    if (unix_to_string(1227806311UL, 0, buf, sizeof buf) == 0)  // local time
        puts(buf);
}

Tips:

  • Make sure your input is in seconds, not milliseconds. If it is ms, divide by 1000 first.
  • Prefer time_t or uint64_t for the input; ULONG can overflow on 32-bit builds and will hit the Year 2038 limit.
  • Use the thread-safe variants shown (gmtime_r/localtime_r on POSIX, gmtime_s/localtime_s on Windows).
  • If you only need UTC, always use gmtime*; localtime* will apply your system timezone and DST rules.

Recommended Answers

All 9 Replies

Can you explain what is meant by "unix timestamp" and (mainly) "system timestamp"?

Can you explain what is meant by "unix timestamp" and (mainly) "system timestamp"?

unix time stamp is like = 1227806311
the same system time stamp is = 27/11/2008 HR MIN SEC

I have never seen before so funny "definitions" of a "unix timestamp" data type and a "system time" representation: ;)

unix time stamp is like = 1227806311
the same system time stamp is = 27/11/2008 HR MIN SEC

Well, try to adopt this code:

/* UnixTime is unspecified unix time stamp type.
     * Define it before compilation, for example:
     * typedef time_t UnixTime;
     *  or
     * typedef long int UnixTime;
     */
    UnixTime ut;  /* Unix timestamp to be converted */

    time_t thist; /* This system timestamp value */
    struct tm* ptm;
    char mysyst[20] = "\?"; /* for "sys time" */
    const char* fmt = "%d/%m/%Y %H %M %S"; /* sys?time */
    /* C Standard does not specify what's time_t epoch */
    time_t uepoch; /* Unix epoch in this system time_t */
    struct tm tmepoch;
    /* Prepare unix time epoch on this system */
    memset(&tmepoch,0,sizeof tmepoch);
    tmepoch.tm_year = 1970 - 1900;
    tmepoch.tm_mon = 1 - 1;
    tmepoch.tm_mday = 1;
    tmepoch.tm_hour = 0;
    tmepoch.tm_min = 0;
    tmepoch.tm_sec = 0;
    uepoch = mktime(&tmepoch); /* this system unix epoch */
    /* Recalculate unix time to this system time_t */
    thist = (time_t)ut - uepoch;
    ptm = gmtime(&thist); 
    if (ptm && strftime(mysyst,sizeof mysyst,fmt,ptm))
        printf("%s\n",mysyst);

Sry to tell you guys my output time should be GMT or UTC.Any one plz help me on this urgently?

hmmmm dude i was busy with some other issues.

hmmmm dude i was busy with some other issues.

Just the TIME to go on with that issues ;)...

> hmmmm dude i was busy with some other issues.
Your choice - give up programming or give up something else.

You're not going to learn anything by dipping into programming for a couple of hours a month, so I'd say give up.

Otherwise you would have found a way to do something in the last fortnight, rather than just showing up here at the last minute hoping for salvation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.