DCL 4.0
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->nMonth = mdy[0];
32 _r->nDay = mdy[1];
33 _r->nYear = 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->nMonth;
41 mdy[1] = _s->nDay;
42 mdy[2] = _s->nYear;
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->nHour = h;
54 _r->nMin = m;
55 _r->nSec = s;
56 _r->nFrac = f * 10000;
57}
58
59ByteString __encode(const SQL::Time* _s)
60{
61 return ByteString::format(__TIME_FORMAT,
62 _s->nHour, _s->nMin, _s->nSec, _s->nFrac / 10000);
63}
64
65void __decode(const timestamp* _s, SQL::TimeStamp* _r)
66{
67 // PostgreSQL timestamp API에서 TZ는 무시된다.
68 char sz[40]; // 2025-04-07 10:09:49 +0900 25
69 int r = PGTYPEStimestamp_fmt_asc((timestamp*) _s, sz, sizeof(sz),
70 "%Y-%m-%d %H:%M:%S %z");
71 //__DCL_TRACE2(L"[%lld][%hs]\n", *_s, sz);
72 int Y, m, d, H, M, S, zh, zm;
73 (void) sscanf(sz, "%d-%02u-%02u %02u:%02u:%02u %3d%02u",
74 &Y, &m, &d, &H, &M, &S, &zh, &zm);
75 _r->nYear = Y - (*_s < -63082281600000000LL ? 1 : 0);
76 _r->nMonth = m;
77 _r->nDay = d;
78 _r->nHour = H;
79 _r->nMin = M;
80 _r->nSec = S;
81 _r->nFrac = 0;
82 _r->nTzMin = INT16_MIN; // (zh * 60) + zm;
83 //__DCL_TRACE4(L"[%d][%d][%d][%d]\n", Y, m, d, H);
84 //__DCL_TRACE4(L"[%d][%d][%d][%d]\n", M, S, zh, zm);
85}
86
87inline int __ABS(int n)
88{
89 return n < 0 ? -n : n;
90}
91
92void __encode(const SQL::TimeStamp* _s, timestamp* _r)
93{
94 //int zh = 0, zm = 0;
95 //if (_s->nTzMin != INT16_MIN) {
96 // zh = _s->nTzMin / 60;
97 // zm = __ABS(_s->nTzMin % 60);
98 //}
99 char sz[40];
100 int n = snprintf(
101 sz, sizeof(sz), "%04d-%02u-%02u %02u:%02u:%02u",
102 _s->nYear, _s->nMonth, _s->nDay,
103 _s->nHour, _s->nMin, _s->nSec
104 );
105 //sz[n] = '\0';
106 //__DCL_TRACE1(L"[%hs]\n", sz);
107 PGTYPEStimestamp_defmt_asc(sz, "%Y-%m-%d %H:%M:%S", _r);
108}
109
110void __decode(const interval* _s, SQL::Interval* _r)
111{
112 //__DCL_TRACE2(L"month[%ld] time[%lld]\n", _s->month, _s->time);
113 _r->nYears = _s->month / 12;
114 _r->nMonths = _s->month % 12;
115
116 _r->nFracs = (_s->time % 1000000) * 1000;
117 int64_t t = _s->time / 1000000;
118 _r->nSecs = t % 60;
119 t /= 60;
120 _r->nMins = t % 60;
121 t /= 60;
122 _r->nHours = t % 24;
123 _r->nDays = (int) (t / 24);
124}
125
126void __encode(const SQL::Interval* _s, interval* _r)
127{
128 //__DCL_TRACE3(L"[%d][%d][%d]\n", _s->nYears, _s->nMonths, _s->nDays);
129 //__DCL_TRACE4(L"[%d][%d][%d][%d]\n",
130 // _s->nHours, _s->nMins, _s->nSecs, _s->nFracs);
131 _r->month = _s->nYears * 12 + _s->nMonths;
132 _r->time = (_s->nFracs / 1000LL)
133 + (_s->nSecs * 1000000LL)
134 + (_s->nMins * 60 * 1000000LL)
135 + (_s->nHours * 60 * 60 * 1000000LL)
136 + (_s->nDays * 24 * 60 * 60 * 1000000LL)
137 ;
138}
139
140__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:260
void CharsetConvertException *size_t n
Definition SQLField.cpp:253
uint8_t nMonth
Definition SQLCore.h:97
int16_t nYear
Definition SQLCore.h:96
uint8_t nDay
Definition SQLCore.h:98
int8_t nHours
Definition SQLCore.h:128
int32_t nFracs
Definition SQLCore.h:131
int8_t nSecs
Definition SQLCore.h:130
int8_t nMins
Definition SQLCore.h:129
int32_t nYears
Definition SQLCore.h:125
int8_t nMonths
Definition SQLCore.h:126
int32_t nDays
Definition SQLCore.h:127
uint8_t nHour
Definition SQLCore.h:103
uint8_t nMin
Definition SQLCore.h:104
uint8_t nSec
Definition SQLCore.h:105
uint32_t nFrac
Definition SQLCore.h:106
int16_t nYear
Definition SQLCore.h:111
uint8_t nDay
Definition SQLCore.h:113
uint8_t nHour
Definition SQLCore.h:114
uint8_t nSec
Definition SQLCore.h:116
uint8_t nMonth
Definition SQLCore.h:112
uint8_t nMin
Definition SQLCore.h:115