forked from r-dbi/RPostgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPqUtils.cpp
More file actions
17 lines (15 loc) · 681 Bytes
/
Copy pathPqUtils.cpp
File metadata and controls
17 lines (15 loc) · 681 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "pch.h"
#include "PqUtils.h"
// From https://stackoverflow.com/a/40914871/946850:
int days_from_civil(int y, int m, int d) {
y -= m <= 2;
const int era = (y >= 0 ? y : y - 399) / 400;
const int yoe = static_cast<unsigned>(y - era * 400); // [0, 399]
const int doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; // [0, 365]
const int doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
return era * 146097 + doe - 719468;
}
time_t tm_to_time_t(const tm& tm_) {
const time_t days = days_from_civil(tm_.tm_year + 1900, tm_.tm_mon + 1, tm_.tm_mday);
return days * 86400 + tm_.tm_hour * 60 * 60 + tm_.tm_min * 60 + tm_.tm_sec;
}