]> git.saurik.com Git - apple/icu.git/blob - icuSources/samples/date/date.c
ICU-461.17.tar.gz
[apple/icu.git] / icuSources / samples / date / date.c
1 /*
2 **********************************************************************
3 * Copyright (C) 1998-2008, International Business Machines
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);
46 static void date(const UChar *tz, UDateFormatStyle style, char *format, UErrorCode *status);
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;
67 char *format = NULL;
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 }
102 else if(strcmp(arg, "-F") == 0 || strcmp(arg, "--format") == 0) {
103 if ( optind + 1 < argc ) {
104 optind++;
105 format = argv[optind];
106 }
107 }
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 */
138 date(tz, style, format, &status);
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 UErrorCode status = U_ZERO_ERROR;
164 const char *tzVer;
165 int len = 256;
166 UChar tzName[256];
167 printf("icudate version %s, created by Stephen F. Booth.\n",
168 DATE_VERSION);
169 puts(U_COPYRIGHT_STRING);
170 tzVer = ucal_getTZDataVersion(&status);
171 if(U_FAILURE(status)) {
172 tzVer = u_errorName(status);
173 }
174 printf("\n");
175 printf("ICU Version: %s\n", U_ICU_VERSION);
176 printf("ICU Data (major+min): %s\n", U_ICUDATA_NAME);
177 printf("Default Locale: %s\n", uloc_getDefault());
178 printf("Time Zone Data Version: %s\n", tzVer);
179 printf("Default Time Zone: ");
180 status = U_ZERO_ERROR;
181 u_init(&status);
182 len = ucal_getDefaultTimeZone(tzName, len, &status);
183 if(U_FAILURE(status)) {
184 printf(" ** Error getting default zone: %s\n", u_errorName(status));
185 }
186 uprint(tzName, stdout, &status);
187 printf("\n\n");
188 }
189
190 /* Format the date */
191 static void
192 date(const UChar *tz,
193 UDateFormatStyle style,
194 char *format,
195 UErrorCode *status)
196 {
197 UChar *s = 0;
198 int32_t len = 0;
199 UDateFormat *fmt;
200 UChar uFormat[100];
201
202 fmt = udat_open(style, style, 0, tz, -1,NULL,0, status);
203 if ( format != NULL ) {
204 u_charsToUChars(format,uFormat,strlen(format)),
205 udat_applyPattern(fmt,FALSE,uFormat,strlen(format));
206 }
207 len = udat_format(fmt, ucal_getNow(), 0, len, 0, status);
208 if(*status == U_BUFFER_OVERFLOW_ERROR) {
209 *status = U_ZERO_ERROR;
210 s = (UChar*) malloc(sizeof(UChar) * (len+1));
211 if(s == 0) goto finish;
212 udat_format(fmt, ucal_getNow(), s, len + 1, 0, status);
213 if(U_FAILURE(*status)) goto finish;
214 }
215
216 /* print the date string */
217 uprint(s, stdout, status);
218
219 /* print a trailing newline */
220 printf("\n");
221
222 finish:
223 udat_close(fmt);
224 free(s);
225 }
226 #endif