Thanks everyone

I did a search to insure that time.h had the correct struct tm definition, 
recoded and tested.

The reason why I rejected the code the first time was that year was coming back 
at 111 and month as zero.  Adding 1900 to year and 1 to month calibrated the 
date, so I am happy..

Thanks to all for their advice and for confirming with me that the localtime() 
function actually returned the proper structure.

>From my previous email I posted how I took a second approach, which was to 
>calculate the number of seconds since the epoch. The epoch is noon at 
>1970-1-1,  Adjusting GMT-5 brought my code into alignment.

Ergo, I have two solutions.  Again, thanks to all

------------------

Regards  
 Leslie
 Mr. Leslie Satenstein
40 years in IT and going strong.
Yesterday was a good day, today is a better day,
and tomorrow will be even better.
 
mailto:[email protected]
alternative: [email protected] 
www.itbms.biz / www.eclipseguard.com
 


--- On Sun, 1/2/11, Jérôme Oufella <[email protected]> wrote:

> From: Jérôme Oufella <[email protected]>
> Subject: Re: [MLUG] Date Routines in Linux C
> To: "Montreal Linux Users Group" <[email protected]>
> Date: Sunday, January 2, 2011, 5:04 PM
> Happy new year all.
> 
> Leslie, you may also want to have a look to strftime(3) if
> you need fancy formatting.
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
> 
> #define STRMAX 64
> 
> int
> main(int argc, char **argv) {
>     time_t now = time(NULL);
>     if (now == (time_t) -1 ) {
>         perror("time");
>         exit(1);  
>     }
> 
>     struct tm *now_tm =
> localtime(&now);
>     if (now_tm == NULL) {
>         perror("localtime");
>         exit(1);
>     }
>         
>     char outstring[STRMAX];
>     size_t ret = strftime(outstring, STRMAX,
> "Today is %A, %B %d.", now_tm);
>     if (ret == 0) {
>         perror("failure in
> strftime");
>         exit(1);
>     }
> 
>     printf("%s\n", outstring);
> 
>     return 0;
> }
> _______________________________________________
> mlug mailing list
> [email protected]
> https://listes.koumbit.net/cgi-bin/mailman/listinfo/mlug-listserv.mlug.ca
> 
_______________________________________________
mlug mailing list
[email protected]
https://listes.koumbit.net/cgi-bin/mailman/listinfo/mlug-listserv.mlug.ca

Reply via email to