]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/date/date.c
2 *************************************************************************
3 * © 2016 and later: Unicode, Inc. and others.
4 * License & terms of use: http://www.unicode.org/copyright.html#License
5 *************************************************************************
6 ***********************************************************************
7 * Copyright (C) 1998-2012, International Business Machines
8 * Corporation and others. All Rights Reserved.
9 **********************************************************************
13 * Modification History:
15 * Date Name Description
16 * 06/11/99 stephen Creation.
17 * 06/16/99 stephen Modified to use uprint.
18 * 08/11/11 srl added Parse and milli/second in/out
19 *******************************************************************************
26 #include "unicode/utypes.h"
27 #include "unicode/ustring.h"
28 #include "unicode/uclean.h"
30 #include "unicode/ucnv.h"
31 #include "unicode/udat.h"
32 #include "unicode/ucal.h"
36 int main(int argc
, char **argv
);
38 #if UCONFIG_NO_FORMATTING || UCONFIG_NO_CONVERSION
40 int main(int argc
, char **argv
)
42 printf("%s: Sorry, UCONFIG_NO_FORMATTING or UCONFIG_NO_CONVERSION was turned on (see uconfig.h). No formatting can be done. \n", argv
[0]);
49 static void usage(void);
50 static void version(void);
51 static void date(UDate when
, const UChar
*tz
, UDateFormatStyle style
, const char *format
, UErrorCode
*status
);
52 static UDate
getWhen(const char *millis
, const char *seconds
, const char *format
, UDateFormatStyle style
, const char *parse
, const UChar
*tz
, UErrorCode
*status
);
54 UConverter
*cnv
= NULL
;
56 /* The version of date */
57 #define DATE_VERSION "1.0"
60 static const UChar GMT_ID
[] = { 0x0047, 0x004d, 0x0054, 0x0000 };
62 #define FORMAT_MILLIS "%"
63 #define FORMAT_SECONDS "%%"
74 UDateFormatStyle style
= UDAT_DEFAULT
;
75 UErrorCode status
= U_ZERO_ERROR
;
76 const char *format
= NULL
;
82 /* parse the options */
83 for(optInd
= 1; optInd
< argc
; ++optInd
) {
87 if(strcmp(arg
, "-v") == 0 || strcmp(arg
, "--version") == 0) {
91 else if(strcmp(arg
, "-h") == 0 || strcmp(arg
, "--help") == 0) {
94 /* display date in gmt */
95 else if(strcmp(arg
, "-u") == 0 || strcmp(arg
, "--gmt") == 0) {
98 /* display date in gmt */
99 else if(strcmp(arg
, "-f") == 0 || strcmp(arg
, "--full") == 0) {
102 /* display date in long format */
103 else if(strcmp(arg
, "-l") == 0 || strcmp(arg
, "--long") == 0) {
106 /* display date in medium format */
107 else if(strcmp(arg
, "-m") == 0 || strcmp(arg
, "--medium") == 0) {
110 /* display date in short format */
111 else if(strcmp(arg
, "-s") == 0 || strcmp(arg
, "--short") == 0) {
114 else if(strcmp(arg
, "-F") == 0 || strcmp(arg
, "--format") == 0) {
115 if ( optInd
+ 1 < argc
) {
117 format
= argv
[optInd
];
119 } else if(strcmp(arg
, "-r") == 0) {
120 if ( optInd
+ 1 < argc
) {
122 seconds
= argv
[optInd
];
124 } else if(strcmp(arg
, "-R") == 0) {
125 if ( optInd
+ 1 < argc
) {
127 millis
= argv
[optInd
];
129 } else if(strcmp(arg
, "-P") == 0) {
130 if ( optInd
+ 1 < argc
) {
132 parse
= argv
[optInd
];
135 /* POSIX.1 says all arguments after -- are not options */
136 else if(strcmp(arg
, "--") == 0) {
141 /* unrecognized option */
142 else if(strncmp(arg
, "-", strlen("-")) == 0) {
143 printf("icudate: invalid option -- %s\n", arg
+1);
146 /* done with options, display date */
152 /* print usage info */
158 /* print version info */
164 /* get the 'when' (or now) */
165 when
= getWhen(millis
, seconds
, format
, style
, parse
, tz
, &status
);
167 format
= FORMAT_MILLIS
; /* output in millis */
171 date(when
, tz
, style
, format
, &status
);
176 return (U_FAILURE(status
) ? 1 : 0);
179 /* Usage information */
183 puts("Usage: icudate [OPTIONS]");
185 puts(" -h, --help Print this message and exit.");
186 puts(" -v, --version Print the version number of date and exit.");
187 puts(" -u, --gmt Display the date in Greenwich Mean Time.");
188 puts(" -f, --full Use full display format.");
189 puts(" -l, --long Use long display format.");
190 puts(" -m, --medium Use medium display format.");
191 puts(" -s, --short Use short display format.");
192 puts(" -F <format>, --format <format> Use <format> as the display format.");
193 puts(" (Special formats: \"%\" alone is Millis since 1970, \"%%\" alone is Seconds since 1970)");
194 puts(" -r <seconds> Use <seconds> as the time (Epoch 1970) rather than now.");
195 puts(" -R <millis> Use <millis> as the time (Epoch 1970) rather than now.");
196 puts(" -P <string> Parse <string> as the time, output in millis format.");
199 /* Version information */
203 UErrorCode status
= U_ZERO_ERROR
;
207 printf("icudate version %s, created by Stephen F. Booth.\n",
209 puts(U_COPYRIGHT_STRING
);
210 tzVer
= ucal_getTZDataVersion(&status
);
211 if(U_FAILURE(status
)) {
212 tzVer
= u_errorName(status
);
215 printf("ICU Version: %s\n", U_ICU_VERSION
);
216 printf("ICU Data (major+min): %s\n", U_ICUDATA_NAME
);
217 printf("Default Locale: %s\n", uloc_getDefault());
218 printf("Time Zone Data Version: %s\n", tzVer
);
219 printf("Default Time Zone: ");
220 status
= U_ZERO_ERROR
;
222 len
= ucal_getDefaultTimeZone(tzName
, len
, &status
);
223 if(U_FAILURE(status
)) {
224 fprintf(stderr
, " ** Error getting default zone: %s\n", u_errorName(status
));
226 uprint(tzName
, stdout
, &status
);
230 static int32_t charsToUCharsDefault(UChar
*uchars
, int32_t ucharsSize
, const char*chars
, int32_t charsSize
, UErrorCode
*status
) {
232 if(U_FAILURE(*status
)) return len
;
234 cnv
= ucnv_open(NULL
, status
);
236 if(cnv
&&U_SUCCESS(*status
)) {
237 len
= ucnv_toUChars(cnv
, uchars
, ucharsSize
, chars
,charsSize
, status
);
242 /* Format the date */
246 UDateFormatStyle style
,
255 if(U_FAILURE(*status
)) return;
257 if( format
!= NULL
) {
258 if(!strcmp(format
,FORMAT_MILLIS
)) {
259 printf("%.0f\n", when
);
261 } else if(!strcmp(format
, FORMAT_SECONDS
)) {
262 printf("%.3f\n", when
/1000.0);
267 fmt
= udat_open(style
, style
, 0, tz
, -1,NULL
,0, status
);
268 if ( format
!= NULL
) {
269 charsToUCharsDefault(uFormat
,sizeof(uFormat
)/sizeof(uFormat
[0]),format
,-1,status
);
270 udat_applyPattern(fmt
,FALSE
,uFormat
,-1);
272 len
= udat_format(fmt
, when
, 0, len
, 0, status
);
273 if(*status
== U_BUFFER_OVERFLOW_ERROR
) {
274 *status
= U_ZERO_ERROR
;
275 s
= (UChar
*) malloc(sizeof(UChar
) * (len
+1));
276 if(s
== 0) goto finish
;
277 udat_format(fmt
, when
, s
, len
+ 1, 0, status
);
279 if(U_FAILURE(*status
)) goto finish
;
281 /* print the date string */
282 uprint(s
, stdout
, status
);
284 /* print a trailing newline */
288 if(U_FAILURE(*status
)) {
289 fprintf(stderr
, "Error in Print: %s\n", u_errorName(*status
));
295 static UDate
getWhen(const char *millis
, const char *seconds
, const char *format
,
296 UDateFormatStyle style
, const char *parse
, const UChar
*tz
, UErrorCode
*status
) {
297 UDateFormat
*fmt
= NULL
;
301 int32_t parsepos
= 0;
304 sscanf(millis
, "%lf", &when
);
306 } else if(seconds
!= NULL
) {
307 sscanf(seconds
, "%lf", &when
);
312 if( format
!= NULL
) {
313 if(!strcmp(format
,FORMAT_MILLIS
)) {
314 sscanf(parse
, "%lf", &when
);
316 } else if(!strcmp(format
, FORMAT_SECONDS
)) {
317 sscanf(parse
, "%lf", &when
);
322 fmt
= udat_open(style
, style
, 0, tz
, -1,NULL
,0, status
);
323 if ( format
!= NULL
) {
324 charsToUCharsDefault(uFormat
,sizeof(uFormat
)/sizeof(uFormat
[0]), format
,-1,status
);
325 udat_applyPattern(fmt
,FALSE
,uFormat
,-1);
328 charsToUCharsDefault(uParse
,sizeof(uParse
)/sizeof(uParse
[0]), parse
,-1,status
);
329 when
= udat_parse(fmt
, uParse
, -1, &parsepos
, status
);
330 if(U_FAILURE(*status
)) {
331 fprintf(stderr
, "Error in Parse: %s\n", u_errorName(*status
));
332 if(parsepos
> 0 && parsepos
<= (int32_t)strlen(parse
)) {
333 fprintf(stderr
, "ERR>\"%s\" @%d\n"
335 parse
,parsepos
,parsepos
,"");
343 return ucal_getNow();