home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / samples / date / date.c next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  4.9 KB  |  185 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright International Business Machines Corporation, 1998, 1999     *
  6. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  7. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  8. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  9. *                                                                             *
  10. *******************************************************************************
  11. *
  12. * File date.c
  13. *
  14. * Modification History:
  15. *
  16. *   Date        Name        Description
  17. *   06/11/99    stephen     Creation.
  18. *   06/16/99    stephen     Modified to use uprint.
  19. *******************************************************************************
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24.  
  25. #include "utypes.h"
  26. #include "ustring.h"
  27.  
  28. #include "ucnv.h"
  29. #include "udat.h"
  30. #include "ucal.h"
  31.  
  32. #include "uprint.h"
  33.  
  34. /* Protos */
  35. static void usage();
  36. static void version();
  37. static void date(const UChar *tz, UDateFormatStyle style, UErrorCode *status);
  38. int main(int argc, char **argv);
  39.  
  40.  
  41. /* The version of date */
  42. #define DATE_VERSION "1.0"
  43.  
  44. /* "GMT" */
  45. static const UChar GMT_ID [] = { 0x0047, 0x004d, 0x0054, 0x0000 };
  46.  
  47.  
  48. int
  49. main(int argc,
  50.      char **argv)
  51. {
  52.   int printUsage = 0;
  53.   int printVersion = 0;
  54.   int optind = 1;
  55.   char *arg;
  56.   const UChar *tz = 0;
  57.   UDateFormatStyle style = UDAT_DEFAULT;
  58.   UErrorCode status = U_ZERO_ERROR;
  59.  
  60.  
  61.   /* parse the options */
  62.   for(optind = 1; optind < argc; ++optind) {
  63.     arg = argv[optind];
  64.     
  65.     /* version info */
  66.     if(strcmp(arg, "-v") == 0 || strcmp(arg, "--version") == 0) {
  67.       printVersion = 1;
  68.     }
  69.     /* usage info */
  70.     else if(strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
  71.       printUsage = 1;
  72.     }
  73.     /* display date in gmt */
  74.     else if(strcmp(arg, "-u") == 0 || strcmp(arg, "--gmt") == 0) {
  75.       tz = GMT_ID;
  76.     }
  77.     /* display date in gmt */
  78.     else if(strcmp(arg, "-f") == 0 || strcmp(arg, "--full") == 0) {
  79.       style = UDAT_FULL;
  80.     }
  81.     /* display date in long format */
  82.     else if(strcmp(arg, "-l") == 0 || strcmp(arg, "--long") == 0) {
  83.       style = UDAT_LONG;
  84.     }
  85.     /* display date in medium format */
  86.     else if(strcmp(arg, "-m") == 0 || strcmp(arg, "--medium") == 0) {
  87.       style = UDAT_MEDIUM;
  88.     }
  89.     /* display date in short format */
  90.     else if(strcmp(arg, "-s") == 0 || strcmp(arg, "--short") == 0) {
  91.       style = UDAT_SHORT;
  92.     }
  93.     /* POSIX.1 says all arguments after -- are not options */
  94.     else if(strcmp(arg, "--") == 0) {
  95.       /* skip the -- */
  96.       ++optind;
  97.       break;
  98.     }
  99.     /* unrecognized option */
  100.     else if(strncmp(arg, "-", strlen("-")) == 0) {
  101.       printf("date: invalid option -- %s\n", arg+1);
  102.       printUsage = 1;
  103.     }
  104.     /* done with options, display date */
  105.     else {
  106.       break;
  107.     }
  108.   }
  109.  
  110.   /* print usage info */
  111.   if(printUsage) {
  112.     usage();
  113.     return 0;
  114.   }
  115.  
  116.   /* print version info */
  117.   if(printVersion) {
  118.     version();
  119.     return 0;
  120.   }
  121.  
  122.   /* print the date */
  123.   date(tz, style, &status);
  124.  
  125.   return (U_FAILURE(status) ? 1 : 0);
  126. }
  127.  
  128. /* Usage information */
  129. static void
  130. usage()
  131. {  
  132.   puts("Usage: date [OPTIONS]");
  133.   puts("Options:");
  134.   puts("  -h, --help        Print this message and exit.");
  135.   puts("  -v, --version     Print the version number of date and exit.");
  136.   puts("  -u, --gmt         Display the date in Greenwich Mean Time.");
  137.   puts("  -f, --full        Use full display format.");
  138.   puts("  -l, --long        Use long display format.");
  139.   puts("  -m, --medium      Use medium display format.");
  140.   puts("  -s, --short       Use short display format.");
  141. }
  142.  
  143. /* Version information */
  144. static void
  145. version()
  146. {
  147.   printf("date version %s (ICU version %s), by Stephen F. Booth.\n", 
  148.      DATE_VERSION, ICU_VERSION);
  149.   puts("(C) Copyright International Business Machines Corporation, 1998, 1999");
  150.   puts("Licensed Material - Program-Property of IBM - All Rights Reserved.");
  151.   puts("US Government Users Restricted Rights - Use, duplication, or disclosure");
  152.   puts("restricted by GSA ADP Schedule Contract with IBM Corp.");
  153. }
  154.  
  155. /* Format the date */
  156. static void
  157. date(const UChar *tz,
  158.      UDateFormatStyle style,
  159.      UErrorCode *status)
  160. {
  161.   UChar *s = 0;
  162.   int32_t len = 0;
  163.   UDateFormat *fmt;
  164.  
  165.   fmt = udat_open(style, style, 0, tz, -1, status);
  166.   len = udat_format(fmt, ucal_getNow(), 0, len, 0, status);
  167.   if(*status == U_BUFFER_OVERFLOW_ERROR) {
  168.     *status = U_ZERO_ERROR;
  169.     s = (UChar*) malloc(sizeof(UChar) * (len+1));
  170.     if(s == 0) goto finish;
  171.     udat_format(fmt, ucal_getNow(), s, len + 1, 0, status);
  172.     if(U_FAILURE(*status)) goto finish;
  173.   }
  174.  
  175.   /* print the date string */
  176.   uprint(s, stdout, status);
  177.  
  178.   /* print a trailing newline */
  179.   printf("\n");
  180.  
  181.  finish:
  182.   udat_close(fmt);
  183.   free(s);
  184. }
  185.