File: timezone.c

package info (click to toggle)
librra 0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,272 kB
  • ctags: 1,427
  • sloc: ansic: 12,769; sh: 10,505; makefile: 179
file content (401 lines) | stat: -rw-r--r-- 10,290 bytes parent folder | download | duplicates (3)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* $Id: timezone.c 2335 2006-03-27 18:44:22Z twogood $ */
#define _BSD_SOURCE 1
#include "timezone.h"
#include "generator.h"
#include <rapi.h>
#include <synce.h>
#include <synce_log.h>
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define REGISTRY_KEY_NAME     "Time"
#define REGISTRY_VALUE_NAME   "TimeZoneInformation"

#define LETOH16(x)  x = letoh16(x)
#define LETOH32(x)  x = letoh32(x)

static const uint8_t empty[6] = {0,0,0,0,0,0};

/*
   See this page for info:

   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcemain4/html/_wcepb_time_zone_registry_settings.asp
 */
static const uint8_t default_timezone[172]={
  0xe0,0x01,0x00,0x00,0x4d,0x00,0x54,0x00,
  0x2d,0x00,0x38,0x00,0x20,0x00,0x50,0x00,
  0x61,0x00,0x63,0x00,0x69,0x00,0x66,0x00,
  0x69,0x00,0x63,0x00,0x20,0x00,0x55,0x00,
  0x53,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x0a,0x00,
  0x00,0x00,0x05,0x00,0x02,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x54,0x00,0x2d,0x00,0x38,0x00,0x20,0x00,
  0x50,0x00,0x61,0x00,0x63,0x00,0x69,0x00,
  0x66,0x00,0x69,0x00,0x63,0x00,0x20,0x00,
  0x55,0x00,0x53,0x00,0x22,0x00,0x50,0x00,
  0x61,0x00,0x63,0x00,0x69,0x00,0x66,0x00,
  0x69,0x00,0x63,0x00,0x20,0x00,0x53,0x00,
  0x74,0x00,0x61,0x00,0x6e,0x00,0x64,0x00,
  0x61,0x00,0x72,0x00,0x64,0x00,0x00,0x00,
  0x00,0x00,0x04,0x00,0x00,0x00,0x01,0x00,
  0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0xc4,0xff,0xff,0xff};

bool rra_timezone_get(RRA_Timezone* tzi)/*{{{*/
{
  bool success = false;
  LONG error;
  HKEY key = 0;
  WCHAR* wide_value_name = NULL;
  DWORD size = sizeof(RRA_Timezone);
  bool registry_success = false;
  
  assert(sizeof(RRA_Timezone) == 172);

  if (!tzi)
  {
    synce_error("Invalid parameter");
    goto exit;
  }

  if (rapi_reg_open_key(HKEY_LOCAL_MACHINE, REGISTRY_KEY_NAME, &key))
  {
    wide_value_name = wstr_from_ascii(REGISTRY_VALUE_NAME);

    error = CeRegQueryValueEx(key, wide_value_name, NULL, NULL, (void*)tzi, &size);
    if (ERROR_SUCCESS == error)
    {
      if (sizeof(RRA_Timezone) == size)
        registry_success = true;
      else
      {
        synce_warning("Expected value size %i but got %i", sizeof(RRA_Timezone), size);
        goto exit;
      }
    }
    else
      synce_warning("Failed to get registry value: %s", synce_strerror(error));
  }
  else
    synce_warning("Failed to open registry key 'HKEY_LOCAL_MACHINE\\%s'", 
        REGISTRY_KEY_NAME);

  if (!registry_success)
  {
    synce_warning("Fallback to default time zone enabled");
    memcpy(tzi, default_timezone, sizeof(RRA_Timezone));
  }

  LETOH32(tzi->Bias);
  
  LETOH16(tzi->StandardMonthOfYear);
  LETOH16(tzi->StandardInstance);
  LETOH16(tzi->StandardStartHour);
  LETOH32(tzi->StandardBias);
  
  LETOH16(tzi->DaylightMonthOfYear);
  LETOH16(tzi->DaylightInstance);
  LETOH16(tzi->DaylightStartHour);
  LETOH32(tzi->DaylightBias);

  if (tzi->unknown0 || tzi->unknown1 || tzi->unknown3 || tzi->unknown4 ||
      0 != memcmp(tzi->unknown2, empty, sizeof(tzi->unknown2)) || 
      0 != memcmp(tzi->unknown5, empty, sizeof(tzi->unknown5)))
  {
    synce_warning("Unknown value used in time zone information");
  }

  success = true;
  
exit:
  if (key)
    CeRegCloseKey(key);
  wstr_free_string(wide_value_name);
  return success;
}/*}}}*/

/* XXX: improve ID generation */
void rra_timezone_create_id(RRA_Timezone* tzi, char** id)/*{{{*/
{
  char* name = wstr_to_ascii(tzi->StandardName);
  char* p;
  char buffer[128];

  if (!id)
    return;

  for (p = name; *p != '\0'; p++)
  {
    if (!isalnum(*p))
      *p = '_';
  }

  snprintf(buffer, sizeof(buffer), "/synce.sourceforge.net/SynCE/%s", name);
  
  *id = strdup(buffer);

  wstr_free_string(name);
}/*}}}*/

static const unsigned days_of_month[12] =/*{{{*/
{
  31,  /* jan */
  28,
  31,  /* mar */
  30,
  31,  /* may */
  30,
  31,  /* jul */
  31,
  30,  /* sep */
  31,
  30,  /* nov */
  31
};/*}}}*/

/* http://www.visi.com/~pmk/new-dayofweek.html */
static const unsigned month_skew[12] =/*{{{*/
{
  0,  /* jan */
  3,
  3,  /* mar */
  6,
  1,  /* may */
  4,
  6,  /* jul */
  2,
  5,  /* sep */
  0,
  3,  /* nov */
  5
};/*}}}*/

static unsigned day_from_month_and_week(unsigned month, unsigned week)/*{{{*/
{
  /* don't ask... */
  unsigned first_sunday = (8 - ((4 + month_skew[month-1]) % 7)) % 7;
  unsigned result;

  if (week < 1 || week > 5)
  {
    synce_error("Invalid week number %i", week);
    return 0;
  }

  for (;;)
  {
    result = first_sunday + (week - 1) * 7;
    if (result > days_of_month[month-1])
      week--;
    else
      break;
  }

  return result;
}/*}}}*/

static bool using_daylight_saving(RRA_Timezone* tzi, struct tm* time_struct)
{
  int month = time_struct->tm_mon + 1;

  if (tzi->StandardMonthOfYear > tzi->DaylightMonthOfYear)
  {
    if (month < tzi->DaylightMonthOfYear || month > tzi->StandardMonthOfYear)
      return false;
    else if (month > tzi->DaylightMonthOfYear && month < tzi->StandardMonthOfYear)
      return true;
    
    if (month == tzi->StandardMonthOfYear)
    {
      int day = day_from_month_and_week(tzi->StandardMonthOfYear, tzi->StandardInstance);
      if (time_struct->tm_mday < day)
        return !false;
      else if (time_struct->tm_mday > day)
        return !true;
      else /* Standard start day */
      {
        if (time_struct->tm_hour >= tzi->StandardStartHour)
          return !true;
        else
          return !false;
      }
    }
    
    if (month == tzi->DaylightMonthOfYear)
    {
      int day = day_from_month_and_week(tzi->DaylightMonthOfYear, tzi->DaylightInstance);
      if (time_struct->tm_mday < day)
        return false;
      else if (time_struct->tm_mday > day)
        return true;
      else /* daylight saving start day */
      {
        if (time_struct->tm_hour >= tzi->DaylightStartHour)
          return true;
        else
          return false;
      }
    }

    synce_error("Month is %i", month);
    assert(0);  /* should not be reached */
  }
  else
  {
    synce_error("Cannot handle this time zone");
  }
    
  return false;
}

time_t rra_timezone_convert_from_utc(RRA_Timezone* tzi, time_t unix_time)
{
  time_t result = RRA_TIMEZONE_INVALID_TIME;
  struct tm time_struct;
  
  if (tzi && gmtime_r(&unix_time, &time_struct))
  {
    unix_time -= tzi->Bias * 60;

    if (using_daylight_saving(tzi, &time_struct))
      unix_time -= tzi->DaylightBias * 60;
    else
      unix_time -= tzi->StandardBias * 60;

    result = unix_time;
  }

  return result;
}

time_t rra_timezone_convert_to_utc(RRA_Timezone* tzi, time_t unix_time)
{
  time_t result = RRA_TIMEZONE_INVALID_TIME;
  struct tm time_struct;
  
  if (tzi && gmtime_r(&unix_time, &time_struct))
  {
    unix_time += tzi->Bias * 60;
    
    if (using_daylight_saving(tzi, &time_struct))
      unix_time += tzi->DaylightBias * 60;
    else
      unix_time += tzi->StandardBias * 60;

    result = unix_time;
  }

  return result;
}

static void offset_string(char* buffer, size_t size, int default_bias, int extra_bias)/*{{{*/
{
  int bias = default_bias + extra_bias;
  snprintf(buffer, size, "%+03i%02i", -bias / 60, abs(bias) % 60);
}/*}}}*/

static bool time_string(char* buffer, size_t size, /*{{{*/
    unsigned month, unsigned week, unsigned hour)
{
  if (week <= 5 || month <= 12)
  {
    unsigned day = day_from_month_and_week(month, week);

    if (!day)
    {
      synce_error("Unknown month/week combination: week=%i, month=%i - report to SynCE developers!",
          week, month);
    }
    
    snprintf(buffer, size, "1970%02i%02iT%02i0000",
        month, day, hour);
    return true;
  }
  else
  {
    synce_error("Bad time zone information: week=%i, month=%i", week, month);
    return false;
  }
}/*}}}*/

static void add_rrule(Generator* generator, unsigned instance, unsigned month)/*{{{*/
{
  char rrule[128];
  
  snprintf(rrule, sizeof(rrule), 
      "FREQ=YEARLY;INTERVAL=1;BYDAY=%iSU;BYMONTH=%i",
      (5 == instance) ? (unsigned)-1 : instance, month);
  
  generator_add_simple(generator, "RRULE", rrule);
}     /*}}}*/

static void add_tzid(Generator* generator, RRA_Timezone* tzi)/*{{{*/
{
  char* id = NULL;
  rra_timezone_create_id(tzi, &id);
  generator_add_simple(generator, "TZID", id);
  rra_timezone_free_id(id);
}/*}}}*/

bool rra_timezone_generate_vtimezone(Generator* generator, RRA_Timezone* tzi)/*{{{*/
{
  bool success = false;
  char standard_offset[10];
  char daylight_offset[10];
  char dtstart[20];

  /* UTC = local time + bias   -->   UTC - bias = local time */

  offset_string(standard_offset, sizeof(standard_offset), tzi->Bias, tzi->StandardBias);
  offset_string(daylight_offset, sizeof(daylight_offset), tzi->Bias, tzi->DaylightBias);

  generator_add_simple(generator, "BEGIN", "VTIMEZONE");
  add_tzid(generator, tzi);

  /* 
     Daylight time 
   */

  generator_add_simple(generator, "BEGIN", "DAYLIGHT");
  /* required: dtstart / tzoffsetto / tzoffsetfrom */
  generator_add_simple(generator, "TZOFFSETFROM", standard_offset);
  generator_add_simple(generator, "TZOFFSETTO", daylight_offset);

  if (!time_string(dtstart, sizeof(dtstart), tzi->DaylightMonthOfYear, tzi->DaylightInstance, tzi->DaylightStartHour))
    goto exit;

  generator_add_simple(generator, "DTSTART", dtstart);
  add_rrule(generator, tzi->DaylightInstance, tzi->DaylightMonthOfYear);
  generator_add_simple(generator, "END", "DAYLIGHT");

  /* 
     Standard time 
   */

  generator_add_simple(generator, "BEGIN", "STANDARD");
  /* required: dtstart / tzoffsetto / tzoffsetfrom */
  generator_add_simple(generator, "TZOFFSETFROM", daylight_offset);
  generator_add_simple(generator, "TZOFFSETTO", standard_offset);

  if (!time_string(dtstart, sizeof(dtstart), tzi->StandardMonthOfYear, tzi->StandardInstance, tzi->StandardStartHour))
    goto exit;
  
  generator_add_simple(generator, "DTSTART", dtstart);
  add_rrule(generator, tzi->StandardInstance, tzi->StandardMonthOfYear);
  generator_add_simple(generator, "END", "STANDARD");

  generator_add_simple(generator, "END", "VTIMEZONE");

  success = true;

exit:
  return success;
}/*}}}*/