summaryrefslogtreecommitdiff
path: root/src/datetime.c
blob: 5f5d9bfb1da17147a4ac8add08398964038bb57b (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* 19950925 */
#include "datetime.h"

void datetime_tai(struct datetime *dt, datetime_sec t)
{
  int day;
  int tod;
  int year;
  int yday;
  int wday;
  int mon;

  tod = t % 86400;
  day = t / 86400;
  if (tod < 0) {
    tod += 86400;
    --day;
  }

  dt->hour = tod / 3600;
  tod %= 3600;
  dt->min = tod / 60;
  dt->sec = tod % 60;

  wday = (day + 4) % 7;
  if (wday < 0) wday += 7;
  dt->wday = wday;

  day -= 11017;
  /* day 0 is march 1, 2000 */
  year = 5 + day / 146097;
  day = day % 146097;
  if (day < 0) {
    day += 146097;
    --year;
  }
  /* from now on, day is nonnegative */
  year *= 4;
  if (day == 146096) {
    year += 3;
    day = 36524;
  } else {
    year += day / 36524;
    day %= 36524;
  }
  year *= 25;
  year += day / 1461;
  day %= 1461;
  year *= 4;
  yday = (day < 306);
  if (day == 1460) {
    year += 3;
    day = 365;
  } else {
    year += day / 365;
    day %= 365;
  }
  yday += day;

  day *= 10;
  mon = (day + 5) / 306;
  day = day + 5 - 306 * mon;
  day /= 10;
  if (mon >= 10) {
    yday -= 306;
    ++year;
    mon -= 10;
  } else {
    yday += 59;
    mon += 2;
  }

  dt->yday = yday;
  dt->year = year - 1900;
  dt->mon = mon;
  dt->mday = day + 1;
}