Problems with mktime

Discussion in 'C++' started by Paul N, Apr 4, 2011.

  1. Paul N

    Paul N Guest

    I'm trying to write a program that will show the time at various
    different places. The idea is to get the GMT time, adjust it for the
    correct time zone offset (including DST if appropriate) and display
    it. My first attempt (limited to BST) would use gmtime to get GMT, add
    1 to the tm_hour member, knock off 24 if the adjusted hour exceeded
    23, and show it. This worked OK. However, I discovered the function
    mktime which I though might provide a more general solution. As I
    understand it, if you pass it a struct in which the number of the hour
    is outside the proper range, it will adjust things so that it isn't.

    Unfortunately it's not doing what I want. In the following code:

    struct tm *t;
    time_t lTime;

    time(&lTime);
    t = gmtime(&lTime);
    mktime(t);

    it is showing an hour ahead of GMT. Whereas if I comment out the
    mktime line it shows GMT correctly. I've tried setting t -> tm_isdst =
    0; between the gmtime and the mktime but this doesn't help. Why is
    mktime changing the time it is passed in? (By the way, my actual time
    zone is BST, ie currently one hour ahead of GMT.)

    Am I using it wrong, or am I misunderstanding what it's meant to do?

    Thanks for any help.
    Paul.
     
    Paul N, Apr 4, 2011
    #1
    1. Advertising

  2. Paul N <> writes:

    > I'm trying to write a program that will show the time at various
    > different places. The idea is to get the GMT time, adjust it for the
    > correct time zone offset (including DST if appropriate) and display
    > it. My first attempt (limited to BST) would use gmtime to get GMT, add
    > 1 to the tm_hour member, knock off 24 if the adjusted hour exceeded
    > 23, and show it. This worked OK. However, I discovered the function
    > mktime which I though might provide a more general solution. As I
    > understand it, if you pass it a struct in which the number of the hour
    > is outside the proper range, it will adjust things so that it isn't.
    >
    > Unfortunately it's not doing what I want. In the following code:
    >
    > struct tm *t;
    > time_t lTime;
    >
    > time(&lTime);
    > t = gmtime(&lTime);
    > mktime(t);
    >
    > it is showing an hour ahead of GMT. Whereas if I comment out the
    > mktime line it shows GMT correctly. I've tried setting t -> tm_isdst =
    > 0; between the gmtime and the mktime but this doesn't help. Why is
    > mktime changing the time it is passed in? (By the way, my actual time
    > zone is BST, ie currently one hour ahead of GMT.)
    >
    > Am I using it wrong, or am I misunderstanding what it's meant to do?


    mktime() expects local time - i.e. BST in your case. The UTC equivalent
    is timegm(), but it isn't implemented everywhere.

    --
    http://www.greenend.org.uk/rjk/
     
    Richard Kettlewell, Apr 4, 2011
    #2
    1. Advertising

  3. Paul N <> writes:
    > I'm trying to write a program that will show the time at various
    > different places. The idea is to get the GMT time, adjust it for the
    > correct time zone offset (including DST if appropriate) and display
    > it. My first attempt (limited to BST) would use gmtime to get GMT, add
    > 1 to the tm_hour member, knock off 24 if the adjusted hour exceeded
    > 23, and show it. This worked OK. However, I discovered the function
    > mktime which I though might provide a more general solution. As I
    > understand it, if you pass it a struct in which the number of the hour
    > is outside the proper range, it will adjust things so that it isn't.
    >
    > Unfortunately it's not doing what I want. In the following code:
    >
    > struct tm *t;
    > time_t lTime;
    >
    > time(&lTime);
    > t = gmtime(&lTime);
    > mktime(t);
    >
    > it is showing an hour ahead of GMT. Whereas if I comment out the
    > mktime line it shows GMT correctly. I've tried setting t -> tm_isdst =
    > 0; between the gmtime and the mktime but this doesn't help. Why is
    > mktime changing the time it is passed in? (By the way, my actual time
    > zone is BST, ie currently one hour ahead of GMT.)
    >
    > Am I using it wrong, or am I misunderstanding what it's meant to do?


    Quoting the standard's description of mktime() (C99 7.23.2.3p2)
    (emphasis added):

    The mktime function converts the broken-down time, *expressed
    as local time*, in the structure pointed to by timeptr into
    a calendar time value with the same encoding as that of the
    values returned by the time function.

    Unfortunately, there's no corresponding function that converts a
    broken-down time expressed in GMT/UTC.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Apr 4, 2011
    #3
  4. Paul N

    Paul N Guest

    On Apr 4, 8:31 pm, Keith Thompson <> wrote:
    > Paul N <> writes:
    > > I'm trying to write a program that will show the time at various
    > > different places. The idea is to get the GMT time, adjust it for the
    > > correct time zone offset (including DST if appropriate) and display
    > > it. My first attempt (limited to BST) would use gmtime to get GMT, add
    > > 1 to the tm_hour member, knock off 24 if the adjusted hour exceeded
    > > 23, and show it. This worked OK. However, I discovered the function
    > > mktime which I though might provide a more general solution. As I
    > > understand it, if you pass it a struct in which the number of the hour
    > > is outside the proper range, it will adjust things so that it isn't.

    >
    > > Unfortunately it's not doing what I want. In the following code:

    >
    > > struct tm *t;
    > > time_t lTime;

    >
    > > time(&lTime);
    > > t = gmtime(&lTime);
    > > mktime(t);

    >
    > > it is showing an hour ahead of GMT. Whereas if I comment out the
    > > mktime line it shows GMT correctly. I've tried setting t -> tm_isdst =
    > > 0; between the gmtime and the mktime but this doesn't help. Why is
    > > mktime changing the time it is passed in? (By the way, my actual time
    > > zone is BST, ie currently one hour ahead of GMT.)

    >
    > > Am I using it wrong, or am I misunderstanding what it's meant to do?

    >
    > Quoting the standard's description of mktime() (C99 7.23.2.3p2)
    > (emphasis added):
    >
    >     The mktime function converts the broken-down time, *expressed
    >     as local time*, in the structure pointed to by timeptr into
    >     a calendar time value with the same encoding as that of the
    >     values returned by the time function.
    >
    > Unfortunately, there's no corresponding function that converts a
    > broken-down time expressed in GMT/UTC.


    Thanks to you both. I'm still a bit puzzled as to what's going on -
    calling mktime twice only adjusts the hour once, it must somehow know
    it's already done it and I don't see how - but I think the best
    solution is probably for me to leave it well alone!

    Paul.
     
    Paul N, Apr 5, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Quetting

    mktime segfault

    Florian Quetting, Dec 31, 2004, in forum: C++
    Replies:
    2
    Views:
    534
    Florian Quetting
    Dec 31, 2004
  2. Rajmohan

    GMT equivalent of mktime

    Rajmohan, Jul 14, 2003, in forum: C Programming
    Replies:
    1
    Views:
    924
    Mark McIntyre
    Jul 14, 2003
  3. Gore

    mktime and DST.

    Gore, Oct 7, 2003, in forum: C Programming
    Replies:
    1
    Views:
    4,362
    Richard Bos
    Oct 7, 2003
  4. Bengt Richter

    time.mktime memory access violation bug

    Bengt Richter, Nov 18, 2003, in forum: Python
    Replies:
    6
    Views:
    744
    Bengt Richter
    Nov 21, 2003
  5. Paul N

    Problems with mktime

    Paul N, Apr 4, 2011, in forum: C Programming
    Replies:
    3
    Views:
    1,922
    Paul N
    Apr 5, 2011
Loading...

Share This Page