DCL 4.1
Loading...
Searching...
No Matches
PgTypes.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#include <pgtypes_date.h>
4#include <pgtypes_timestamp.h>
5#include <pgtypes_interval.h>
6
7#include <stdio.h>
8#include <string.h>
9
10#include <dcl/Object.h>
11#if __DCL_HAVE_ALLOC_DEBUG
12#undef __DCL_ALLOC_LEVEL
13#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
14#endif
15
16#include <dcl/SQLCore.h>
17
18#include "PgTypes_.h"
19
20#ifdef __DCL_DEBUG
21#undef __THIS_FILE__
22static const wchar_t __THIS_FILE__[] = __T("dcl/sql/PgTypes.cpp");
23#endif
24
25__DCL_BEGIN_NAMESPACE
26
27void __decode(const date* _s, SQL::Date* _r)
28{
29 int mdy[3];
30 PGTYPESdate_julmdy(*_s, mdy);
31 _r->month = mdy[0];
32 _r->day = mdy[1];
33 _r->year = mdy[2] - (*_s < 730119 ? 1 : 0);
34 //__DCL_TRACE4(L"[%d][%d][%d][%d]\n", *_s, mdy[0], mdy[1], mdy[2]);
35}
36
37void __encode(const SQL::Date* _s, date* _r)
38{
39 int mdy[3];
40 mdy[0] = _s->month;
41 mdy[1] = _s->day;
42 mdy[2] = _s->year;
43 PGTYPESdate_mdyjul(mdy, _r);
44}
45
46static const char* __TIME_FORMAT =
47 "%02u:%02u:%02u.%05u";
48
49void __decode(const char* _s, SQL::Time* _r)
50{
51 int h, m, s, f;
52 (void) sscanf(_s, __TIME_FORMAT, &h, &m, &s, &f);
53 _r->hour = h;
54 _r->min = m;
55 _r->sec = s;
56 _r->frac = f * 10000;
57 _r->tzoff = INT16_MIN;
58}
59
60ByteString __encode(const SQL::Time* _s)
61{
62 return ByteString::format(__TIME_FORMAT,
63 _s->hour, _s->min, _s->sec, _s->frac / 10000);
64}
65
66void __decode(const timestamp* _s, SQL::TimeStamp* _r)
67{
68 // PostgreSQL timestamp API에서 TZ는 무시된다.
69 char sz[40]; // 2025-04-07 10:09:49 +0900 25
70 int r = PGTYPEStimestamp_fmt_asc((timestamp*) _s, sz, sizeof(sz),
71 "%Y-%m-%d %H:%M:%S %z");
72 //__DCL_TRACE2(L"[%lld][%hs]\n", *_s, sz);
73 int Y, m, d, H, M, S, tzhour, tzmin;
74 (void) sscanf(sz, "%d-%02u-%02u %02u:%02u:%02u %3d%02u",
75 &Y, &m, &d, &H, &M, &S, &tzhour, &tzmin);
76 _r->year = Y - (*_s < -63082281600000000LL ? 1 : 0);
77 _r->month = m;
78 _r->day = d;
79 _r->hour = H;
80 _r->min = M;
81 _r->sec = S;
82 _r->frac = 0;
83 _r->tzoff = INT16_MIN; // (tzhour * 60) + tzmin;
84 //__DCL_TRACE4(L"[%d][%d][%d][%d]\n", Y, m, d, H);
85 //__DCL_TRACE4(L"[%d][%d][%d][%d]\n", M, S, tzhour, tzmin);
86}
87
88inline int __ABS(int n)
89{
90 return n < 0 ? -n : n;
91}
92
93void __encode(const SQL::TimeStamp* _s, timestamp* _r)
94{
95 //int tzhour = 0, tzmin = 0;
96 //if (_s->tzoff != INT16_MIN) {
97 // tzhour = _s->tzoff / 60;
98 // tzmin = __ABS(_s->tzoff % 60);
99 //}
100 char sz[40];
101 int n = snprintf(
102 sz, sizeof(sz), "%04d-%02u-%02u %02u:%02u:%02u",
103 _s->year, _s->month, _s->day,
104 _s->hour, _s->min, _s->sec
105 );
106 //sz[n] = '\0';
107 //__DCL_TRACE1(L"[%hs]\n", sz);
108 PGTYPEStimestamp_defmt_asc(sz, "%Y-%m-%d %H:%M:%S", _r);
109}
110
111void __decode(const interval* _s, SQL::Interval* _r)
112{
113 //__DCL_TRACE2(L"month[%ld] time[%lld]\n", _s->month, _s->time);
114 _r->years = _s->month / 12;
115 _r->months = _s->month % 12;
116
117 _r->fracs = (_s->time % 1000000) * 1000;
118 int64_t t = _s->time / 1000000;
119 _r->secs = t % 60;
120 t /= 60;
121 _r->mins = t % 60;
122 t /= 60;
123 _r->hours = t % 24;
124 _r->days = (int) (t / 24);
125}
126
127void __encode(const SQL::Interval* _s, interval* _r)
128{
129 //__DCL_TRACE3(L"[%d][%d][%d]\n", _s->years, _s->months, _s->days);
130 //__DCL_TRACE4(L"[%d][%d][%d][%d]\n",
131 // _s->hours, _s->mins, _s->secs, _s->fracs);
132 _r->month = _s->years * 12 + _s->months;
133 _r->time = (_s->fracs / 1000LL)
134 + (_s->secs * 1000000LL)
135 + (_s->mins * 60 * 1000000LL)
136 + (_s->hours * 60 * 60 * 1000000LL)
137 + (_s->days * 24 * 60 * 60 * 1000000LL)
138 ;
139}
140
141__DCL_END_NAMESPACE
#define __THIS_FILE__
Definition _trace.h:14
#define INT16_MIN
Definition Config.h:312
#define H(x, y, z)
Definition MD5.cpp:175
#define __ABS(n)
Definition MyParam.cpp:143
#define __T(str)
Definition Object.h:44
__DCL_BEGIN_NAMESPACE void __decode(const date *_s, SQL::Date *_r)
Definition PgTypes.cpp:27
void __encode(const SQL::Date *_s, date *_r)
Definition PgTypes.cpp:37
ByteString r
_r
Definition SQLField.cpp:261
void CharsetConvertException *size_t n
Definition SQLField.cpp:254
int16_t year
Definition SQLCore.h:97
uint8_t month
Definition SQLCore.h:98
uint8_t day
Definition SQLCore.h:99
int32_t years
Definition SQLCore.h:127
int32_t days
Definition SQLCore.h:129
int8_t hours
Definition SQLCore.h:130
int8_t mins
Definition SQLCore.h:131
int32_t fracs
Definition SQLCore.h:133
int8_t months
Definition SQLCore.h:128
int8_t secs
Definition SQLCore.h:132
uint8_t hour
Definition SQLCore.h:104
uint8_t sec
Definition SQLCore.h:106
uint32_t frac
Definition SQLCore.h:107
uint8_t min
Definition SQLCore.h:105
uint8_t min
Definition SQLCore.h:117
uint8_t sec
Definition SQLCore.h:118
uint8_t hour
Definition SQLCore.h:116
uint8_t day
Definition SQLCore.h:115
int16_t year
Definition SQLCore.h:113
uint8_t month
Definition SQLCore.h:114