1 /*
2 **********************************************************************
3 * Copyright (C) 2007-2007, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 *
7 * File tzdate.c
8 *
9 * Author: Michael Ow
10 *
11 **********************************************************************
12 */
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <time.h>
17 #include <string.h>
18
19 #include "unicode/utypes.h"
20 #include "unicode/ustring.h"
21 #include "unicode/uclean.h"
22
23 #include "unicode/ucnv.h"
24 #include "unicode/udat.h"
25 #include "unicode/ucal.h"
26
27 #include "putilimp.h"
28
29 #define SIZE 80
30 #define OFFSET_MONTH 1
31
32 int64_t getSystemCurrentTime(char* systime, int year, int month, int day, int hour, int minute, int useCurrentTime);
33 void getICUCurrentTime(char* icutime, double timeToCheck);
34 void printTime(char* systime, char* icutime);
35
main(int argc,char ** argv)36 int main(int argc, char** argv) {
37 char systime[SIZE];
38 char icutime[SIZE];
39 int year, month, day, hour, minute;
40 int sysyear;
41 int useCurrentTime;
42 int64_t systemtime;
43
44 sysyear = year = month = day = 0;
45
46 if (argc <= 6) {
47 fprintf(stderr, "Not enough arguments\n");
48 return -1;
49 }
50
51 year = atoi(argv[1]);
52 month = atoi(argv[2]);
53 day = atoi(argv[3]);
54 hour = atoi(argv[4]);
55 minute = atoi(argv[5]);
56 useCurrentTime = atoi(argv[6]);
57
58 /* format year for system time */
59 sysyear = year - 1900;
60
61 systemtime = getSystemCurrentTime(systime, sysyear, month, day, hour, minute, useCurrentTime);
62 getICUCurrentTime(icutime, systemtime * U_MILLIS_PER_SECOND);
63
64 /* print out the times if failed */
65 if (strcmp(systime, icutime) != 0) {
66 printf("Failed\n");
67 printTime(systime, icutime);
68 }
69
70 return 0;
71 }
72
getICUCurrentTime(char * icutime,double timeToCheck)73 void getICUCurrentTime(char* icutime, double timeToCheck) {
74 UDateFormat *fmt;
75 const UChar *tz = 0;
76 UChar *s = 0;
77 UDateFormatStyle style = UDAT_RELATIVE;
78 UErrorCode status = U_ZERO_ERROR;
79 int32_t len = 0;
80 int i;
81
82 fmt = udat_open(style, style, 0, tz, -1,NULL,0, &status);
83
84 len = udat_format(fmt, timeToCheck, 0, len, 0, &status);
85
86 if (status == U_BUFFER_OVERFLOW_ERROR)
87 status = U_ZERO_ERROR;
88
89 s = (UChar*) malloc(sizeof(UChar) * (len+1));
90
91 if(s == 0)
92 goto finish;
93
94 udat_format(fmt, timeToCheck, s, len + 1, 0, &status);
95
96 if (U_FAILURE(status))
97 goto finish;
98
99 /* +1 to NULL terminate */
100 for(i = 0; i < len+1; i++) {
101 icutime[i] = (char)s[i];
102 }
103
104 finish:
105 udat_close(fmt);
106 free(s);
107 }
108
getSystemCurrentTime(char * systime,int year,int month,int day,int hour,int minute,int useCurrentTime)109 int64_t getSystemCurrentTime(char* systime, int year, int month, int day, int hour, int minute, int useCurrentTime) {
110 time_t now;
111 struct tm ts;
112
113 if (useCurrentTime){
114 time(&now);
115 ts = *localtime(&now);
116 }
117 else {
118 memset(&ts, 0, sizeof(ts));
119 ts.tm_year = year;
120 ts.tm_mon = month - OFFSET_MONTH;
121 ts.tm_mday = day;
122 ts.tm_hour = hour;
123 ts.tm_min = minute;
124
125 now = mktime(&ts);
126 ts = *localtime(&now);
127 }
128
129 /* Format the string */
130 strftime(systime, sizeof(char) * 80, "%Y%m%d %I:%M %p", &ts);
131
132 return (double)now;
133 }
134
printTime(char * systime,char * icutime)135 void printTime(char* systime, char* icutime) {
136 printf("System Time: %s\n", systime);
137 printf("ICU Time: %s\n", icutime);
138 printf("STD=%s DST=%s OFFSET=%d\n", uprv_tzname(0), uprv_tzname(1), uprv_timezone());
139 }
140
141