]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/date/date.c
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / samples / date / date.c
CommitLineData
b75a7d8f
A
1/*
2**********************************************************************
3* Copyright (C) 1998-2000, 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
31int main(int argc, char **argv);
32
33#if UCONFIG_NO_FORMATTING
34
35int 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 */
44static void usage(void);
45static void version(void);
46static void date(const UChar *tz, UDateFormatStyle style, UErrorCode *status);
47
48
49/* The version of date */
50#define DATE_VERSION "1.0"
51
52/* "GMT" */
53static const UChar GMT_ID [] = { 0x0047, 0x004d, 0x0054, 0x0000 };
54
55
56int
57main(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
68
69 /* parse the options */
70 for(optind = 1; optind < argc; ++optind) {
71 arg = argv[optind];
72
73 /* version info */
74 if(strcmp(arg, "-v") == 0 || strcmp(arg, "--version") == 0) {
75 printVersion = 1;
76 }
77 /* usage info */
78 else if(strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
79 printUsage = 1;
80 }
81 /* display date in gmt */
82 else if(strcmp(arg, "-u") == 0 || strcmp(arg, "--gmt") == 0) {
83 tz = GMT_ID;
84 }
85 /* display date in gmt */
86 else if(strcmp(arg, "-f") == 0 || strcmp(arg, "--full") == 0) {
87 style = UDAT_FULL;
88 }
89 /* display date in long format */
90 else if(strcmp(arg, "-l") == 0 || strcmp(arg, "--long") == 0) {
91 style = UDAT_LONG;
92 }
93 /* display date in medium format */
94 else if(strcmp(arg, "-m") == 0 || strcmp(arg, "--medium") == 0) {
95 style = UDAT_MEDIUM;
96 }
97 /* display date in short format */
98 else if(strcmp(arg, "-s") == 0 || strcmp(arg, "--short") == 0) {
99 style = UDAT_SHORT;
100 }
101 /* POSIX.1 says all arguments after -- are not options */
102 else if(strcmp(arg, "--") == 0) {
103 /* skip the -- */
104 ++optind;
105 break;
106 }
107 /* unrecognized option */
108 else if(strncmp(arg, "-", strlen("-")) == 0) {
109 printf("icudate: invalid option -- %s\n", arg+1);
110 printUsage = 1;
111 }
112 /* done with options, display date */
113 else {
114 break;
115 }
116 }
117
118 /* print usage info */
119 if(printUsage) {
120 usage();
121 return 0;
122 }
123
124 /* print version info */
125 if(printVersion) {
126 version();
127 return 0;
128 }
129
130 /* print the date */
131 date(tz, style, &status);
132
133 u_cleanup();
134 return (U_FAILURE(status) ? 1 : 0);
135}
136
137/* Usage information */
138static void
139usage()
140{
141 puts("Usage: icudate [OPTIONS]");
142 puts("Options:");
143 puts(" -h, --help Print this message and exit.");
144 puts(" -v, --version Print the version number of date and exit.");
145 puts(" -u, --gmt Display the date in Greenwich Mean Time.");
146 puts(" -f, --full Use full display format.");
147 puts(" -l, --long Use long display format.");
148 puts(" -m, --medium Use medium display format.");
149 puts(" -s, --short Use short display format.");
150}
151
152/* Version information */
153static void
154version()
155{
156 printf("icudate version %s (ICU version %s), created by Stephen F. Booth.\n",
157 DATE_VERSION, U_ICU_VERSION);
158 puts(U_COPYRIGHT_STRING);
159}
160
161/* Format the date */
162static void
163date(const UChar *tz,
164 UDateFormatStyle style,
165 UErrorCode *status)
166{
167 UChar *s = 0;
168 int32_t len = 0;
169 UDateFormat *fmt;
170
171 fmt = udat_open(style, style, 0, tz, -1,NULL,0, status);
172 len = udat_format(fmt, ucal_getNow(), 0, len, 0, status);
173 if(*status == U_BUFFER_OVERFLOW_ERROR) {
174 *status = U_ZERO_ERROR;
175 s = (UChar*) malloc(sizeof(UChar) * (len+1));
176 if(s == 0) goto finish;
177 udat_format(fmt, ucal_getNow(), s, len + 1, 0, status);
178 if(U_FAILURE(*status)) goto finish;
179 }
180
181 /* print the date string */
182 uprint(s, stdout, status);
183
184 /* print a trailing newline */
185 printf("\n");
186
187 finish:
188 udat_close(fmt);
189 free(s);
190}
191#endif