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