]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/date/date.c
2 **********************************************************************
3 * Copyright (C) 1998-2000, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
9 * Modification History:
11 * Date Name Description
12 * 06/11/99 stephen Creation.
13 * 06/16/99 stephen Modified to use uprint.
14 *******************************************************************************
21 #include "unicode/utypes.h"
22 #include "unicode/ustring.h"
23 #include "unicode/uclean.h"
25 #include "unicode/ucnv.h"
26 #include "unicode/udat.h"
27 #include "unicode/ucal.h"
31 int main(int argc
, char **argv
);
33 #if UCONFIG_NO_FORMATTING
35 int main(int argc
, char **argv
)
37 printf("%s: Sorry, UCONFIG_NO_FORMATTING was turned on (see uconfig.h). No formatting can be done. \n", argv
[0]);
44 static void usage(void);
45 static void version(void);
46 static void date(const UChar
*tz
, UDateFormatStyle style
, UErrorCode
*status
);
49 /* The version of date */
50 #define DATE_VERSION "1.0"
53 static const UChar GMT_ID
[] = { 0x0047, 0x004d, 0x0054, 0x0000 };
65 UDateFormatStyle style
= UDAT_DEFAULT
;
66 UErrorCode status
= U_ZERO_ERROR
;
69 /* parse the options */
70 for(optind
= 1; optind
< argc
; ++optind
) {
74 if(strcmp(arg
, "-v") == 0 || strcmp(arg
, "--version") == 0) {
78 else if(strcmp(arg
, "-h") == 0 || strcmp(arg
, "--help") == 0) {
81 /* display date in gmt */
82 else if(strcmp(arg
, "-u") == 0 || strcmp(arg
, "--gmt") == 0) {
85 /* display date in gmt */
86 else if(strcmp(arg
, "-f") == 0 || strcmp(arg
, "--full") == 0) {
89 /* display date in long format */
90 else if(strcmp(arg
, "-l") == 0 || strcmp(arg
, "--long") == 0) {
93 /* display date in medium format */
94 else if(strcmp(arg
, "-m") == 0 || strcmp(arg
, "--medium") == 0) {
97 /* display date in short format */
98 else if(strcmp(arg
, "-s") == 0 || strcmp(arg
, "--short") == 0) {
101 /* POSIX.1 says all arguments after -- are not options */
102 else if(strcmp(arg
, "--") == 0) {
107 /* unrecognized option */
108 else if(strncmp(arg
, "-", strlen("-")) == 0) {
109 printf("icudate: invalid option -- %s\n", arg
+1);
112 /* done with options, display date */
118 /* print usage info */
124 /* print version info */
131 date(tz
, style
, &status
);
134 return (U_FAILURE(status
) ? 1 : 0);
137 /* Usage information */
141 puts("Usage: icudate [OPTIONS]");
143 puts(" -h, --help Print this message and exit.");
144 puts(" -v, --version Print the version number of date and exit.");
145 puts(" -u, --gmt Display the date in Greenwich Mean Time.");
146 puts(" -f, --full Use full display format.");
147 puts(" -l, --long Use long display format.");
148 puts(" -m, --medium Use medium display format.");
149 puts(" -s, --short Use short display format.");
152 /* Version information */
156 printf("icudate version %s (ICU version %s), created by Stephen F. Booth.\n",
157 DATE_VERSION
, U_ICU_VERSION
);
158 puts(U_COPYRIGHT_STRING
);
161 /* Format the date */
163 date(const UChar
*tz
,
164 UDateFormatStyle style
,
171 fmt
= udat_open(style
, style
, 0, tz
, -1,NULL
,0, status
);
172 len
= udat_format(fmt
, ucal_getNow(), 0, len
, 0, status
);
173 if(*status
== U_BUFFER_OVERFLOW_ERROR
) {
174 *status
= U_ZERO_ERROR
;
175 s
= (UChar
*) malloc(sizeof(UChar
) * (len
+1));
176 if(s
== 0) goto finish
;
177 udat_format(fmt
, ucal_getNow(), s
, len
+ 1, 0, status
);
178 if(U_FAILURE(*status
)) goto finish
;
181 /* print the date string */
182 uprint(s
, stdout
, status
);
184 /* print a trailing newline */