system time in specific format in c program

157 views Asked by At

I am using RED HAT machine. I suppose to get system time in my program.below is a part of a code. date=%Y-%m-%d time=%H:%M:%S is the format I wants. issue is it is giving me GMT time instead of my current system time. how can I get system time in this format?

char *buf_cur = buf;
    char timestr[TIMEBUF_SIZE];
    time_t now = time (0);

    strftime(timestr,TIMEBUF_SIZE, "date=%Y-%m-%d time=%H:%M:%S", localtime(&now));
    buf_cur += sprintf(buf_cur, "%s",timestr);

in short I want a time which "date" command gives in linux. with above format.

2

There are 2 answers

0
Arya On

The localtime() function converts the calendar time timep to broken- down time representation, expressed relative to the user's specified timezone i.e. GMT

To get time in UTC , you should use gmtime().

3
alk On

If the BIOS time is set to the local time at the machine location and the time zone is set to location's time zone, then the code you show should give you local time.

  • Check/adjust the machine's time zone.
  • Check/adjust the time set in the machine's BIOS