]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/date/date.c
2 **********************************************************************
3 * Copyright (C) 1998-2007, 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
, char *format
, 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
;
70 /* parse the options */
71 for(optind
= 1; optind
< argc
; ++optind
) {
75 if(strcmp(arg
, "-v") == 0 || strcmp(arg
, "--version") == 0) {
79 else if(strcmp(arg
, "-h") == 0 || strcmp(arg
, "--help") == 0) {
82 /* display date in gmt */
83 else if(strcmp(arg
, "-u") == 0 || strcmp(arg
, "--gmt") == 0) {
86 /* display date in gmt */
87 else if(strcmp(arg
, "-f") == 0 || strcmp(arg
, "--full") == 0) {
90 /* display date in long format */
91 else if(strcmp(arg
, "-l") == 0 || strcmp(arg
, "--long") == 0) {
94 /* display date in medium format */
95 else if(strcmp(arg
, "-m") == 0 || strcmp(arg
, "--medium") == 0) {
98 /* display date in short format */
99 else if(strcmp(arg
, "-s") == 0 || strcmp(arg
, "--short") == 0) {
102 else if(strcmp(arg
, "-F") == 0 || strcmp(arg
, "--format") == 0) {
103 if ( optind
+ 1 < argc
) {
105 format
= argv
[optind
];
108 /* POSIX.1 says all arguments after -- are not options */
109 else if(strcmp(arg
, "--") == 0) {
114 /* unrecognized option */
115 else if(strncmp(arg
, "-", strlen("-")) == 0) {
116 printf("icudate: invalid option -- %s\n", arg
+1);
119 /* done with options, display date */
125 /* print usage info */
131 /* print version info */
138 date(tz
, style
, format
, &status
);
141 return (U_FAILURE(status
) ? 1 : 0);
144 /* Usage information */
148 puts("Usage: icudate [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.");
159 /* Version information */
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
);
168 /* Format the date */
170 date(const UChar
*tz
,
171 UDateFormatStyle style
,
180 fmt
= udat_open(style
, style
, 0, tz
, -1,NULL
,0, status
);
181 if ( format
!= NULL
) {
182 u_charsToUChars(format
,uFormat
,strlen(format
)),
183 udat_applyPattern(fmt
,FALSE
,uFormat
,strlen(format
));
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
;
194 /* print the date string */
195 uprint(s
, stdout
, status
);
197 /* print a trailing newline */