Branch data Line data Source code
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-2011 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 : : #include <string.h>
31 : :
32 : : #include <pdf-alloc.h>
33 : : #include <pdf-time-context.h>
34 : :
35 : : /* Time module context */
36 : : struct pdf_time_context_s {
37 : : pdf_i32_t local_time_gmt_offset; /* Seconds west of GMT */
38 : : pdf_bool_t local_time_daylight_save; /* True if Daylight saving times */
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 struct pdf_time_context_s time_context;
45 : : pdf_bool_t time_context_initialized = PDF_FALSE;
46 : :
47 : : void
48 : 0 : pdf_time_context_deinit (void)
49 : : {
50 [ # # ]: 0 : if (time_context_initialized)
51 : : {
52 : 0 : memset (&time_context, 0, sizeof (time_context));
53 : 0 : time_context_initialized = PDF_FALSE;
54 : : PDF_DEBUG_BASE ("Deinitialized Text context...");
55 : : }
56 : 0 : }
57 : :
58 : : /* Initialize time context */
59 : : pdf_bool_t
60 : 745 : pdf_time_context_init (pdf_error_t **error)
61 : : {
62 : : time_t tloc;
63 : : struct tm* time_struct;
64 : :
65 : : /* If already initialized, just return */
66 [ - + ]: 745 : if (time_context_initialized)
67 : 0 : return PDF_TRUE;
68 : :
69 : 745 : time (&tloc);
70 : 745 : time_struct = localtime (&tloc);
71 : :
72 : : #if defined PDF_HOST_WIN32
73 : : /* mingw does not support tm_gmtoff in struct tm, but it provides
74 : : * the _timezone global variable with the difference in seconds
75 : : * between GMT and local time. */
76 : : time_context.local_time_gmt_offset = _timezone;
77 : : #else
78 : : /* Set GMT offset */
79 : 745 : time_context.local_time_gmt_offset = time_struct->tm_gmtoff;
80 : : #endif
81 : :
82 : : /* Set flag to indicate if Daylight saving times are applied in the system
83 : : * if needed */
84 : 745 : time_context.local_time_daylight_save = (time_struct->tm_isdst == 0 ?
85 : : PDF_FALSE : PDF_TRUE);
86 : :
87 : : PDF_DEBUG_BASE("Initialized Time context...");
88 : : PDF_DEBUG_BASE(" GMT offset: %d secs", time_context.local_time_gmt_offset);
89 : : PDF_DEBUG_BASE(" Daylight saving? %s", time_context.local_time_daylight_save ? \
90 : : "yes":"no");
91 : :
92 : 745 : return PDF_TRUE;
93 : : }
94 : :
95 : : /* Get the GMT offset of the local time configuration. The offset is obtained as
96 : : * seconds west of GMT */
97 : : pdf_i32_t
98 : 79 : pdf_time_context_get_gmt_offset (void)
99 : : {
100 : 79 : return time_context.local_time_gmt_offset;
101 : : }
102 : :
103 : : /* End of pdf-time-context.c */
|