472,958 Members | 2,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Time problem....mktime(), gmtime(), localtime()

KW
Hi all,

Appreciate if someone can help me out on this.

Currently, I have a tm structure holding information of the UTC time,
which is very likely to be in the past, meaning not the current time.
So, let's say I'm in CST(-6) now, and I want to know the equivalent
local time of that tm structure, how do I do that taking into
consideration daylight savings adjustments?

Well, I have gathered some info:

mktime() will convert the tm structure and return a time_t object, but
this time_t returned is of the local time, not UTC.

localtime() takes in a time_t argument(of UTC) and returns a tm
structure pointing to the local time. This is actually what I desire,
but, I still can't seem to translate the tm structure of the UTC time
to a time_t (UTC) data type.

gmtime() also takes in one argument of time_t (UTC) but returns a tm
structure pointing to the UTC time.

In short:

local tm -> mktime() -> local time_t
UTC time_t -> localtime() -> local tm
UTC time_t -> gmtime() -> UTC tm

So, what I am looking for is:
UTC tm -> ???() -> UTC time_t

Thanks in advance!
KW

Nov 14 '05 #1
1 24469
KW wrote:
Hi all,

Appreciate if someone can help me out on this.

Currently, I have a tm structure holding information of the UTC time,
which is very likely to be in the past, meaning not the current time.
So, let's say I'm in CST(-6) now, and I want to know the equivalent
local time of that tm structure, how do I do that taking into
consideration daylight savings adjustments?

Well, I have gathered some info:

mktime() will convert the tm structure and return a time_t object, but
this time_t returned is of the local time, not UTC.

localtime() takes in a time_t argument(of UTC) and returns a tm
structure pointing to the local time. This is actually what I desire,
but, I still can't seem to translate the tm structure of the UTC time
to a time_t (UTC) data type.

gmtime() also takes in one argument of time_t (UTC) but returns a tm
structure pointing to the UTC time.

In short:

local tm -> mktime() -> local time_t
UTC time_t -> localtime() -> local tm
UTC time_t -> gmtime() -> UTC tm

So, what I am looking for is:
UTC tm -> ???() -> UTC time_t


As far as I know, you'll need to apply the time zone
correction manually:

time_t when;
struct tm tm = ...time in UTC...;
tm.tm_hour += -6; /* convert to local time zone */
when = mktime(&tm); /* convert local to time_t */

One way to figure the offset instead of hard-wiring "-6"
might be to pass the same arbitrary time_t value through both
gmtime() and localtime() and study the difference:

time_t when = time(NULL);
struct tm utc = *gmtime(&when);
struct tm lcl = *localtime(&when);
int delta_h = lcl.tm_hour - utc.tm_hour;

.... with some extra care around midnight, especially around
midnight on December 31! However, this isn't perfect even
with the extra care: the UTC-to-local delta in effect now
might not be the same that was in effect at the time you're
interested in, because of seasonal adjustments ("daylight
savings time"). You could, I guess, try the delta_h as
computed above, use mktime(), put the resulting time_t back
through gmtime() again, and see whether you got your original
struct tm back -- if not, you could adjust delta_h and try
again. (In fact, if you've got such a predict-and-correct
loop in place, you might as well just start delta_h at zero
and let the corrector figure it out.)

Three final points: First, some time zones' offsets from
UTC are not an integral number of hours, so you might want
to use a delta_m in addition to or instead of delta_h (I'm
not aware of any time zone that would require a delta_s).
Second, time_t is neither "local" nor "UTC" nor anything
else; such adjustments come into play only when developing
a struct tm from a time_t. Finally, I've omitted all error
checking above but you must not: all of gmtime(), localtime(),
mktime(), and even time() can fail, and not all systems "know"
their own time zone.

--
Er*********@sun.com
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dhaval | last post by:
Hi, Well I am facing this strange issue with javascript. Specifically on the 26th October 2003, when I select any roomtype I get the rate 0 if "In the Operating system, Time Zone is selected...
3
by: Troy | last post by:
I have a class library (DLL) that I created and have been using for sometime in my various .NET applications. Recently, I have been having problems with what I believe is corruption (or a bug) of the...
1
by: E. Liepins | last post by:
I am working on a vehicle database. We track when a vehicle is borrowed and when it is returned. We also track the number of kilometres travelled on a particular trip. There are several tables:...
3
by: idikoko | last post by:
I have a small problem I am trying to extract charcters from one string store it in another and then compare the new string and out a msg. The code looks fine but it always runs into a compiler...
7
by: Yuanfei | last post by:
Hi There, I just found that there is a problem in vc2005 regarding to time_t and localtime. See code snippets belows. Using this code segment, I found that when ut is 86200, the corresponding...
2
by: Lady dyna | last post by:
Get the current date and time from library function localtime() and display it. You have to include time.h The prototype of this function is given in time.h Sample Output Today is Tuesday 3rd...
5
by: Fareast Adam | last post by:
I've a field in a table that datatype's type datetime. Here i want to get current server GMT time before insert into db. Any idea why this code always give me result in am although the present time...
0
by: Fabio Durieux Lopes | last post by:
Hi, I'm trying to execute some operations based on a file's time. The file's time is actually the file's name (e.g. FILE1_20080326170558). So I do this: fileTimeInSecs =...
5
by: KusoYumi | last post by:
Hello, im facing date and time problem. I want get a time in Date Type, with this format HH:mm:SS i try to get new Date(), then format it as String. Then i use substring to get the time. Then i...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.