]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/samples/date/date.c
ICU-64232.0.1.tar.gz
[apple/icu.git] / icuSources / samples / date / date.c
index 9ade2134ccb59186c2f19014f349334a573e77ff..400fe2c90555005a0137107dd8b0085a407f4b31 100644 (file)
@@ -1,6 +1,10 @@
 /*
-**********************************************************************
-*   Copyright (C) 1998-2011, International Business Machines
+*************************************************************************
+*   © 2016 and later: Unicode, Inc. and others.
+*   License & terms of use: http://www.unicode.org/copyright.html#License
+*************************************************************************
+***********************************************************************
+*   Copyright (C) 1998-2012, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 **********************************************************************
 *
@@ -44,8 +48,8 @@ int main(int argc, char **argv)
 /* Protos */
 static void usage(void);
 static void version(void);
-static void date(UDate when, const UChar *tz, UDateFormatStyle style, const char *format, UErrorCode *status);
-static UDate getWhen(const char *millis, const char *seconds, const char *format, UDateFormatStyle style, const char *parse, const UChar *tz, UErrorCode *status);
+static void date(UDate when, const UChar *tz, UDateFormatStyle style, const char *format, const char *locale, UErrorCode *status);
+static UDate getWhen(const char *millis, const char *seconds, const char *format, const char *locale, UDateFormatStyle style, const char *parse, const UChar *tz, UErrorCode *status);
 
 UConverter *cnv = NULL;
 
@@ -64,20 +68,21 @@ main(int argc,
 {
   int printUsage = 0;
   int printVersion = 0;
-  int optind = 1;
+  int optInd = 1;
   char *arg;
   const UChar *tz = 0;
   UDateFormatStyle style = UDAT_DEFAULT;
   UErrorCode status = U_ZERO_ERROR;
   const char *format = NULL;
+  const char *locale = NULL;
   char *parse = NULL;
   char *seconds = NULL;
   char *millis = NULL;
   UDate when;
 
   /* parse the options */
-  for(optind = 1; optind < argc; ++optind) {
-    arg = argv[optind];
+  for(optInd = 1; optInd < argc; ++optInd) {
+    arg = argv[optInd];
     
     /* version info */
     if(strcmp(arg, "-v") == 0 || strcmp(arg, "--version") == 0) {
@@ -108,30 +113,36 @@ main(int argc,
       style = UDAT_SHORT;
     }
     else if(strcmp(arg, "-F") == 0 || strcmp(arg, "--format") == 0) {
-      if ( optind + 1 < argc ) { 
-         optind++;
-         format = argv[optind];
+      if ( optInd + 1 < argc ) { 
+         optInd++;
+         format = argv[optInd];
       }
     } else if(strcmp(arg, "-r") == 0) {
-      if ( optind + 1 < argc ) { 
-         optind++;
-         seconds = argv[optind];
+      if ( optInd + 1 < argc ) { 
+         optInd++;
+         seconds = argv[optInd];
       }
     } else if(strcmp(arg, "-R") == 0) {
-      if ( optind + 1 < argc ) { 
-         optind++;
-         millis = argv[optind];
+      if ( optInd + 1 < argc ) { 
+         optInd++;
+         millis = argv[optInd];
       }
     } else if(strcmp(arg, "-P") == 0) {
-      if ( optind + 1 < argc ) { 
-         optind++;
-         parse = argv[optind];
+      if ( optInd + 1 < argc ) { 
+         optInd++;
+         parse = argv[optInd];
+      }
+    }
+    else if (strcmp(arg, "-L") == 0) {
+      if (optInd + 1 < argc) {
+         optInd++;
+         locale = argv[optInd];
       }
     }
     /* POSIX.1 says all arguments after -- are not options */
     else if(strcmp(arg, "--") == 0) {
       /* skip the -- */
-      ++optind;
+      ++optInd;
       break;
     }
     /* unrecognized option */
@@ -158,13 +169,13 @@ main(int argc,
   }
 
   /* get the 'when' (or now) */
-  when = getWhen(millis, seconds, format, style, parse, tz, &status);
+  when = getWhen(millis, seconds, format, locale, style, parse, tz, &status);
   if(parse != NULL) {
     format = FORMAT_MILLIS; /* output in millis */
   }
 
   /* print the date */
-  date(when, tz, style, format, &status);
+  date(when, tz, style, format, locale, &status);
 
   ucnv_close(cnv);
 
@@ -190,6 +201,7 @@ usage()
   puts("  -r <seconds>      Use <seconds> as the time (Epoch 1970) rather than now.");
   puts("  -R <millis>       Use <millis> as the time (Epoch 1970) rather than now.");
   puts("  -P <string>       Parse <string> as the time, output in millis format.");
+  puts("  -L <string>       Use the locale <string> instead of the default ICU locale.");
 }
 
 /* Version information */
@@ -241,6 +253,7 @@ date(UDate when,
      const UChar *tz,
      UDateFormatStyle style,
      const char *format,
+     const char *locale,
      UErrorCode *status )
 {
   UChar *s = 0;
@@ -252,15 +265,15 @@ date(UDate when,
 
   if( format != NULL ) {
     if(!strcmp(format,FORMAT_MILLIS)) {
-      printf("%.0lf\n", when);
+      printf("%.0f\n", when);
       return;
     } else if(!strcmp(format, FORMAT_SECONDS)) {
-      printf("%.3lf\n", when/1000.0);
+      printf("%.3f\n", when/1000.0);
       return;
     }
   }
 
-  fmt = udat_open(style, style, 0, tz, -1,NULL,0, status);
+  fmt = udat_open(style, style, locale, tz, -1,NULL,0, status);
   if ( format != NULL ) {
     charsToUCharsDefault(uFormat,sizeof(uFormat)/sizeof(uFormat[0]),format,-1,status);
     udat_applyPattern(fmt,FALSE,uFormat,-1);
@@ -288,7 +301,7 @@ date(UDate when,
   free(s);
 }
 
-static UDate getWhen(const char *millis, const char *seconds, const char *format, 
+static UDate getWhen(const char *millis, const char *seconds, const char *format, const char *locale,
                      UDateFormatStyle style, const char *parse, const UChar *tz, UErrorCode *status) {
   UDateFormat *fmt = NULL; 
   UChar uFormat[100];
@@ -315,7 +328,7 @@ static UDate getWhen(const char *millis, const char *seconds, const char *format
       }
     }
 
-    fmt = udat_open(style, style, 0, tz, -1,NULL,0, status);
+    fmt = udat_open(style, style, locale, tz, -1,NULL,0, status);
     if ( format != NULL ) {
       charsToUCharsDefault(uFormat,sizeof(uFormat)/sizeof(uFormat[0]), format,-1,status);
       udat_applyPattern(fmt,FALSE,uFormat,-1);
@@ -325,7 +338,7 @@ static UDate getWhen(const char *millis, const char *seconds, const char *format
     when = udat_parse(fmt, uParse, -1, &parsepos, status);
     if(U_FAILURE(*status)) {
       fprintf(stderr, "Error in Parse: %s\n", u_errorName(*status));
-      if(parsepos>0&&parsepos<=strlen(parse)) {
+      if(parsepos > 0 && parsepos <= (int32_t)strlen(parse)) {
         fprintf(stderr, "ERR>\"%s\" @%d\n"
                         "ERR> %*s^\n",
                 parse,parsepos,parsepos,"");