CUBRID Engine  latest
tz_support.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * tz_support.h : Timezone support
21  */
22 #ifndef _TZ_SUPPORT_H_
23 #define _TZ_SUPPORT_H_
24 
25 #include "dbtype_def.h"
26 #include "thread_compat.hpp"
27 #include "timezone_lib_common.h"
28 
29 #define db_utime_to_string db_timestamp_to_string
30 #define db_string_to_utime db_string_to_timestamp
31 #define db_date_parse_utime db_date_parse_timestamp
32 
33 enum
34 {
40 };
41 
42 extern void db_date_locale_init (void);
43 
44 #define TZLIB_SYMBOL_NAME_SIZE 64
45 #define MAX_LEN_OFFSET 10
46 
47 #if defined(_WIN32) || defined(WINDOWS) || defined(WIN64)
48 #define SHLIB_EXPORT_PREFIX "__declspec(dllexport)"
49 #define LIB_TZ_NAME "libcubrid_timezones.dll"
50 #elif defined(_AIX)
51 #define makestring1(x) #x
52 #define makestring(x) makestring1(x)
53 
54 #define SHLIB_EXPORT_PREFIX ""
55 #define LIB_TZ_NAME \
56  "libcubrid_timezones.a(libcubrid_timezones.so." makestring(MAJOR_VERSION) ")"
57 #else
58 #define SHLIB_EXPORT_PREFIX ""
59 #define LIB_TZ_NAME "libcubrid_timezones.so"
60 #endif
61 
62 #define IS_LEAP_YEAR(y) ((((y) & 3) == 0) && ((((y) % 25) != 0) || (((y) & 15) == 0)))
63 
64 /* WARNING: the define below yields 30 days in February; use with care! */
65 #define DAYS_IN_MONTH(m) (31 - ((m) > 6 ? (m) - 7 : (m)) % 2)
66 
67 #define IS_EMPTY_STR(s) ((s) == NULL || *(s) == '\0')
68 
69 #define TZ_MAX_YEAR 9999
70 /* support dates up to 31/12/9999 23:59:59 */
71 /* we need 2 Jan 10000 because of timezones like America/New_York
72  * which when transformed into UTC time reference exceed 1 Jan 10000 */
73 /* julian date for 2 Jan 10000 */
74 #define TZ_MAX_JULIAN_DATE 5373486
75 
76 #define TZ_ZONE_ID_MAX 0x3ff
77 #define TZ_OFFSET_ID_MAX 0xff
78 #define TZ_DS_ID_MAX 0xff
79 
80 /* the difference in days-seconds between datetimes with timezone when is safe
81  * to consider only date without time */
82 #define DATE_DIFF_MATCH_SAFE_THRESHOLD_DAYS 2
83 #define DATE_DIFF_MATCH_SAFE_THRESHOLD_SEC 172800ll
84 
85 #define TZ_DS_STRING_SIZE 10
86 #define TZR_SIZE 100
87 #define ZONE_MAX 10000
88 
89 enum
90 {
104 };
105 
106 enum
107 {
116 };
117 
118 #define TZ_IS_ZONE_VALID_DECODE_INFO(tz_info) \
119  ((tz_info)->type == TZ_REGION_OFFSET \
120  || (tz_info)->zone.zone_id != TZ_ZONE_ID_MAX)
121 
122 #define TZ_IS_UTC_TZ_REGION(r) \
123  ((r)->type == TZ_REGION_OFFSET && (r)->offset == 0)
124 
126 #if defined (SA_MODE)
127 extern bool tz_Is_backward_compatible_timezone[];
128 extern bool tz_Compare_datetimetz_tz_id;
129 extern bool tz_Compare_timestamptz_tz_id;
130 #endif /* SA_MODE */
131 
132 #ifdef __cplusplus
133 extern "C"
134 {
135 #endif
136  extern int tz_load (void);
137  extern void tz_unload (void);
138  extern DB_BIGINT tz_timestamp_encode_leap_sec_adj (const int year_century, const int year, const int mon,
139  const int day);
140  extern void tz_timestamp_decode_sec (int timestamp, int *yearp, int *monthsp, int *dayp, int *hoursp, int *minutesp,
141  int *secondsp);
142  extern void tz_timestamp_decode_no_leap_sec (int timestamp, int *yearp, int *monthsp, int *dayp, int *hoursp,
143  int *minutesp, int *secondsp);
144  extern int tz_get_timezone_offset (const char *tz_str, int tz_size, char *result, DB_DATETIME * utc_datetime);
145  extern int tz_get_first_weekday_around_date (const int year, const int month, const int weekday, const int after_day,
146  const bool before);
147  extern const TZ_DATA *tz_get_data (void);
148  extern void tz_set_data (const TZ_DATA * data);
149  extern const TZ_DATA *tz_get_new_timezone_data (void);
150  extern void tz_set_new_timezone_data (const TZ_DATA * data);
151  extern const char *tz_get_system_timezone (void);
152  extern const char *tz_get_session_local_timezone (void);
154  extern void tz_get_session_tz_region (TZ_REGION * tz_region);
155  extern void tz_id_to_region (const TZ_ID * tz_id, TZ_REGION * tz_region);
156  extern const TZ_ID *tz_get_utc_tz_id (void);
157  extern const TZ_REGION *tz_get_utc_tz_region (void);
158  extern int tz_create_session_tzid_for_datetime (const DB_DATETIME * src_dt, bool src_is_utc, TZ_ID * tz_id);
159  extern int tz_create_session_tzid_for_timestamp (const DB_UTIME * src_ts, TZ_ID * tz_id);
160  extern int tz_str_to_region (const char *tz_str, const int tz_str_size, TZ_REGION * tz_region);
161  extern int tz_create_session_tzid_for_time (const DB_TIME * src_time, bool src_is_utc, TZ_ID * tz_id);
162  extern int tz_str_read_number (const char *str, const char *str_end, const bool strict, const bool read_sign,
163  int *val, const char **str_next);
164  extern int tz_str_read_time (const char *str, const char *str_end, bool need_minutes, bool allow_sec60, int *hour,
165  int *min, int *sec, const char **str_next);
166  extern int tz_str_to_seconds (const char *str, const char *str_end, int *sec, const char **str_next,
167  const bool is_offset);
168  extern int tz_get_ds_change_julian_date_diff (const int src_julian_date, const TZ_DS_RULE * ds_rule, const int year,
169  int *ds_rule_julian_date, full_date_t * date_diff);
170  extern int tz_create_datetimetz (const DB_DATETIME * dt, const char *tz_str, const int tz_size,
171  const TZ_REGION * default_tz_region, DB_DATETIMETZ * dt_tz, const char **end_tz_str);
172  extern int tz_create_timestamptz (const DB_DATE * date, const DB_TIME * time, const char *tz_str, const int tz_size,
173  const TZ_REGION * default_tz_region, DB_TIMESTAMPTZ * ts_tz,
174  const char **end_tz_str);
175  extern int tz_create_datetimetz_from_ses (const DB_DATETIME * dt, DB_DATETIMETZ * dt_tz);
176  extern int tz_utc_datetimetz_to_local (const DB_DATETIME * dt_utc, const TZ_ID * tz_id, DB_DATETIME * dt_local);
177  extern int tz_datetimeltz_to_local (const DB_DATETIME * dt_ltz, DB_DATETIME * dt_local);
178  extern int tz_id_to_str (const TZ_ID * tz_id, char *tz_str, const int tz_str_size);
179  extern int tz_datetimetz_fix_zone (const DB_DATETIMETZ * src_dt_tz, DB_DATETIMETZ * dest_dt_tz);
180  extern int tz_timestamptz_fix_zone (const DB_TIMESTAMPTZ * src_ts_tz, DB_TIMESTAMPTZ * dest_ts_tz);
181  extern int tz_conv_tz_datetime_w_region (const DB_DATETIME * src_dt, const TZ_REGION * src_tz_region,
182  const TZ_REGION * dest_tz_region, DB_DATETIME * dest_dt,
183  TZ_ID * src_tz_id_out, TZ_ID * dest_tz_id_out);
184  extern int tz_conv_tz_datetime_w_zone_name (const DB_DATETIME * src_dt, const char *source_zone, int len_source,
185  const char *dest_zone, int len_dest, DB_DATETIME * dest_dt);
186  extern int tz_conv_tz_time_w_zone_name (const DB_TIME * time_source, const char *source_zone, int len_source,
187  const char *dest_zone, int len_dest, DB_TIME * time_dest);
188  extern int tz_explain_tz_id (const TZ_ID * tz_id, char *tzr, const int tzr_size, char *tzdst, const int tzdst_size,
189  int *tzh, int *tzm);
190  extern int tz_create_datetimetz_from_offset (const DB_DATETIME * dt, const int tzh, const int tzm,
191  DB_DATETIMETZ * dt_tz);
192  extern int tz_create_timestamptz_from_offset (const DB_DATE * date, const DB_TIME * time, const int tzh,
193  const int tzm, DB_TIMESTAMPTZ * timestamp_tz);
194  extern int tz_get_best_match_zone (const char *name, int *size);
195  extern int tz_create_datetimetz_from_zoneid_and_tzd (const DB_DATETIME * dt, TZ_REGION * default_tz_region,
196  const int zone_id, const char *tzd, const int tzd_len,
197  bool is_time_tz, DB_DATETIMETZ * dt_tz);
198  extern int tz_create_timestamptz_from_zoneid_and_tzd (const DB_DATE * date, const DB_TIME * time,
199  TZ_REGION * default_tz_region, const int zone_id,
200  const char *tzd, const int tzd_len,
201  DB_TIMESTAMPTZ * timestamp_tz);
202  extern int tz_resolve_os_timezone (char *timezone, int buf_size);
203  extern void tz_set_tz_region_system (const TZ_REGION * tz_region);
204 #if !defined(SERVER_MODE)
206 #else
207  extern TZ_REGION *tz_get_server_tz_region_session (void);
208 #endif
209 #if !defined (CS_MODE)
210  extern int tz_timezones_start_scan (THREAD_ENTRY * thread_p, int show_type, DB_VALUE ** arg_values, int arg_cnt,
211  void **ptr);
212  extern int tz_full_timezones_start_scan (THREAD_ENTRY * thread_p, int show_type, DB_VALUE ** arg_values, int arg_cnt,
213  void **ptr);
214 #endif
215 
216  extern int tz_load_with_library_path (TZ_DATA * tzd, const char *timezone_library_path);
217  extern int tz_check_geographic_tz (const TZ_ID * tz_id);
218  extern int tz_check_session_has_geographic_tz (void);
219 #if !defined(SERVER_MODE)
220  extern int put_timezone_checksum (char *checksum);
221 #endif /* SERVER_MODE */
222 
223  extern int check_timezone_compat (const char *client_checksum, const char *server_checksum, const char *client_text,
224  const char *server_text);
225  extern void tz_tzid_convert_region_to_offset (TZ_ID * tz_id);
226  extern int tz_create_datetimetz_from_utc (const DB_DATETIME * src_dt, const TZ_REGION * dest_region,
227  DB_DATETIMETZ * dest_dt_tz);
228  extern int tz_create_datetimetz_from_parts (const int m, const int d, const int y, const int h, const int mi,
229  const int s, const int ms, const TZ_ID * tz_id, DB_DATETIMETZ * dt_tz);
230  extern int conv_tz (void *, const void *, DB_TYPE);
231  int tz_get_offset_in_mins (); //time zone offset in minutes from GMT
232 #ifdef __cplusplus
233 }
234 #endif
235 
236 #endif /* _TZ_SUPPORT_H_ */
unsigned int TZ_ID
Definition: dbtype_def.h:756
void tz_id_to_region(const TZ_ID *tz_id, TZ_REGION *tz_region)
Definition: tz_support.c:803
const TZ_ID * tz_get_utc_tz_id(void)
Definition: tz_support.c:816
int tz_full_timezones_start_scan(THREAD_ENTRY *thread_p, int show_type, DB_VALUE **arg_values, int arg_cnt, void **ptr)
Definition: tz_support.c:4775
int tz_resolve_os_timezone(char *timezone, int buf_size)
Definition: tz_support.c:4588
void tz_timestamp_decode_sec(int timestamp, int *yearp, int *monthsp, int *dayp, int *hoursp, int *minutesp, int *secondsp)
Definition: tz_support.c:458
int tz_check_session_has_geographic_tz(void)
Definition: tz_support.c:4995
DB_TYPE
Definition: dbtype_def.h:670
int tz_conv_tz_datetime_w_zone_name(const DB_DATETIME *src_dt, const char *source_zone, int len_source, const char *dest_zone, int len_dest, DB_DATETIME *dest_dt)
Definition: tz_support.c:3858
int tz_create_timestamptz_from_zoneid_and_tzd(const DB_DATE *date, const DB_TIME *time, TZ_REGION *default_tz_region, const int zone_id, const char *tzd, const int tzd_len, DB_TIMESTAMPTZ *timestamp_tz)
Definition: tz_support.c:4280
int tz_datetimeltz_to_local(const DB_DATETIME *dt_ltz, DB_DATETIME *dt_local)
Definition: tz_support.c:1628
const TZ_DATA * tz_get_new_timezone_data(void)
Definition: tz_support.c:713
void tz_set_tz_region_system(const TZ_REGION *tz_region)
Definition: tz_support.c:4650
int tz_conv_tz_time_w_zone_name(const DB_TIME *time_source, const char *source_zone, int len_source, const char *dest_zone, int len_dest, DB_TIME *time_dest)
Definition: tz_support.c:1528
int tz_get_ds_change_julian_date_diff(const int src_julian_date, const TZ_DS_RULE *ds_rule, const int year, int *ds_rule_julian_date, full_date_t *date_diff)
Definition: tz_support.c:2333
int tz_get_first_weekday_around_date(const int year, const int month, const int weekday, const int after_day, const bool before)
Definition: tz_support.c:2085
void tz_get_session_tz_region(TZ_REGION *tz_region)
Definition: tz_support.c:767
int tz_create_session_tzid_for_datetime(const DB_DATETIME *src_dt, bool src_is_utc, TZ_ID *tz_id)
Definition: tz_support.c:1037
void db_date_locale_init(void)
Definition: db_date.c:1531
void THREAD_ENTRY
int tz_create_datetimetz_from_offset(const DB_DATETIME *dt, const int tzh, const int tzm, DB_DATETIMETZ *dt_tz)
Definition: tz_support.c:4068
int tz_datetimetz_fix_zone(const DB_DATETIMETZ *src_dt_tz, DB_DATETIMETZ *dest_dt_tz)
Definition: tz_support.c:1815
DB_BIGINT tz_timestamp_encode_leap_sec_adj(const int year_century, const int year, const int mon, const int day)
Definition: tz_support.c:415
int tz_create_datetimetz(const DB_DATETIME *dt, const char *tz_str, const int tz_size, const TZ_REGION *default_tz_region, DB_DATETIMETZ *dt_tz, const char **end_tz_str)
Definition: tz_support.c:1383
int tz_create_datetimetz_from_utc(const DB_DATETIME *src_dt, const TZ_REGION *dest_region, DB_DATETIMETZ *dest_dt_tz)
Definition: tz_support.c:5104
void tz_get_system_tz_region(TZ_REGION *tz_region)
Definition: tz_support.c:758
int tz_load_with_library_path(TZ_DATA *tzd, const char *timezone_library_path)
Definition: tz_support.c:4937
int tz_str_read_time(const char *str, const char *str_end, bool need_minutes, bool allow_sec60, int *hour, int *min, int *sec, const char **str_next)
Definition: tz_support.c:2186
int tz_create_datetimetz_from_parts(const int m, const int d, const int y, const int h, const int mi, const int s, const int ms, const TZ_ID *tz_id, DB_DATETIMETZ *dt_tz)
Definition: tz_support.c:5138
int tz_get_timezone_offset(const char *tz_str, int tz_size, char *result, DB_DATETIME *utc_datetime)
Definition: tz_support.c:935
DB_BIGINT full_date_t
Definition: tz_support.h:125
int tz_create_session_tzid_for_timestamp(const DB_UTIME *src_ts, TZ_ID *tz_id)
Definition: tz_support.c:1068
int tz_get_best_match_zone(const char *name, int *size)
Definition: tz_support.c:4157
void tz_set_new_timezone_data(const TZ_DATA *data)
Definition: tz_support.c:729
int tz_get_offset_in_mins()
Definition: tz_support.c:5480
const char * tz_get_system_timezone(void)
Definition: tz_support.c:749
#define min(a, b)
void tz_set_data(const TZ_DATA *data)
Definition: tz_support.c:702
int tz_id_to_str(const TZ_ID *tz_id, char *tz_str, const int tz_str_size)
Definition: tz_support.c:1789
TZ_REGION * tz_get_client_tz_region_session(void)
Definition: tz_support.c:4662
int tz_create_timestamptz(const DB_DATE *date, const DB_TIME *time, const char *tz_str, const int tz_size, const TZ_REGION *default_tz_region, DB_TIMESTAMPTZ *ts_tz, const char **end_tz_str)
Definition: tz_support.c:1435
int tz_create_timestamptz_from_offset(const DB_DATE *date, const DB_TIME *time, const int tzh, const int tzm, DB_TIMESTAMPTZ *timestamp_tz)
Definition: tz_support.c:4107
int tz_create_datetimetz_from_ses(const DB_DATETIME *dt, DB_DATETIMETZ *dt_tz)
Definition: tz_support.c:1493
int tz_str_to_region(const char *tz_str, const int tz_str_size, TZ_REGION *tz_region)
Definition: tz_support.c:1286
int tz_explain_tz_id(const TZ_ID *tz_id, char *tzr, const int tzr_size, char *tzdst, const int tzdst_size, int *tzh, int *tzm)
Definition: tz_support.c:3893
int tz_create_session_tzid_for_time(const DB_TIME *src_time, bool src_is_utc, TZ_ID *tz_id)
Definition: tz_support.c:1091
int conv_tz(void *, const void *, DB_TYPE)
Definition: tz_support.c:5209
int64_t DB_BIGINT
Definition: dbtype_def.h:751
void tz_tzid_convert_region_to_offset(TZ_ID *tz_id)
Definition: tz_support.c:5068
int tz_conv_tz_datetime_w_region(const DB_DATETIME *src_dt, const TZ_REGION *src_tz_region, const TZ_REGION *dest_tz_region, DB_DATETIME *dest_dt, TZ_ID *src_tz_id_out, TZ_ID *dest_tz_id_out)
Definition: tz_support.c:3812
const char * tz_get_session_local_timezone(void)
Definition: tz_support.c:739
int tz_create_datetimetz_from_zoneid_and_tzd(const DB_DATETIME *dt, TZ_REGION *default_tz_region, const int zone_id, const char *tzd, const int tzd_len, bool is_time_tz, DB_DATETIMETZ *dt_tz)
Definition: tz_support.c:4212
unsigned int DB_TIME
Definition: dbtype_def.h:754
unsigned int DB_DATE
Definition: dbtype_def.h:771
void tz_unload(void)
Definition: tz_support.c:381
int tz_timestamptz_fix_zone(const DB_TIMESTAMPTZ *src_ts_tz, DB_TIMESTAMPTZ *dest_ts_tz)
Definition: tz_support.c:1843
int tz_timezones_start_scan(THREAD_ENTRY *thread_p, int show_type, DB_VALUE **arg_values, int arg_cnt, void **ptr)
Definition: tz_support.c:4716
int tz_str_read_number(const char *str, const char *str_end, const bool strict, const bool read_sign, int *val, const char **str_next)
Definition: tz_support.c:2130
const TZ_DATA * tz_get_data(void)
Definition: tz_support.c:687
int tz_str_to_seconds(const char *str, const char *str_end, int *sec, const char **str_next, const bool is_offset)
Definition: tz_support.c:2278
int tz_utc_datetimetz_to_local(const DB_DATETIME *dt_utc, const TZ_ID *tz_id, DB_DATETIME *dt_local)
Definition: tz_support.c:1571
const TZ_REGION * tz_get_utc_tz_region(void)
Definition: tz_support.c:828
int tz_check_geographic_tz(const TZ_ID *tz_id)
Definition: tz_support.c:4975
DB_TIMESTAMP DB_UTIME
Definition: dbtype_def.h:761
int check_timezone_compat(const char *client_checksum, const char *server_checksum, const char *client_text, const char *server_text)
Definition: tz_support.c:5043
void tz_timestamp_decode_no_leap_sec(int timestamp, int *yearp, int *monthsp, int *dayp, int *hoursp, int *minutesp, int *secondsp)
Definition: tz_support.c:601
int put_timezone_checksum(char *checksum)
Definition: tz_support.c:5017
int tz_load(void)
Definition: tz_support.c:337