Jatin Bodarya
Jatin Bodarya

Reputation: 1455

system time in specific format in c program

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.

Upvotes: 1

Views: 156

Answers (2)

alk
alk

Reputation: 70931

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

Upvotes: 0

Arya
Arya

Reputation: 371

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

Upvotes: 2

Related Questions