replacement for <ctime>?

Discussion in 'C++' started by Ernst Murnleitner, Jul 16, 2003.

  1. Hello Readers,

    I am looking for a replacement of <ctime>.

    for the functions

    gmtime
    mktime

    One reason is, that these time functions use statically allocated memory but
    I want to use it in an multithreaded program.
    Another reason is, that mktime is not the reverse of gmtime (but of
    localtime).

    The only thing I need is: convert a time_t value into struct tm and back -
    but without daylight saving and time zones. The time should be in UTC.

    Unfortunately mktime uses the timezone (OK, I could set the timezone
    temporarily to GMT, but this is not clean).

    I think there is already an open source code somewhere? (The formula for
    converting struct tm into time_t would also be helpful).

    Greetings and best thanks in advance

    Ernst Murnleitner
    Ernst Murnleitner, Jul 16, 2003
    #1
    1. Advertising

  2. "Ernst Murnleitner" <> wrote...
    > I am looking for a replacement of <ctime>.
    > [...]
    > I think there is already an open source code somewhere?


    If there is, why are you looking here? www.google.com is
    your friend.

    Check out posts by Nikki Locke here about every month. They
    are titled "Available Libraries FAQ" or something like that.
    There is a link to a web site with tons of libraries mentioned
    (commercial and otherwise).

    Also, ask in a newsgroup for your OS. Every OS has some kind
    of time functionality provided. They can also tell you if it
    is thread-safe or not.

    Victor
    Victor Bazarov, Jul 16, 2003
    #2
    1. Advertising

  3. "Ernst Murnleitner" <> wrote...
    > > > I am looking for a replacement of <ctime>.
    > > > [...]

    > I do not want to use something OS dependent. Therefore I asked here.


    Why not? On how many platforms are you planning to run your
    program? How many platforms do you think there are that do
    not have any calendar support? It is quite possible that
    what you're looking for does not exist independent from OS.

    Victor
    Victor Bazarov, Jul 16, 2003
    #3
  4. "Victor Bazarov" <> schrieb im Newsbeitrag
    news:...
    > > I do not want to use something OS dependent. Therefore I asked here.

    >
    > Why not? On how many platforms are you planning to run your
    > program? How many platforms do you think there are that do
    > not have any calendar support? It is quite possible that
    > what you're looking for does not exist independent from OS.


    I think for what I need, device dependence is not necessary.

    The program should run on Linux and Windows. > 95 % is device independent
    (only serial interface and tcp/ip not).
    The user interface is extra (but here I also use http/html and QT).

    Greetings
    Ernst
    Ernst Murnleitner, Jul 17, 2003
    #4
  5. Ernst Murnleitner wrote:
    >
    > Hello Readers,
    >
    > I am looking for a replacement of <ctime>.
    >
    > for the functions
    >
    > gmtime
    > mktime


    Try <http://www.boost.org/libs/date_time/doc/index.html>.

    >
    > One reason is, that these time functions use statically allocated memory but
    > I want to use it in an multithreaded program.


    http://www.opengroup.org/onlinepubs/007904975/functions/gmtime_r.html
    (note "_r")

    regards,
    alexander.
    Alexander Terekhov, Jul 17, 2003
    #5
  6. Ernst Murnleitner, Jul 17, 2003
    #6
  7. Alexander Terekhov <> wrote in message news:<>...

    > Ernst Murnleitner wrote:
    > >
    > > Hello Readers,
    > >
    > > I am looking for a replacement of <ctime>.
    > >
    > > for the functions
    > >
    > > gmtime
    > > mktime

    >
    > Try <http://www.boost.org/libs/date_time/doc/index.html>.


    The Boost Date/Time library uses the non-reentrant localtime and
    gmtime functions from <ctime>...

    > > One reason is, that these time functions use statically allocated memory but
    > > I want to use it in an multithreaded program.


    ....so if the app is threaded, it may be prudent to avoid the library,
    or at least those parts which directly or indirectly rely on localtime
    or gmtime, unless the implementation happens to use thread-local
    storage or something.

    - Shane
    Shane Beasley, Jul 17, 2003
    #7
  8. "Ernst Murnleitner" <> wrote in message news:<bf3cso$ak000$-berlin.de>...

    > I am looking for a replacement of <ctime>.
    >
    > for the functions
    >
    > gmtime
    > mktime
    >
    > One reason is, that these time functions use statically allocated memory but
    > I want to use it in an multithreaded program.


    On a POSIX system, you have functions like gmtime_r, mktime_r, etc.,
    available, which take a pointer to storage rather than using static
    storage. Versions of these are implemented as part of the tz library,
    available here:

    <http://www.twinsun.com/tz/tz-link.htm>

    Parts of this package apparently are included as parts of most popular
    Unix systems (e.g., every one I've used except AIX), plus DJGPP and
    Cygwin, so you're already set on those platforms. If you're trying for
    Visual C++ under Windows, this might at least give you a springboard
    from which to work. (Perhaps you could coerce Cygwin to generate
    MSVC-compatible DLLs for you?)

    NB: This code has two header files. One is named "private.h" [for
    obvious reasons], and the other doesn't prototype the relevant
    functions. I made it work by doing it myself:

    extern "C" {
    void tzset (); /* call this first!! */
    char *asctime_r (const struct tm *tm, char *buf);
    char *ctime_r (const time_t *timep, char *buf);
    struct tm *gmtime_r (const time_t *timep, struct tm *result);
    struct tm *localtime_r (const time_t *timep, struct tm *result);
    }

    g++ -ansi -pedantic -Wall -Werror test.cpp -ltz
    TZ='CST6CDT' ./a.out

    Note also that you have to specify your time zone manually, either via
    the TZ environment variable or an external time-zone file whose name
    is compiled into the binary. Check the docs for more info on that.

    > Another reason is, that mktime is not the reverse of gmtime (but of
    > localtime).


    Haven't seen one of those. However, you can do it yourself; see below.

    > The only thing I need is: convert a time_t value into struct tm and back -
    > but without daylight saving and time zones. The time should be in UTC.
    >
    > Unfortunately mktime uses the timezone (OK, I could set the timezone
    > temporarily to GMT, but this is not clean).
    >
    > I think there is already an open source code somewhere? (The formula for
    > converting struct tm into time_t would also be helpful).


    <http://aa.usno.navy.mil/faq/docs/JD_Formula.html> supplies Fortran
    code, easily convertible to C++, to convert between Julian and
    Gregorian dates. One can convert time_t to Julian by dividing by the
    number of seconds in a day and adding 2440588 (Julian for 1 Jan 1970);
    the inverse will convert it back.

    This code works great with GMT/UTC, but time zones are another mess
    altogether. The ISO C time interface doesn't seem to know anything
    about them except that some do daylight savings time on occasion; you
    can't find out which one you're in or whether a particular date in
    that zone was in daylight savings time at the time.

    I guess that's what the tz library is for. :)

    Good luck!

    - Shane
    Shane Beasley, Jul 17, 2003
    #8
  9. Ernst Murnleitner

    Limech Guest

    Limech, Jul 20, 2003
    #9
    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. Tivo Escobar

    Question about ctime

    Tivo Escobar, Jul 23, 2003, in forum: C++
    Replies:
    1
    Views:
    395
    Victor Bazarov
    Jul 23, 2003
  2. Oplec
    Replies:
    2
    Views:
    512
    Oplec
    Oct 26, 2003
  3. Kevin

    ctime Help

    Kevin, May 16, 2004, in forum: C++
    Replies:
    3
    Views:
    790
    osmium
    May 16, 2004
  4. wukexin
    Replies:
    1
    Views:
    473
    Karl Heinz Buchegger
    May 25, 2005
  5. Hao Xu
    Replies:
    2
    Views:
    731
    Mark McIntyre
    Aug 17, 2004
Loading...

Share This Page