kDB: Knowledge DataBase
Loading...
Searching...
No Matches
BinaryInterfaceTimeStamp_p.h
1/*-------------------------------------------------------------------------
2 *
3 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
4 * Portions Copyright (c) 1994, Regents of the University of California
5 *
6 *-------------------------------------------------------------------------
7 */
8
9#include "postgresql_p.h"
10
11extern "C"
12{
13#include <datatype/timestamp.h>
14#include <pgtime.h>
15#include <utils/datetime.h>
16}
17
18namespace kDB::Repository::DatabaseInterface::PostgreSQL::BinaryInterface
19{
20
21 inline void j2date(int jd, int* year, int* month, int* day)
22 {
23 unsigned int julian;
24 unsigned int quad;
25 unsigned int extra;
26 int y;
27
28 julian = jd;
29 julian += 32044;
30 quad = julian / 146097;
31 extra = (julian - quad * 146097) * 4 + 3;
32 julian += 60 + quad * 3 + extra / 146097;
33 quad = julian / 1461;
34 julian -= quad * 1461;
35 y = julian * 4 / 1461;
36 julian = ((y != 0) ? (julian + 305) % 365 : (julian + 306) % 366) + 123;
37 y += quad * 4;
38 *year = y - 4800;
39 quad = julian * 2141 / 65536;
40 *day = julian - 7834 * quad / 256;
41 *month = (quad + 10) % 12 + 1;
42
43 return;
44 } /* j2date() */
45
46 inline void dt2time(double jd, int* hour, int* min, int* sec, fsec_t* fsec)
47 {
48 int64 time;
49
50 time = jd;
51 *hour = time / USECS_PER_HOUR;
52 time -= (*hour) * USECS_PER_HOUR;
53 *min = time / USECS_PER_MINUTE;
54 time -= (*min) * USECS_PER_MINUTE;
55 *sec = time / USECS_PER_SEC;
56 *fsec = time - (*sec * USECS_PER_SEC);
57 } /* dt2time() */
58
59 /*
60 * timestamp2tm() - Convert timestamp data type to POSIX time structure.
61 *
62 * Note that year is _not_ 1900-based, but is an explicit full value.
63 * Also, month is one-based, _not_ zero-based.
64 * Returns:
65 * 0 on success
66 * -1 on out of range
67 *
68 * If attimezone is NULL, the global timezone setting will be used.
69 */
70 inline int timestamp2tm(qint64 dt, struct pg_tm* tm, fsec_t* fsec, const char** tzn)
71 {
72 qint64 date;
73 qint64 time;
74
75 time = dt;
76 TMODULO(time, date, USECS_PER_DAY);
77
78 if(time < (qint64)(0))
79 {
80 time += USECS_PER_DAY;
81 date -= 1;
82 }
83
84 /* add offset to go from J2000 back to standard Julian date */
85 date += POSTGRES_EPOCH_JDATE;
86
87 /* Julian day routine does not work for negative Julian days */
88 if(date < 0 || date > (qint64)INT_MAX)
89 return -1;
90
91 j2date((int)date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
92 dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
93
94 /* Done no TZ conversion wanted */
95 tm->tm_isdst = -1;
96 tm->tm_gmtoff = 0;
97 tm->tm_zone = NULL;
98 if(tzn != NULL)
99 *tzn = NULL;
100 return 0;
101 }
102} // namespace kDB::Repository::DatabaseInterface::PostgreSQL::BinaryInterface