1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2007-2007, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
13 **********************************************************************
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"
32 #define OFFSET_MONTH 1
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);
38 int main(int argc, char** argv) {
41 int year, month, day, hour, minute;
46 sysyear = year = month = day = 0;
49 fprintf(stderr, "Not enough arguments\n");
54 month = atoi(argv[2]);
57 minute = atoi(argv[5]);
58 useCurrentTime = atoi(argv[6]);
60 /* format year for system time */
61 sysyear = year - 1900;
63 systemtime = getSystemCurrentTime(systime, sysyear, month, day, hour, minute, useCurrentTime);
64 getICUCurrentTime(icutime, systemtime * U_MILLIS_PER_SECOND);
66 /* print out the times if failed */
67 if (strcmp(systime, icutime) != 0) {
69 printTime(systime, icutime);
75 void getICUCurrentTime(char* icutime, double timeToCheck) {
79 UDateFormatStyle style = UDAT_RELATIVE;
80 UErrorCode status = U_ZERO_ERROR;
84 fmt = udat_open(style, style, 0, tz, -1,NULL,0, &status);
86 len = udat_format(fmt, timeToCheck, 0, len, 0, &status);
88 if (status == U_BUFFER_OVERFLOW_ERROR)
89 status = U_ZERO_ERROR;
91 s = (UChar*) malloc(sizeof(UChar) * (len+1));
96 udat_format(fmt, timeToCheck, s, len + 1, 0, &status);
98 if (U_FAILURE(status))
101 /* +1 to NULL terminate */
102 for(i = 0; i < len+1; i++) {
103 icutime[i] = (char)s[i];
111 int64_t getSystemCurrentTime(char* systime, int year, int month, int day, int hour, int minute, int useCurrentTime) {
117 ts = *localtime(&now);
120 memset(&ts, 0, sizeof(ts));
122 ts.tm_mon = month - OFFSET_MONTH;
128 ts = *localtime(&now);
131 /* Format the string */
132 strftime(systime, sizeof(char) * 80, "%Y%m%d %I:%M %p", &ts);
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());