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#include <datatype/timestamp.h>
13#include <pgtime.h>
14#include <utils/datetime.h>
15}
16
17namespace kDB::Repository::DatabaseInterface::PostgreSQL::BinaryInterface
18{
19
20 inline void
21 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
71 timestamp2tm(qint64 dt, struct pg_tm * tm, fsec_t *fsec, const char **tzn)
72 {
73 qint64 date;
74 qint64 time;
75
76 time = dt;
77 TMODULO(time, date, USECS_PER_DAY);
78
79 if (time < (qint64)(0))
80 {
81 time += USECS_PER_DAY;
82 date -= 1;
83 }
84
85 /* add offset to go from J2000 back to standard Julian date */
86 date += POSTGRES_EPOCH_JDATE;
87
88 /* Julian day routine does not work for negative Julian days */
89 if (date < 0 || date > (qint64) INT_MAX)
90 return -1;
91
92 j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
93 dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
94
95 /* Done no TZ conversion wanted */
96 tm->tm_isdst = -1;
97 tm->tm_gmtoff = 0;
98 tm->tm_zone = NULL;
99 if (tzn != NULL)
100 *tzn = NULL;
101 return 0;
102 }
103}