summaryrefslogtreecommitdiff
path: root/goo/glibc.cc
blob: 4968047feeafd59d2c3e7bc0b329446c03ca51c7 (plain)
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
//========================================================================
//
// glibc.h
//
// Emulate various non-portable glibc functions.
//
// This file is licensed under the GPLv2 or later
//
// Copyright (C) 2016 Adrian Johnson <ajohnson@redneon.com>
//
//========================================================================

#include "glibc.h"

#ifndef HAVE_GMTIME_R
struct tm *gmtime_r(const time_t *timep, struct tm *result)
{
  struct tm *gt;
  gt = gmtime(timep);
  if (gt)
    *result = *gt;
  return gt;
}
#endif

#ifndef HAVE_LOCALTIME_R
struct tm *localtime_r(const time_t *timep, struct tm *result)
{
  struct tm *lt;
  lt = localtime(timep);
  *result = *lt;
  return lt;
}
#endif

#ifndef HAVE_TIMEGM
// Get offset of local time from UTC in seconds. DST is ignored.
static time_t getLocalTimeZoneOffset()
{
  time_t utc, local;
  struct tm tm_utc;
  time (&utc);
  gmtime_r(&utc, &tm_utc);
  local = mktime(&tm_utc);
  return difftime(utc, local);
}

time_t timegm(struct tm *tm)
{
  tm->tm_isdst = 0;
  time_t t = mktime(tm);
  if (t == -1)
    return t;

  t += getLocalTimeZoneOffset();
  return t;
}
#endif