/
timegm.c
88 lines (68 loc) · 1.51 KB
/
timegm.c
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
/* -*- Mode: c; c-basic-offset: 2 -*-
*
* timegm.c - timegm compatibility
*
* This file is in the public domain.
*
*/
#ifdef HAVE_CONFIG_H
#include <rasqal_config.h>
#endif
#ifdef WIN32
#include <win32_rasqal_config.h>
#endif
#include <stdio.h>
#include <string.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include "rasqal.h"
#include "rasqal_internal.h"
#ifdef WIN32
time_t
rasqal_timegm(struct tm *tm)
{
struct tm my_tm;
memcpy(&my_tm, tm, sizeof(struct tm));
/* _mkgmtime() changes the value of the struct tm* you pass in, so
* use a copy
*/
return _mkgmtime(&my_tm);
}
#else
PRAGMA_IGNORE_WARNING_REDUNDANT_DECLS_START
/* function prototype may be in stdlib.h or stdio.h or nowhere */
extern char *getenv(const char *);
extern int setenv(const char *name, const char *value, int replace);
extern int unsetenv(const char *name);
/* time.h or nowhere */
extern void tzset(void);
PRAGMA_IGNORE_WARNING_END
time_t
rasqal_timegm(struct tm *tm)
{
time_t result;
char *zone;
zone = getenv("TZ");
if(zone) {
/* save it so that setenv() does not destroy shared value */
size_t zone_len = strlen(zone);
char *zone_copy = RASQAL_MALLOC(char*, zone_len + 1);
if(!zone_copy)
return -1;
memcpy(zone_copy, zone, zone_len + 1); /* Copy NUL */
zone = zone_copy;
}
setenv("TZ", "UTC", 1);
tzset();
result = mktime(tm);
if(zone)
setenv("TZ", zone, 1);
else
unsetenv("TZ");
tzset();
if(zone)
RASQAL_FREE(char*, zone);
return result;
}
#endif