replacement for <ctime>?

E

Ernst Murnleitner

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
 
V

Victor Bazarov

Ernst Murnleitner said:
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
 
V

Victor Bazarov

Ernst Murnleitner said:
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
 
E

Ernst Murnleitner

Victor Bazarov said:
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
 
S

Shane Beasley

Ernst Murnleitner said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top