• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 *   Copyright (C) 1998-2009, International Business Machines
6 *   Corporation and others.  All Rights Reserved.
7 **********************************************************************
8 *
9 * File date.c
10 *
11 * Modification History:
12 *
13 *   Date        Name        Description
14 *   06/11/99    stephen     Creation.
15 *   06/16/99    stephen     Modified to use uprint.
16 *******************************************************************************
17 */
18 
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "unicode/utypes.h"
25 #include "unicode/ustring.h"
26 #include "unicode/uclean.h"
27 
28 #include "unicode/ucnv.h"
29 #include "unicode/udat.h"
30 #include "unicode/ucal.h"
31 
32 #include "uprint.h"
33 
34 int main(int argc, char **argv);
35 
36 #if UCONFIG_NO_FORMATTING
37 
main(int argc,char ** argv)38 int main(int argc, char **argv)
39 {
40   printf("%s: Sorry, UCONFIG_NO_FORMATTING was turned on (see uconfig.h). No formatting can be done. \n", argv[0]);
41   return 0;
42 }
43 #else
44 
45 
46 /* Protos */
47 static void usage(void);
48 static void version(void);
49 static void date(const UChar *tz, UDateFormatStyle style, char *format, UErrorCode *status);
50 
51 
52 /* The version of date */
53 #define DATE_VERSION "1.0"
54 
55 /* "GMT" */
56 static const UChar GMT_ID [] = { 0x0047, 0x004d, 0x0054, 0x0000 };
57 
58 
59 int
main(int argc,char ** argv)60 main(int argc,
61      char **argv)
62 {
63   int printUsage = 0;
64   int printVersion = 0;
65   int optind = 1;
66   char *arg;
67   const UChar *tz = 0;
68   UDateFormatStyle style = UDAT_DEFAULT;
69   UErrorCode status = U_ZERO_ERROR;
70   char *format = NULL;
71 
72 
73   /* parse the options */
74   for(optind = 1; optind < argc; ++optind) {
75     arg = argv[optind];
76 
77     /* version info */
78     if(strcmp(arg, "-v") == 0 || strcmp(arg, "--version") == 0) {
79       printVersion = 1;
80     }
81     /* usage info */
82     else if(strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
83       printUsage = 1;
84     }
85     /* display date in gmt */
86     else if(strcmp(arg, "-u") == 0 || strcmp(arg, "--gmt") == 0) {
87       tz = GMT_ID;
88     }
89     /* display date in gmt */
90     else if(strcmp(arg, "-f") == 0 || strcmp(arg, "--full") == 0) {
91       style = UDAT_FULL;
92     }
93     /* display date in long format */
94     else if(strcmp(arg, "-l") == 0 || strcmp(arg, "--long") == 0) {
95       style = UDAT_LONG;
96     }
97     /* display date in medium format */
98     else if(strcmp(arg, "-m") == 0 || strcmp(arg, "--medium") == 0) {
99       style = UDAT_MEDIUM;
100     }
101     /* display date in short format */
102     else if(strcmp(arg, "-s") == 0 || strcmp(arg, "--short") == 0) {
103       style = UDAT_SHORT;
104     }
105     else if(strcmp(arg, "-F") == 0 || strcmp(arg, "--format") == 0) {
106       if ( optind + 1 < argc ) {
107          optind++;
108          format = argv[optind];
109       }
110     }
111     /* POSIX.1 says all arguments after -- are not options */
112     else if(strcmp(arg, "--") == 0) {
113       /* skip the -- */
114       ++optind;
115       break;
116     }
117     /* unrecognized option */
118     else if(strncmp(arg, "-", strlen("-")) == 0) {
119       printf("icudate: invalid option -- %s\n", arg+1);
120       printUsage = 1;
121     }
122     /* done with options, display date */
123     else {
124       break;
125     }
126   }
127 
128   /* print usage info */
129   if(printUsage) {
130     usage();
131     return 0;
132   }
133 
134   /* print version info */
135   if(printVersion) {
136     version();
137     return 0;
138   }
139 
140   /* print the date */
141   date(tz, style, format, &status);
142 
143   u_cleanup();
144   return (U_FAILURE(status) ? 1 : 0);
145 }
146 
147 /* Usage information */
148 static void
usage()149 usage()
150 {
151   puts("Usage: icudate [OPTIONS]");
152   puts("Options:");
153   puts("  -h, --help        Print this message and exit.");
154   puts("  -v, --version     Print the version number of date and exit.");
155   puts("  -u, --gmt         Display the date in Greenwich Mean Time.");
156   puts("  -f, --full        Use full display format.");
157   puts("  -l, --long        Use long display format.");
158   puts("  -m, --medium      Use medium display format.");
159   puts("  -s, --short       Use short display format.");
160 }
161 
162 /* Version information */
163 static void
version()164 version()
165 {
166   printf("icudate version %s (ICU version %s), created by Stephen F. Booth.\n",
167 	 DATE_VERSION, U_ICU_VERSION);
168   puts(U_COPYRIGHT_STRING);
169 }
170 
171 /* Format the date */
172 static void
date(const UChar * tz,UDateFormatStyle style,char * format,UErrorCode * status)173 date(const UChar *tz,
174      UDateFormatStyle style,
175      char *format,
176      UErrorCode *status)
177 {
178   UChar *s = 0;
179   int32_t len = 0;
180   UDateFormat *fmt;
181   UChar uFormat[100];
182 
183   fmt = udat_open(style, style, 0, tz, -1,NULL,0, status);
184   if ( format != NULL ) {
185      u_charsToUChars(format,uFormat,strlen(format)),
186      udat_applyPattern(fmt,false,uFormat,strlen(format));
187   }
188   len = udat_format(fmt, ucal_getNow(), 0, len, 0, status);
189   if(*status == U_BUFFER_OVERFLOW_ERROR) {
190     *status = U_ZERO_ERROR;
191     s = (UChar*) malloc(sizeof(UChar) * (len+1));
192     if(s == 0) goto finish;
193     udat_format(fmt, ucal_getNow(), s, len + 1, 0, status);
194     if(U_FAILURE(*status)) goto finish;
195   }
196 
197   /* print the date string */
198   uprint(s, stdout, status);
199 
200   /* print a trailing newline */
201   printf(" @ ICU " U_ICU_VERSION "\n");
202 
203  finish:
204   udat_close(fmt);
205   free(s);
206 }
207 #endif
208