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