CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Common/time.cpp
Views: 1798
1
#include "time.h"
2
3
/*
4
mktime replacement from Samba
5
*/
6
time_t ap_mktime(const struct tm *t)
7
{
8
time_t epoch = 0;
9
int n;
10
int mon [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, y, m, i;
11
const unsigned MINUTE = 60;
12
const unsigned HOUR = 60*MINUTE;
13
const unsigned DAY = 24*HOUR;
14
const unsigned YEAR = 365*DAY;
15
16
if (t->tm_year < 70) {
17
return (time_t)-1;
18
}
19
20
n = t->tm_year + 1900 - 1;
21
epoch = (t->tm_year - 70) * YEAR +
22
((n / 4 - n / 100 + n / 400) - (1969 / 4 - 1969 / 100 + 1969 / 400)) * DAY;
23
24
y = t->tm_year + 1900;
25
m = 0;
26
27
for (i = 0; i < t->tm_mon; i++) {
28
epoch += mon [m] * DAY;
29
if (m == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) {
30
epoch += DAY;
31
}
32
33
if (++m > 11) {
34
m = 0;
35
y++;
36
}
37
}
38
39
epoch += (t->tm_mday - 1) * DAY;
40
epoch += t->tm_hour * HOUR + t->tm_min * MINUTE + t->tm_sec;
41
42
return epoch;
43
}
44
45
46