1 : /* -*- mode: C -*-
2 : *
3 : * File: pdf-time-context.c
4 : * Date: Sun May 18 13:08:37 2008
5 : *
6 : * GNU PDF Library - Time Module Context management
7 : *
8 : */
9 :
10 : /* Copyright (C) 2008 Free Software Foundation, Inc. */
11 :
12 : /* This program is free software: you can redistribute it and/or modify
13 : * it under the terms of the GNU General Public License as published by
14 : * the Free Software Foundation, either version 3 of the License, or
15 : * (at your option) any later version.
16 : *
17 : * This program is distributed in the hope that it will be useful,
18 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 : * GNU General Public License for more details.
21 : *
22 : * You should have received a copy of the GNU General Public License
23 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 : */
25 :
26 : #include <config.h>
27 :
28 : #include <stdio.h>
29 : #include <time.h>
30 :
31 : #include <pdf-alloc.h>
32 : #include <pdf-time-context.h>
33 :
34 :
35 : typedef struct pdf_time_context_s {
36 : pdf_i32_t local_time_gmt_offset; /* Seconds west of GMT */
37 : pdf_bool_t local_time_daylight_save; /* True if Daylight saving times */
38 : } pdf_time_context_t;
39 :
40 :
41 : /* This context will be initialized only once at program startup, and it will
42 : * be treated as constant from then on, so there shouldn't be any problem
43 : * with multiple threading and reentrancy */
44 : static pdf_time_context_t time_context;
45 :
46 :
47 :
48 : /* Initialize time context. Must be done only once at program startup!.
49 : * Not thread-safe! */
50 : pdf_status_t
51 : pdf_time_context_init(void)
52 618 : {
53 : time_t tloc;
54 : struct tm* time_struct;
55 :
56 618 : time(&tloc);
57 618 : time_struct = localtime(&tloc);
58 :
59 : #if defined PDF_HOST_WIN32
60 : /* mingw does not support tm_gmtoff in struct tm, but it provides
61 : the _timezone global variable with the difference in seconds
62 : between GMT and local time. */
63 : time_context.local_time_gmt_offset = _timezone;
64 : #else
65 : /* Set GMT offset */
66 618 : time_context.local_time_gmt_offset = time_struct->tm_gmtoff;
67 : #endif
68 :
69 : /* Set flag to indicate if Daylight saving times are applied in the system
70 : * if needed */
71 618 : time_context.local_time_daylight_save = (time_struct->tm_isdst == 0) ? PDF_FALSE : PDF_TRUE;
72 :
73 : PDF_DEBUG_BASE("Initializing Time module...");
74 : PDF_DEBUG_BASE("GMT offset: %d secs", time_context.local_time_gmt_offset);
75 : PDF_DEBUG_BASE("Daylight saving? %s",time_context.local_time_daylight_save ? \
76 : "yes":"no");
77 :
78 618 : return PDF_OK;
79 : }
80 :
81 :
82 : /* Get the GMT offset of the local time configuration. The offset is obtained as
83 : * seconds west of GMT */
84 : pdf_i32_t
85 : pdf_time_context_get_gmt_offset(void)
86 76 : {
87 76 : return time_context.local_time_gmt_offset;
88 : }
89 :
90 :
91 : /* End of pdf-time-context.c */
|