swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getting the time....


From: glen e. p. ropella
Subject: getting the time....
Date: Mon, 17 Mar 1997 13:15:36 -0700

ginger> Hi, Swarmfolk, I have a Not-Deep problem.  I want to simply
ginger> get the current time string (the date, in English) to add as a
ginger> comment to my output files (Program Version Ran Date).  On my
ginger> system, the following works:

ginger>     char* timeString() { // there ought to be an easier
ginger> way....  int timeofday; struct timeval tp;
    
ginger>         timeofday = gettimeofday (&tp, (struct timezone *) 0);
    
ginger>         return( ctime ( &tp.tv_sec)); }

ginger> But apparently this isn't very portable. I don't care how I do
ginger> it, but feel it needs to be done.  What's a "standard" Unix
ginger> way to do it?

Hmmm.  Well, if you're trying to standardize, POSIX is the way
to do it.  But, I failed to find a good reference.  Try using
time() in conjunction with localtime(), gmtime(), or asctime().    

Here's an example I found somewhere:
-------------------------------------------------------------------
     #include <stdio.h>
     #include <time.h>
     int main (void) {
       char s[256];
       time_t current_time;
       struct tm *local_time;
     
       current_time = time (NULL);
       local_time = localtime (&current_time);

       (void) fputs (asctime (local_time), stdout);

       strftime (s, 256, "Date: %A, %B %d.\n", local_time);
       (void) fputs (s, stdout);
       strftime (s, 256, "Time: %I:%M %p.\n", local_time);
       (void) fputs (s, stdout);
     
       return 0;
     }

which might translate to:

    char* timeString()
    {
       time_t current_time;
       struct tm *local_time;

       current_time = time(NULL);
       local_time = localtime (&current_time);

       return( asctime (local_time));
    }

Any better????

glen


reply via email to

[Prev in Thread] Current Thread [Next in Thread]