summaryrefslogtreecommitdiffstats
path: root/timegm.c
blob: a59f6bde657217d2e7cd9d736b8159f23c8cf654 (plain) (blame)
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
/**
 * @file
 * For systems lacking timegm()
 *
 * @authors
 * Copyright (C) 2022 Claes Nästén <pekdon@gmail.com>
 *
 * @copyright
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @page neo_timegm For systems lacking timegm()
 *
 * For systems lacking timegm()
 */

#include "config.h"
#include <stdbool.h>
#include <time.h>

/**
 * is_leap_year - Is this a Leap Year?
 * @param year Year
 * @retval true It's a leap year
 */
static bool is_leap_year(int year)
{
  return ((year % 400) == 0) || ((year % 4) == 0) && ((year % 100) != 0);
}

/**
 * tm_to_sec - Convert a time structure to number of seconds since the epoch
 * @param tm Time structure
 * @retval num Seconds since the epoch
 */
static time_t tm_to_sec(struct tm *tm)
{
  time_t val = tm->tm_sec + (tm->tm_min * 60) + (tm->tm_hour * 3600) + (tm->tm_yday * 86400);

  int year = 1900 + tm->tm_year;
  for (int i = 1970; i < year; i++)
  {
    if (is_leap_year(i))
      val += (366 * 86400);
    else
      val += (365 * 86400);
  }

  return val;
}

/**
 * timegm - Convert struct tm to time_t seconds since epoch
 * @param tm Time to convert to seconds since epoch
 * @retval num Seconds since epoch
 */
time_t timegm(struct tm *tm)
{
  return tm_to_sec(tm);
}