home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / learn / times.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-03  |  2.3 KB  |  71 lines

  1. /* TIMES.C illustrates various time and date functions including:
  2.  *      time            ftime           ctime
  3.  *      localtime       asctime         gmtime          mktime
  4.  *      _strtime        _strdate        tzset
  5.  *
  6.  * Also the global variable:
  7.  *      tzname
  8.  */
  9.  
  10. #include <time.h>
  11. #include <stdio.h>
  12. #include <sys\types.h>
  13. #include <sys\timeb.h>
  14. #include <string.h>
  15.  
  16. main()
  17. {
  18.     char timebuf[9], datebuf[9];
  19.     static char ampm[] = "AM";
  20.     time_t ltime;
  21.     struct timeb tstruct;
  22.     struct tm *today, *tomorrow, *gmt;
  23.  
  24.     /* Set time zone from TZ environment variable. If TZ is not set,
  25.      * PST8PDT is used (Pacific standard time, daylight savings).
  26.      */
  27.     tzset();
  28.  
  29.     /* Get Xenix-style time and display as number and string. */
  30.     time( <ime );
  31.     printf( "Time in seconds since GMT 1/1/70:\t%ld\n", ltime );
  32.     printf( "Xenix time and date:\t\t\t%s", ctime( <ime ) );
  33.  
  34.     /* Display GMT. */
  35.     gmt = gmtime( <ime );
  36.     printf( "Greenwich Mean Time:\t\t\t%s", asctime( gmt ) );
  37.  
  38.     /* Convert to local time structure and adjust for PM if necessary. */
  39.     today = localtime( <ime );
  40.     if( today->tm_hour > 12 )
  41.     {
  42.         strcpy( ampm, "PM" );
  43.         today->tm_hour -= 12;
  44.     }
  45.     /* Note how pointer addition is used to skip the first 11 characters
  46.      * and printf is used to trim off terminating characters.
  47.      */
  48.     printf( "12-hour time:\t\t\t\t%.8s %s\n",
  49.             asctime( today ) + 11, ampm );
  50.  
  51.     /* Make tomorrow's time. */
  52.     *tomorrow = *today;
  53.     tomorrow->tm_mday++;
  54.     if( mktime( tomorrow ) != (time_t)-1 )
  55.         printf( "Tomorrow's date:\t\t\t%d/%d/%.2d\n",
  56.                 tomorrow->tm_mon + 1, tomorrow->tm_mday, tomorrow->tm_year );
  57.  
  58.     /* Display DOS date and time. */
  59.     _strtime( timebuf );
  60.     printf( "DOS time:\t\t\t\t%s\n", timebuf );
  61.     _strdate( datebuf );
  62.     printf( "DOS date:\t\t\t\t%s\n", datebuf );
  63.  
  64.     /* Print additional time information. */
  65.     ftime( &tstruct );
  66.     printf( "Plus miliseconds:\t\t\t%u\n", tstruct.millitm );
  67.     printf( "Zone difference in seconds from GMT:\t%u\n", tstruct.timezone );
  68.     printf( "Time zone name:\t\t\t\t%s\n", tzname[0] );
  69.     printf( "Daylight savings:\t\t\t%s\n", tstruct.dstflag ? "YES" : "NO" );
  70. }
  71.