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