home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / i18nv104.zip / SAMPLE / DT / DT.C < prev    next >
C/C++ Source or Header  |  1996-02-13  |  4KB  |  82 lines

  1. /*****************************************************************************/
  2. /***     Include files                                                     ***/
  3. /*****************************************************************************/
  4.  
  5. #include <stdio.h>                      /* Standard IO functions.            */
  6. #include <time.h>                       /* Standard date/time functions.     */
  7.  
  8. #include <wchar.h>                      /* XPG/4 library.                    */
  9. #include <locale.h>                     /* Locale definitions.               */
  10.  
  11. /*****************************************************************************/
  12. /***     Global variables                                                  ***/
  13. /*****************************************************************************/
  14.  
  15. char *locale;                           /* Name of locale string.            */
  16.  
  17. /*****************************************************************************/
  18. /***     Sample date and time conversion                                   ***/
  19. /*****************************************************************************/
  20.  
  21. void date_time_functs(void)
  22. {
  23.   struct tm time_struct;                /* date/time structure.              */
  24.   struct tm *time_ptr;                  /* date/time structure ptr.          */
  25.   char temp_string[100];                /* String to hold temp values.       */
  26.   char *ret_string;                     /* Output string.                    */
  27.   time_t temp_time;                     /* Variable used to get current time.*/
  28.   wchar_t temp_w_string[200];           /* Temp string of wchar_t's.         */
  29.  
  30.                                         /* Print a header.                   */
  31.   printf("\nLocale for date and time: %s\n", locale);
  32.  
  33.                                         /* Copy a date and time into a string*/
  34.                                         /* Copy into structure with strptime */
  35.                                         /* If copied OK...                   */
  36.                                         /*  Format date/time from structure  */
  37.                                         /*  Print it out.                    */
  38.                                         /* Else, print an error message.     */
  39.      strcpy(temp_string, "12:34:56 10 21 93");
  40.      ret_string = strptime(temp_string, "%T %m %d %y", &time_struct);
  41.      if   (ret_string != NULL)
  42.      {
  43.           strftime(temp_string, 100,
  44.                    "Loaded in date: %9x and time: %9X  ", &time_struct);
  45.           printf("\n%s\n", temp_string);
  46.      }
  47.      else printf("\nstrptime returned NULL");
  48.  
  49.                                         /* Get the current time.             */
  50.                                         /* Convert to local time.            */
  51.                                         /* Format the time into a pretty str.*/
  52.                                         /* Print out the string.             */
  53.      temp_time = time(NULL);
  54.      time_ptr = localtime(&temp_time);
  55.      strftime(temp_string, 100,
  56.               "Today is: %A, %B %d %Y. Time is: %9X %p", time_ptr);
  57.      printf("\n%s\n", temp_string);
  58.  
  59.                                         /* Convert exactly the same time with*/
  60.                                         /*  the wchar_t functions and types. */
  61.                                         /* Print the output.                 */
  62.      wcsftime(temp_w_string, 100,
  63.               "Today is: %A, %B %d %Y. Time is: %9X %p", time_ptr);
  64.      printf("\nDate converted with wchar_t type:\n%S\n", temp_w_string);
  65.  
  66. }
  67.  
  68. /*****************************************************************************/
  69. /***     Main program                                                      ***/
  70. /*****************************************************************************/
  71.  
  72. void main(void)
  73. {
  74.                                         /* Set the locale based on env var.  */
  75.                                         /* Get the locale for date/time.     */
  76.   setlocale(LC_ALL, "");
  77.   locale = setlocale(LC_TIME, NULL);
  78.  
  79.                                         /* Show date and time functions.     */
  80.   date_time_functs();
  81. }
  82.