summaryrefslogtreecommitdiff
path: root/Build/source/libs/icu/icu-4.6/test/compat/tzdate.c
blob: 2615a7630adccfab866fa57bbd2df799cadd4b33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
**********************************************************************
*   Copyright (C) 2007-2007, International Business Machines
*   Corporation and others.  All Rights Reserved.
**********************************************************************
*
* File tzdate.c
*
* Author:  Michael Ow
*
**********************************************************************
*/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

#include "unicode/utypes.h"
#include "unicode/ustring.h"
#include "unicode/uclean.h"

#include "unicode/ucnv.h"
#include "unicode/udat.h"
#include "unicode/ucal.h"

#include "putilimp.h"

#define SIZE 80
#define OFFSET_MONTH 1

int64_t getSystemCurrentTime(char* systime, int year, int month, int day, int hour, int minute, int useCurrentTime);
void getICUCurrentTime(char* icutime, double timeToCheck);
void printTime(char* systime, char* icutime);

int main(int argc, char** argv) {
    char systime[SIZE];
    char icutime[SIZE];
    int year, month, day, hour, minute;
    int sysyear;
    int useCurrentTime;
    int64_t systemtime;
    
    sysyear = year = month = day = 0;
    
    if (argc <= 6) {
        fprintf(stderr, "Not enough arguments\n");
        return -1;
    }

    year = atoi(argv[1]);
    month = atoi(argv[2]);
    day = atoi(argv[3]);
    hour = atoi(argv[4]);
    minute = atoi(argv[5]);
    useCurrentTime = atoi(argv[6]);
    
    /* format year for system time */
    sysyear = year - 1900;
    
    systemtime = getSystemCurrentTime(systime, sysyear, month, day, hour, minute, useCurrentTime);
    getICUCurrentTime(icutime, systemtime * U_MILLIS_PER_SECOND);

    /* print out the times if failed */
    if (strcmp(systime, icutime) != 0) {
        printf("Failed\n");
        printTime(systime, icutime);
    }

    return 0;
}

void getICUCurrentTime(char* icutime, double timeToCheck) {
    UDateFormat *fmt;
    const UChar *tz = 0;
    UChar *s = 0;
    UDateFormatStyle style = UDAT_RELATIVE;
    UErrorCode status = U_ZERO_ERROR;
    int32_t len = 0;
    int i;

    fmt = udat_open(style, style, 0, tz, -1,NULL,0, &status);

    len = udat_format(fmt, timeToCheck, 0, len, 0, &status);

    if (status == U_BUFFER_OVERFLOW_ERROR)
        status = U_ZERO_ERROR;

    s = (UChar*) malloc(sizeof(UChar) * (len+1));

    if(s == 0) 
        goto finish;

    udat_format(fmt, timeToCheck, s, len + 1, 0, &status);
    
    if (U_FAILURE(status)) 
        goto finish;

    /* +1 to NULL terminate */
    for(i = 0; i < len+1; i++) {
        icutime[i] = (char)s[i];
    }

finish:
    udat_close(fmt);
    free(s);
}

int64_t getSystemCurrentTime(char* systime, int year, int month, int day, int hour, int minute, int useCurrentTime) {
    time_t now;
    struct tm ts;

    if (useCurrentTime){
        time(&now);
        ts = *localtime(&now);
    }
    else {
        memset(&ts, 0, sizeof(ts));
        ts.tm_year = year;
        ts.tm_mon = month - OFFSET_MONTH;
        ts.tm_mday = day;
        ts.tm_hour = hour;
        ts.tm_min = minute;

        now = mktime(&ts);
        ts = *localtime(&now);
    }

    /* Format the string */
    strftime(systime, sizeof(char) * 80, "%Y%m%d %I:%M %p", &ts);

    return (double)now;
}

void printTime(char* systime, char* icutime) {
    printf("System Time:  %s\n", systime);
    printf("ICU Time:     %s\n", icutime);
    printf("STD=%s DST=%s OFFSET=%d\n", uprv_tzname(0), uprv_tzname(1), uprv_timezone());
}