home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / i18nv102.zip / SAMPLE / DT / DT.C < prev    next >
C/C++ Source or Header  |  1995-08-28  |  4KB  |  89 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_ptr;                  /* Pointer to date/time structure.   */
  24.   char temp_string[100];                /* String to hold temp values.       */
  25.   char *ret_string;                     /* Output string.                    */
  26.   time_t temp_time;                     /* Variable used to get current time.*/
  27.   wchar_t temp_w_string[200];           /* Temp string of wchar_t's.         */
  28.  
  29.                                         /* Print a header.                   */
  30.   printf("\nLocale for date and time: %s\n", locale);
  31.  
  32.                                         /* Allocate memory for tm structure. */
  33.                                         /* If structure allocated OK...      */
  34.   time_ptr = (struct tm *) malloc(sizeof(struct tm));
  35.   if (time_ptr != NULL)
  36.   {
  37.                                         /* Copy a date and time into a string*/
  38.                                         /* Copy into structure with strptime */
  39.                                         /* If copied OK...                   */
  40.                                         /*  Format date/time from structure  */
  41.                                         /*  Print it out.                    */
  42.                                         /* Else, print an error message.     */
  43.      strcpy(temp_string, "12:34:56 10 21 93");
  44.      ret_string = strptime(temp_string, "%T %m %d %y", time_ptr);
  45.      if   (ret_string != NULL)
  46.      {
  47.           strftime(temp_string, 100,
  48.                    "Loaded in date: %9x and time: %9X  ", time_ptr);
  49.           printf("\n%s\n", temp_string);
  50.      }
  51.      else printf("\nstrptime returned NULL");
  52.  
  53.                                         /* Get the current time.             */
  54.                                         /* Convert to local time.            */
  55.                                         /* Format the time into a pretty str.*/
  56.                                         /* Print out the string.             */
  57.      temp_time = time(NULL);
  58.      time_ptr = localtime(&temp_time);
  59.      strftime(temp_string, 100,
  60.               "Today is: %A, %B %d %Y. Time is: %9X %p", time_ptr);
  61.      printf("\n%s\n", temp_string);
  62.  
  63.                                         /* Convert exactly the same time with*/
  64.                                         /*  the wchar_t functions and types. */
  65.                                         /* Print the output.                 */
  66.      wcsftime(temp_w_string, 100,
  67.               "Today is: %A, %B %d %Y. Time is: %9X %p", time_ptr);
  68.      printf("\nDate converted with wchar_t type:\n%S\n", temp_w_string);
  69.  
  70.                                         /* Free up allocated structure.      */
  71.      free (time_ptr);
  72.   }
  73. }
  74.  
  75. /*****************************************************************************/
  76. /***     Main program                                                      ***/
  77. /*****************************************************************************/
  78.  
  79. void main(void)
  80. {
  81.                                         /* Set the locale based on env var.  */
  82.                                         /* Get the locale for date/time.     */
  83.   setlocale(LC_ALL, "");
  84.   locale = setlocale(LC_TIME, NULL);
  85.  
  86.                                         /* Show date and time functions.     */
  87.   date_time_functs();
  88. }
  89.