Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > replacement for <ctime>?

Reply
Thread Tools

replacement for <ctime>?

 
 
Ernst Murnleitner
Guest
Posts: n/a
 
      07-16-2003
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





 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-16-2003
"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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-16-2003
"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


 
Reply With Quote
 
Ernst Murnleitner
Guest
Posts: n/a
 
      07-17-2003

"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



 
Reply With Quote
 
Alexander Terekhov
Guest
Posts: n/a
 
      07-17-2003

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/.../gmtime_r.html
(note "_r")

regards,
alexander.
 
Reply With Quote
 
Ernst Murnleitner
Guest
Posts: n/a
 
      07-17-2003
Dear Alexander,

> > I am looking for a replacement of <ctime>.

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


OK, I already had a look to it. But as I already used the ansi functions,
your second link is maybe better for me.

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


Thank you.


 
Reply With Quote
 
Shane Beasley
Guest
Posts: n/a
 
      07-17-2003
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
 
Reply With Quote
 
Shane Beasley
Guest
Posts: n/a
 
      07-17-2003
"Ernst Murnleitner" <> wrote in message news:<bf3cso$ak000$>...

> 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
 
Reply With Quote
 
Limech
Guest
Posts: n/a
 
      07-20-2003
> I am looking for a replacement of <ctime>.

Maybe XChron C++?
http://www.theprogrammerstoolbox.com/xchron.htm

I've never used it. Don't know if it's multi-thread safe.
Wish I had heard of it a couple of years back.

Cheers
Limech



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacement PSU for an ADSL Router Edward W. Thompson Wireless Networking 0 09-06-2005 10:40 AM
Replacement for FF calendar extension Dan Firefox 5 03-23-2005 08:38 PM
Replacement for AOL newsgroup access EJGroth Firefox 0 02-08-2005 02:41 PM
Calendar replacement William W. Plummer Firefox 1 07-02-2004 04:10 PM
Replacement For Access Point KS Wireless Networking 1 06-24-2004 04:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57