~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to wince/time_wce.c

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************
 
2
  time.c
 
3
 
 
4
  author : uema2
 
5
  date   : Nov 30, 2002
 
6
 
 
7
  You can freely use, copy, modify, and redistribute
 
8
  the whole contents.
 
9
***************************************************************/
 
10
 
 
11
/*#define __SCRATCH_TIMEC_DEBUG__ */
 
12
 
 
13
#include <windows.h>
 
14
#include <tchar.h>
 
15
#include <time.h>
 
16
 
 
17
/* globals */
 
18
const __int64 _onesec_in100ns = (__int64)10000000;
 
19
int   timezone, _timezone, altzone;
 
20
int   daylight;
 
21
char *tzname[2];
 
22
 
 
23
 
 
24
/* __int64 <--> FILETIME */
 
25
static __int64 wce_FILETIME2int64(FILETIME f)
 
26
{
 
27
        __int64 t;
 
28
 
 
29
        t = f.dwHighDateTime;
 
30
        t <<= 32;
 
31
        t |= f.dwLowDateTime;
 
32
        return t;
 
33
}
 
34
 
 
35
static FILETIME wce_int642FILETIME(__int64 t)
 
36
{
 
37
        FILETIME f;
 
38
 
 
39
        f.dwHighDateTime = (DWORD)((t >> 32) & 0x00000000FFFFFFFF);
 
40
        f.dwLowDateTime  = (DWORD)( t        & 0x00000000FFFFFFFF);
 
41
        return f;
 
42
}
 
43
 
 
44
/* FILETIME utility */
 
45
static FILETIME wce_getFILETIMEFromYear(WORD year)
 
46
{
 
47
        SYSTEMTIME s={0};
 
48
        FILETIME f;
 
49
 
 
50
        s.wYear      = year;
 
51
        s.wMonth     = 1;
 
52
        s.wDayOfWeek = 1;
 
53
        s.wDay       = 1;
 
54
 
 
55
        SystemTimeToFileTime( &s, &f );
 
56
        return f;
 
57
}
 
58
 
 
59
static time_t wce_getYdayFromSYSTEMTIME(const SYSTEMTIME* s)
 
60
{
 
61
        __int64 t;
 
62
        FILETIME f1, f2;
 
63
 
 
64
        f1 = wce_getFILETIMEFromYear( s->wYear );
 
65
        SystemTimeToFileTime( s, &f2 );
 
66
 
 
67
        t = wce_FILETIME2int64(f2)-wce_FILETIME2int64(f1);
 
68
 
 
69
        return (time_t)((t/_onesec_in100ns)/(60*60*24));
 
70
}
 
71
 
 
72
/* tm <--> SYSTEMTIME */
 
73
static SYSTEMTIME wce_tm2SYSTEMTIME(struct tm *t)
 
74
{
 
75
        SYSTEMTIME s;
 
76
 
 
77
        s.wYear      = t->tm_year + 1900;
 
78
        s.wMonth     = t->tm_mon  + 1;
 
79
        s.wDayOfWeek = t->tm_wday;
 
80
        s.wDay       = t->tm_mday;
 
81
        s.wHour      = t->tm_hour;
 
82
        s.wMinute    = t->tm_min;
 
83
        s.wSecond    = t->tm_sec;
 
84
        s.wMilliseconds = 0;
 
85
 
 
86
        return s;
 
87
}
 
88
 
 
89
static struct tm wce_SYSTEMTIME2tm(SYSTEMTIME *s)
 
90
{
 
91
        struct tm t;
 
92
 
 
93
        t.tm_year  = s->wYear - 1900;
 
94
        t.tm_mon   = s->wMonth- 1;
 
95
        t.tm_wday  = s->wDayOfWeek;
 
96
        t.tm_mday  = s->wDay;
 
97
        t.tm_yday  = wce_getYdayFromSYSTEMTIME(s);
 
98
        t.tm_hour  = s->wHour;
 
99
        t.tm_min   = s->wMinute;
 
100
        t.tm_sec   = s->wSecond;
 
101
        t.tm_isdst = 0;
 
102
 
 
103
        return t;
 
104
}
 
105
 
 
106
/* FILETIME <--> time_t */
 
107
time_t wce_FILETIME2time_t(const FILETIME* f)
 
108
{
 
109
        FILETIME f1601, f1970;
 
110
        __int64 t, offset;
 
111
 
 
112
        f1601 = wce_getFILETIMEFromYear(1601);
 
113
        f1970 = wce_getFILETIMEFromYear(1970);
 
114
 
 
115
        offset = wce_FILETIME2int64(f1970) - wce_FILETIME2int64(f1601);
 
116
 
 
117
        t = wce_FILETIME2int64(*f);
 
118
 
 
119
        t -= offset;
 
120
        return (time_t)(t / _onesec_in100ns);
 
121
}
 
122
 
 
123
FILETIME wce_time_t2FILETIME(const time_t t)
 
124
{
 
125
        FILETIME f, f1970;
 
126
        __int64 time;
 
127
 
 
128
        f1970 = wce_getFILETIMEFromYear(1970);
 
129
 
 
130
        time = t;
 
131
        time *= _onesec_in100ns;
 
132
        time += wce_FILETIME2int64(f1970);
 
133
 
 
134
        f = wce_int642FILETIME(time);
 
135
 
 
136
        return f;
 
137
}
 
138
 
 
139
/* time.h difinition */
 
140
time_t time( time_t *timer )
 
141
{
 
142
        SYSTEMTIME s;
 
143
        FILETIME   f;
 
144
 
 
145
        if( timer==NULL ) return 0;
 
146
 
 
147
        GetSystemTime( &s );
 
148
 
 
149
        SystemTimeToFileTime( &s, &f );
 
150
 
 
151
        *timer = wce_FILETIME2time_t(&f);
 
152
        return *timer;
 
153
}
 
154
 
 
155
struct tm *localtime( const time_t *timer )
 
156
{
 
157
        SYSTEMTIME ss, ls, s;
 
158
        FILETIME   sf, lf, f;
 
159
        __int64 t, diff;
 
160
        static struct tm tms;
 
161
 
 
162
        GetSystemTime(&ss);
 
163
        GetLocalTime(&ls);
 
164
 
 
165
        SystemTimeToFileTime( &ss, &sf );
 
166
        SystemTimeToFileTime( &ls, &lf );
 
167
 
 
168
        diff = wce_FILETIME2int64(sf) - wce_FILETIME2int64(lf);
 
169
 
 
170
        f = wce_time_t2FILETIME(*timer);
 
171
        t = wce_FILETIME2int64(f) - diff;
 
172
        f = wce_int642FILETIME(t);
 
173
 
 
174
        FileTimeToSystemTime( &f, &s );
 
175
 
 
176
        tms = wce_SYSTEMTIME2tm(&s);
 
177
 
 
178
        return &tms;
 
179
}
 
180
 
 
181
time_t mktime(struct tm* pt)
 
182
{
 
183
        SYSTEMTIME ss, ls, s;
 
184
        FILETIME   sf, lf, f;
 
185
        __int64 diff;
 
186
 
 
187
        GetSystemTime(&ss);
 
188
        GetLocalTime(&ls);
 
189
        SystemTimeToFileTime( &ss, &sf );
 
190
        SystemTimeToFileTime( &ls, &lf );
 
191
 
 
192
        diff = (wce_FILETIME2int64(lf)-wce_FILETIME2int64(sf))/_onesec_in100ns;
 
193
 
 
194
        s = wce_tm2SYSTEMTIME(pt);
 
195
        SystemTimeToFileTime( &s, &f );
 
196
        return wce_FILETIME2time_t(&f) - (time_t)diff;
 
197
}
 
198
 
 
199
struct tm *gmtime(const time_t *t)
 
200
{
 
201
        FILETIME f;
 
202
        SYSTEMTIME s;
 
203
        static struct tm tms;
 
204
        
 
205
        f = wce_time_t2FILETIME(*t);
 
206
        FileTimeToSystemTime(&f, &s);
 
207
        tms = wce_SYSTEMTIME2tm(&s);
 
208
        return &tms;
 
209
}
 
210
 
 
211
char* ctime( const time_t *t )
 
212
{
 
213
        // Wed Jan 02 02:03:55 1980\n\0
 
214
        static char buf[30]={0};
 
215
        char week[] = "Sun Mon Tue Wed Thr Fri Sat ";
 
216
        char month[]= "Jan Feb Mar Apl May Jun Jul Aug Sep Oct Nov Dec ";
 
217
        struct tm tms;
 
218
 
 
219
        tms = *localtime(t);
 
220
 
 
221
        strncpy( buf,    week+tms.tm_wday*4, 4 );
 
222
        strncpy( buf+4,  month+tms.tm_mon*4, 4 );
 
223
        sprintf( buf+8,  "%02d ", tms.tm_mday );
 
224
        sprintf( buf+11, "%02d:%02d:%02d %d\n", 
 
225
                tms.tm_hour, tms.tm_min, tms.tm_sec, tms.tm_year+1900 );
 
226
        return buf;
 
227
}
 
228
 
 
229
char *asctime(const struct tm *pt)
 
230
{
 
231
        static char buf[30]={0};
 
232
        char week[] = "Sun Mon Tue Wed Thr Fri Sat ";
 
233
        char month[]= "Jan Feb Mar Apl May Jun Jul Aug Sep Oct Nov Dec ";
 
234
 
 
235
        strncpy( buf,    week+pt->tm_wday*4, 4 );
 
236
        strncpy( buf+4,  month+pt->tm_mon*4, 4 );
 
237
        sprintf( buf+8,  "%02d ", pt->tm_mday );
 
238
        sprintf( buf+11, "%02d:%02d:%02d %d\n", 
 
239
                pt->tm_hour, pt->tm_min, pt->tm_sec, pt->tm_year+1900 );
 
240
        return buf;
 
241
}
 
242
 
 
243
void tzset()
 
244
{
 
245
        daylight = 1;
 
246
        _timezone = 28800;
 
247
        timezone = 28800;
 
248
}
 
249
 
 
250
int clock(void)
 
251
{
 
252
        return 1;
 
253
}
 
254
 
 
255
//---------------------------------------------------------------
 
256
#ifdef __SCRATCH_TIMEC_DEBUG__
 
257
 
 
258
int main()
 
259
{
 
260
        time_t t1, t2;
 
261
        struct tm tm1, tm2;
 
262
 
 
263
        time( &t1 );
 
264
        tm1 = *localtime(&t1);
 
265
        t1 = mktime(&tm1);
 
266
        tm1 = *gmtime(&t1);
 
267
 
 
268
        _time( &t2 );
 
269
        tm2 = *_localtime(&t2);
 
270
        t2 = _mktime(&tm2);
 
271
        tm2 = *_gmtime(&t2);
 
272
 
 
273
        // time, mktime
 
274
        if( t1==t2 )
 
275
                OutputDebugString( "ok\n" );
 
276
        else
 
277
        {
 
278
                static char buf[128];
 
279
                wsprintf( buf, "ng : %d, %d\n", t1, t2 );
 
280
                OutputDebugString( buf );
 
281
        }
 
282
 
 
283
        // localtime, gmtime
 
284
        if( 0==memcmp( &tm1, &tm2, sizeof(struct tm) ) )
 
285
                OutputDebugString( "ok\n" );
 
286
        else
 
287
                OutputDebugString( "ng\n" );
 
288
 
 
289
        // ctime
 
290
        OutputDebugString( ctime(&t1) );
 
291
        OutputDebugString( _ctime(&t2) );
 
292
 
 
293
        // asctime
 
294
        OutputDebugString( asctime(&tm1) );
 
295
        OutputDebugString( _asctime(&tm2) );
 
296
 
 
297
        return 0;
 
298
}
 
299
 
 
300
#endif
 
301