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