[LCP]Time conversions

Greg Black gjb at gbch.net
Wed Nov 27 13:04:02 UTC 2002


Jack Lloyd wrote:

| I have an interesting time conversion problem. Basically, my application is
| reading in a string specifying a time in UTC/GMT. I parse this down to some
| integers (year, month, ..., second). At some later time, I would like to
| convert this into a time_t (so it can be compared with another time_t
| passed in by the application). The obvious way to do this would be to fill
| in a struct tm with the values and call mktime. Unfortunatly mktime expects
| a struct tm in localtime, and there is no alternative version for UTC as
| there is for localtime.

Many systems provide timegm() which does mktime() but treats the
input data as UTC.

Failing that, just set TZ=UTC in the environment before doing
anything else.

Greg



More information about the linuxCprogramming mailing list