ob_time_convert.cpp 264.0 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#define USING_LOG_PREFIX LIB_TIME

#include "lib/timezone/ob_time_convert.h"
#include "lib/timezone/ob_timezone_info.h"
#include "lib/timezone/ob_oracle_format_models.h"
#include <ctype.h>
#include <math.h>
#include "lib/ob_define.h"
#include "lib/oblog/ob_log.h"
#include "lib/utility/utility.h"
#include "lib/container/ob_se_array.h"
#include "lib/container/ob_bit_set.h"
#include "rpc/obmysql/ob_mysql_util.h"
#include "common/object/ob_object.h"
//#include "lib/timezone/ob_timezone_util.h"

using namespace oceanbase::obmysql;

namespace oceanbase {
namespace common {

X
xy0 已提交
34 35
int check_and_get_tz_info(ObTime &ob_time, const ObTimeConvertCtx &cvrt_ctx, const ObTimeZoneInfo *&tz_info,
    ObTimeZoneInfoPos *&literal_tz_info, ObTZInfoIDPosMap *&tz_id_pos_map);
O
oceanbase-admin 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

ObTimeConverter::ObTimeConverter()
{}

ObTimeConverter::~ObTimeConverter()
{}

const int64_t DT_PART_BASE[DATETIME_PART_CNT] = {100, 12, -1, 24, 60, 60, 1000000};
const int64_t DT_PART_MIN[DATETIME_PART_CNT] = {0, 1, 1, 0, 0, 0, 0};
const int64_t DT_PART_MAX[DATETIME_PART_CNT] = {9999, 12, 31, 23, 59, 59, 1000000};

// the following is for oracle
const int64_t TZ_PART_BASE[DATETIME_PART_CNT] = {100, 12, -1, 24, 60, 60, 1000000000};
const int64_t TZ_PART_MIN[DATETIME_PART_CNT] = {1, 1, 1, 0, 0, 0, 0};
const int64_t TZ_PART_MAX[DATETIME_PART_CNT] = {9999, 12, 31, 23, 59, 59, 1000000000};
const int TZ_PART_ERR[DATETIME_PART_CNT] = {
    /*DT_YEAR*/ OB_ERR_INVALID_YEAR_VALUE,    // ORA-01841: (full) year must be between -4713 and +9999, and not be 0
    /*DT_MON*/ OB_ERR_INVALID_MONTH,          // ORA-01843: not a valid month
    /*DT_MDAY*/ OB_ERR_DAY_OF_MONTH_RANGE,    // ORA-01847: day of month must be between 1 and last day of month
    /*DT_HOUR*/ OB_ERR_INVALID_HOUR24_VALUE,  // ORA-01850: hour must be between 0 and 23
    /*DT_MIN*/ OB_ERR_INVALID_MINUTES_VALUE,  // ORA-01851: minutes must be between 0 and 59
    /*DT_SEC*/ OB_ERR_INVALID_SECONDS_VALUE,  // ORA-01852: seconds must be between 0 and 59
    /*DT_USEC*/ OB_ERR_THE_LEADING_PRECISION_OF_THE_INTERVAL_IS_TOO_SMALL,  // ORA-01873: the leading precision of the
                                                                            // interval is too small
};

static const int8_t DAYS_PER_MON[2][12 + 1] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};

static const int32_t DAYS_UNTIL_MON[2][12 + 1] = {{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
    {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};

/**
 * 3 days after wday 5 is wday 1, so [3][5] = 1.
 * 5 days before wday 2 is wday 4, so [-5][2] = 4.
 * and so on, max wday is 7, min offset is -6, max offset days is 6.
 */
static const int8_t WDAY_OFFSET_ARR[DAYS_PER_WEEK * 2 - 1][DAYS_PER_WEEK + 1] = {{0, 2, 3, 4, 5, 6, 7, 1},
    {0, 3, 4, 5, 6, 7, 1, 2},
    {0, 4, 5, 6, 7, 1, 2, 3},
    {0, 5, 6, 7, 1, 2, 3, 4},
    {0, 6, 7, 1, 2, 3, 4, 5},
    {0, 7, 1, 2, 3, 4, 5, 6},  // offset = -1, wday = 1/2/3/4/5/6/7.
    {0, 1, 2, 3, 4, 5, 6, 7},  // offset =  0, wday = 1/2/3/4/5/6/7.
    {0, 2, 3, 4, 5, 6, 7, 1},  // offset =  1, wday = 1/2/3/4/5/6/7.
    {0, 3, 4, 5, 6, 7, 1, 2},
    {0, 4, 5, 6, 7, 1, 2, 3},
    {0, 5, 6, 7, 1, 2, 3, 4},
    {0, 6, 7, 1, 2, 3, 4, 5},
    {0, 7, 1, 2, 3, 4, 5, 6}};

static const int8_t (*WDAY_OFFSET)[DAYS_PER_WEEK + 1] = &WDAY_OFFSET_ARR[6];

/*
 * if wday of yday 1 is 4, not SUN_BEGIN, not GE_4_BEGIN, yday of fitst day of week 1 is 5, so [4][0][0] = 5.
 * if wday of yday 1 is 2,     SUN_BEGIN,     GE_4_BEGIN, yday of fitst day of week 1 is 5, so [2][1][1] = 1.
 * and so on, max wday is 7, and other two is 1.
 * ps: if the first week is not full week(GE_4_BEGIN), the yday maybe zero or neg, such as 0 means
 *     the last day of prev year, and so on.
 */
static const int8_t YDAY_WEEK1[DAYS_PER_WEEK + 1][2][2] = {
    {{0, 0}, {0, 0}},
    {{1, 1}, {7, 0}},    // wday of day 1 is 1.
    {{7, 0}, {6, -1}},   // 2.
    {{6, -1}, {5, -2}},  // 3.
    {{5, -2}, {4, 4}},   // 4.
    {{4, 4}, {3, 3}},    // 5.
    {{3, 3}, {2, 2}},    // 6.
    {{2, 2}, {1, 1}}     // 7.
};

#define WEEK_MODE_CNT 8
static const ObDTMode WEEK_MODE[WEEK_MODE_CNT] = {DT_WEEK_SUN_BEGIN | DT_WEEK_ZERO_BEGIN,
    DT_WEEK_ZERO_BEGIN | DT_WEEK_GE_4_BEGIN,
    DT_WEEK_SUN_BEGIN,
    DT_WEEK_GE_4_BEGIN,  // ISO-8601 standard week
    DT_WEEK_SUN_BEGIN | DT_WEEK_ZERO_BEGIN | DT_WEEK_GE_4_BEGIN,
    DT_WEEK_ZERO_BEGIN,
    DT_WEEK_SUN_BEGIN | DT_WEEK_GE_4_BEGIN,
    0};

static const int32_t DAYS_PER_YEAR[2] = {DAYS_PER_NYEAR, DAYS_PER_LYEAR};

#define EPOCH_YEAR4 1970
#define EPOCH_YEAR2 70
#define EPOCH_WDAY 4  // 1970-1-1 is thursday.
#define LEAP_YEAR_COUNT(y) ((y) / 4 - (y) / 100 + (y) / 400)
#define IS_LEAP_YEAR(y) ((((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0)) ? 1 : 0)
#define TIME_MAX_VAL (3020399 * 1000000LL)  // 838:59:59 .
#define YEAR_MAX_YEAR 2155
#define YEAR_MIN_YEAR 1901
#define YEAR_BASE_YEAR 1900

#define INT32_MAX_DIGITS_LEN 10
static const int64_t power_of_10[INT32_MAX_DIGITS_LEN] = {
    1L, 10L, 100L, 1000L, 10000L, 100000L, 1000000L, 10000000L, 100000000L, 1000000000L,
    // 2147483647
};

struct ObIntervalIndex {
  int32_t begin_;
  int32_t end_;
  int32_t count_;
  bool calc_with_usecond_;
  // in some cases we can trans the inteval to usecond exactly,
  // in other cases we calc with ob_time, like month, or quarter, or year, because we are not sure
  // how many days should be added exactly in simple way.
};

static const ObIntervalIndex INTERVAL_INDEX[DATE_UNIT_MAX] = {
    {DT_USEC, DT_USEC, 1, true},   // DATE_UNIT_MICROSECOND.
    {DT_SEC, DT_SEC, 1, true},     // DATE_UNIT_SECOND.
    {DT_MIN, DT_MIN, 1, true},     // DATE_UNIT_MINUTE.
    {DT_HOUR, DT_HOUR, 1, true},   // DATE_UNIT_HOUR.
    {DT_MDAY, DT_MDAY, 1, true},   // DATE_UNIT_DAY.
    {DT_MDAY, DT_MDAY, 1, true},   // DATE_UNIT_WEEK.
    {DT_MON, DT_MON, 1, false},    // DATE_UNIT_MONTH.
    {DT_MON, DT_MON, 1, false},    // DATE_UNIT_QUARTER.
    {DT_YEAR, DT_YEAR, 1, false},  // DATE_UNIT_YEAR.
    {DT_SEC, DT_USEC, 2, true},    // DATE_UNIT_SECOND_MICROSECOND.
    {DT_MIN, DT_USEC, 3, true},    // DATE_UNIT_MINUTE_MICROSECOND.
    {DT_MIN, DT_SEC, 2, true},     // DATE_UNIT_MINUTE_SECOND.
    {DT_HOUR, DT_USEC, 4, true},   // DATE_UNIT_HOUR_MICROSECOND.
    {DT_HOUR, DT_SEC, 3, true},    // DATE_UNIT_HOUR_SECOND.
    {DT_HOUR, DT_MIN, 2, true},    // DATE_UNIT_HOUR_MINUTE.
    {DT_MDAY, DT_USEC, 5, true},   // DATE_UNIT_DAY_MICROSECOND.
    {DT_MDAY, DT_SEC, 4, true},    // DATE_UNIT_DAY_SECOND.
    {DT_MDAY, DT_MIN, 3, true},    // DATE_UNIT_DAY_MINUTE.
    {DT_MDAY, DT_HOUR, 2, true},   // DATE_UNIT_DAY_HOUR.
    {DT_YEAR, DT_MON, 2, false},   // DATE_UNIT_YEAR_MONTH.
};

static const ObTimeConstStr MDAY_NAMES[31 + 1] = {{"null", 4},
    {"1st", 3},
    {"2nd", 3},
    {"3rd", 3},
    {"4th", 3},
    {"5th", 3},
    {"6th", 3},
    {"7th", 3},
    {"8th", 3},
    {"9th", 3},
    {"10th", 4},
    {"11th", 4},
    {"12th", 4},
    {"13th", 4},
    {"14th", 4},
    {"15th", 4},
    {"16th", 4},
    {"17th", 4},
    {"18th", 4},
    {"19th", 4},
    {"20th", 4},
    {"21st", 4},
    {"22nd", 4},
    {"23rd", 4},
    {"24th", 4},
    {"25th", 4},
    {"26th", 4},
    {"27th", 4},
    {"28th", 4},
    {"29th", 4},
    {"30th", 4},
    {"31st", 4}};

X
xy0 已提交
201
static const char *DAY_NAME[31 + 1] = {"0th",
O
oceanbase-admin 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    "1st",
    "2nd",
    "3rd",
    "4th",
    "5th",
    "6th",
    "7th",
    "8th",
    "9th",
    "10th",
    "11th",
    "12th",
    "13th",
    "14th",
    "15th",
    "16th",
    "17th",
    "18th",
    "19th",
    "20th",
    "21st",
    "22nd",
    "23rd",
    "24th",
    "25th",
    "26th",
    "27th",
    "28th",
    "29th",
    "30th",
    "31st"};

static const ObTimeConstStr WDAY_NAMES[DAYS_PER_WEEK + 1] = {{"null", 4},
    {"Monday", 6},
    {"Tuesday", 7},
    {"Wednesday", 9},
    {"Thursday", 8},
    {"Friday", 6},
    {"Saturday", 8},
    {"Sunday", 6}};

static const int32_t MAX_WDAY_NAME_LENGTH = ObTimeConverter::calc_max_name_length(WDAY_NAMES, DAYS_PER_WEEK);
static const int32_t MAX_WDAY_NAME_LENGTH_ORACLE = 36;

static const ObTimeConstStr WDAY_ABBR_NAMES[DAYS_PER_WEEK + 1] = {
    {"null", 4}, {"Mon", 3}, {"Tue", 3}, {"Wed", 3}, {"Thu", 3}, {"Fri", 3}, {"Sat", 3}, {"Sun", 3}};

static const int32_t MAX_WDAY_ABBR_NAME_LENGTH = ObTimeConverter::calc_max_name_length(WDAY_ABBR_NAMES, DAYS_PER_WEEK);
static const int32_t MAX_WDAY_ABBR_NAME_LENGTH_ORACLE = 12;

static const ObTimeConstStr MON_NAMES[12 + 1] = {{"null", 4},
    {"January", 7},
    {"February", 8},
    {"March", 5},
    {"April", 5},
    {"May", 3},
    {"June", 4},
    {"July", 4},
    {"August", 6},
    {"September", 9},
    {"October", 7},
    {"November", 8},
    {"December", 8}};

static const int32_t MAX_MON_NAME_LENGTH = ObTimeConverter::calc_max_name_length(MON_NAMES, 12);
static const int32_t MAX_MON_NAME_LENGTH_ORACLE = 36;

static const ObTimeConstStr MON_ABBR_NAMES[12 + 1] = {{"null", 4},
    {"Jan", 3},
    {"Feb", 3},
    {"Mar", 3},
    {"Apr", 3},
    {"May", 3},
    {"Jun", 3},
    {"Jul", 3},
    {"Aug", 3},
    {"Sep", 3},
    {"Oct", 3},
    {"Nov", 3},
    {"Dec", 3}};

static const int32_t MAX_MON_ABBR_NAME_LENGTH = ObTimeConverter::calc_max_name_length(MON_ABBR_NAMES, 12);
static const int32_t MAX_MON_ABBR_NAME_LENGTH_ORACLE = 12;

static const int64_t ELEMENTFLAG_MAX_LEN[] = {
    /*AD*/ 2,
    /*AD2*/ 4,
    /*BC*/ 2,
    /*BC2*/ 4,
    /*CC*/ 2,
    /*SCC*/ 3,
    /*D*/ 1,
    /*Day*/ MAX_WDAY_NAME_LENGTH_ORACLE,
    /*DD*/ 2,
    /*DDD*/ 3,
    /*DY*/ MAX_WDAY_ABBR_NAME_LENGTH_ORACLE,
    /*FF1*/ 9,
    /*FF2*/ 9,
    /*FF3*/ 9,
    /*FF4*/ 9,
    /*FF5*/ 9,
    /*FF6*/ 9,
    /*FF7*/ 9,
    /*FF8*/ 9,
    /*FF9*/ 9,
    /*FF*/ 9,
    /*HH*/ 2,
    /*HH24*/ 2,
    /*HH12*/ 2,
    /*IW*/ 2,
    /*I*/ 1,
    /*IY*/ 2,
    /*IYY*/ 3,
    /*IYYY*/ 4,
    /*MI*/ 2,
    /*MM*/ 2,
    /*MONTH*/ MAX_MON_NAME_LENGTH_ORACLE,
    /*MON*/ MAX_MON_ABBR_NAME_LENGTH_ORACLE,
    /*AM*/ 2,
    /*AM2*/ 3,
    /*PM*/ 2,
    /*PM2*/ 4,
    /*Q*/ 1,
    /*RR*/ 2,
    /*RRRR*/ 4,
    /*SS*/ 2,
    /*SSSSS*/ 5,
    /*WW*/ 2,
    /*W*/ 1,
    /*YGYYY*/ 5,
    /*YEAR*/ 0,
    /*SYEAR*/ 0,
    /*YYYY*/ 4,
    /*SYYYY*/ 5,
    /*YYY*/ 3,
    /*YY*/ 2,
    /*Y*/ 1,
    /*DS*/ 10,
    /*DL*/ MAX_WDAY_NAME_LENGTH_ORACLE + 2 + MAX_MON_NAME_LENGTH_ORACLE + 1 + 2 + 3,
    /*TZH*/ 3,
    /*TZM*/ 2,
    /*TZD*/ OB_MAX_TZ_ABBR_LEN,
    /*TZR*/ MIN(OB_MAX_TZ_NAME_LEN, 6),
    /*X*/ 1,
    /*J*/ 7};

#define START_WITH_SUNDAY 0x04
#define WEEK_FIRST_WEEKDAY 0x02
#define INCLUDE_CRITICAL_WEEK 0x01

const int64_t SECS_PER_HOUR = (SECS_PER_MIN * MINS_PER_HOUR);
const int64_t SECS_PER_DAY = (SECS_PER_HOUR * HOURS_PER_DAY);
const int64_t USECS_PER_DAY = (USECS_PER_SEC * SECS_PER_DAY);
const int64_t NSECS_PER_DAY = (NSECS_PER_SEC * SECS_PER_DAY);
const int64_t USECS_PER_MIN = (USECS_PER_SEC * SECS_PER_MIN);

const ObString ObTimeConverter::DEFAULT_NLS_DATE_FORMAT("DD-MON-RR");
const ObString ObTimeConverter::DEFAULT_NLS_TIMESTAMP_FORMAT("DD-MON-RR HH.MI.SSXFF AM");
const ObString ObTimeConverter::DEFAULT_NLS_TIMESTAMP_TZ_FORMAT("DD-MON-RR HH.MI.SSXFF AM TZR");
const ObString ObTimeConverter::COMPAT_OLD_NLS_DATE_FORMAT("YYYY-MM-DD HH24:MI:SS");
const ObString ObTimeConverter::COMPAT_OLD_NLS_TIMESTAMP_FORMAT("YYYY-MM-DD HH24:MI:SS.FF");
const ObString ObTimeConverter::COMPAT_OLD_NLS_TIMESTAMP_TZ_FORMAT("YYYY-MM-DD HH24:MI:SS.FF TZR TZD");

const ObOracleTimeLimiter ObIntervalLimit::YEAR = {0,
    static_cast<int32_t>(power_of_10[9] - 1),
    OB_ERR_INTERVAL_INVALID};  // ORA-01873: the leading precision of the interval is too small
const ObOracleTimeLimiter ObIntervalLimit::MONTH = {0, 11, OB_ERR_INVALID_MONTH};
const ObOracleTimeLimiter ObIntervalLimit::DAY = {0,
    static_cast<int32_t>(power_of_10[9] - 1),
    OB_ERR_INTERVAL_INVALID};  // ORA-01873: the leading precision of the interval is too small
const ObOracleTimeLimiter ObIntervalLimit::HOUR = {0, 23, OB_ERR_INTERVAL_INVALID};
const ObOracleTimeLimiter ObIntervalLimit::MINUTE = {0, 59, OB_ERR_INTERVAL_INVALID};
const ObOracleTimeLimiter ObIntervalLimit::SECOND = {0, 59, OB_ERR_INTERVAL_INVALID};
const ObOracleTimeLimiter ObIntervalLimit::FRACTIONAL_SECOND = {
    0, static_cast<int32_t>(power_of_10[9] - 1), OB_ERR_INTERVAL_INVALID};

X
xy0 已提交
378
int ObTime::set_tz_name(const ObString &tz_name)
O
oceanbase-admin 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(tz_name.empty()) || OB_UNLIKELY(tz_name.length() >= OB_MAX_TZ_NAME_LEN)) {
    ret = OB_INVALID_DATE_FORMAT;
    LOG_WARN("invalid tz_name", "length", tz_name.length(), "expect_len", OB_MAX_TZ_NAME_LEN, K(ret));
  } else {
    MEMCPY(tz_name_, tz_name.ptr(), tz_name.length());
    tz_name_[tz_name.length()] = '\0';
    is_tz_name_valid_ = true;
  }

  if (OB_FAIL(ret)) {
    tz_name_[0] = '\0';
    is_tz_name_valid_ = false;
  }
  return ret;
}

X
xy0 已提交
397
int ObTime::set_tzd_abbr(const ObString &tzd_abbr)
O
oceanbase-admin 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(tzd_abbr.empty()) || OB_UNLIKELY(tzd_abbr.length() >= OB_MAX_TZ_ABBR_LEN)) {
    ret = OB_INVALID_DATE_FORMAT;
    LOG_WARN("invalid tz_name", "length", tzd_abbr.length(), "expect_len", OB_MAX_TZ_ABBR_LEN, K(ret));
  } else {
    MEMCPY(tzd_abbr_, tzd_abbr.ptr(), tzd_abbr.length());
    tzd_abbr_[tzd_abbr.length()] = '\0';
  }

  if (OB_FAIL(ret)) {
    tzd_abbr_[0] = '\0';
  }
  return ret;
}

N
nroskill 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
DEF_TO_STRING(ObTime)
{
  int64_t pos = 0;
  J_OBJ_START();
  J_KV(K(mode_),
      "parts",
      ObArrayWrap<int32_t>(parts_, TOTAL_PART_CNT),
      "tz_name",
      ObString(OB_MAX_TZ_NAME_LEN, tz_name_),
      "tzd_abbr",
      ObString(OB_MAX_TZ_ABBR_LEN, tzd_abbr_),
      K_(time_zone_id),
      K_(transition_type_id),
      K_(is_tz_name_valid));
  J_OBJ_END();
  return pos;
}

O
oceanbase-admin 已提交
432 433 434
////////////////////////////////
// int / double / string -> datetime / date / time / year.
int ObTimeConverter::int_to_datetime(
X
xy0 已提交
435
    int64_t int_part, int64_t dec_part, const ObTimeConvertCtx &cvrt_ctx, int64_t &value)
O
oceanbase-admin 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
{
  int ret = OB_SUCCESS;
  dec_part = (dec_part + 500) / 1000;
  if (0 == int_part) {
    value = ZERO_DATETIME;
  } else {
    ObTime ob_time(DT_TYPE_DATETIME);
    if (OB_FAIL(int_to_ob_time_with_date(int_part, ob_time))) {
      LOG_WARN("failed to convert integer to datetime", K(ret));
    } else if (OB_FAIL(ob_time_to_datetime(ob_time, cvrt_ctx, value))) {
      LOG_WARN("failed to convert datetime to seconds", K(ret));
    }
  }
  value += dec_part;
  if (OB_SUCC(ret) && !is_valid_datetime(value)) {
    ret = OB_DATETIME_FUNCTION_OVERFLOW;
    LOG_WARN("datetime filed overflow", K(ret), K(value));
  }
  return ret;
}

X
xy0 已提交
457
int ObTimeConverter::int_to_date(int64_t int64, int32_t &value)
O
oceanbase-admin 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
{
  int ret = OB_SUCCESS;
  if (0 == int64) {
    value = ZERO_DATE;
  } else {
    ObTime ob_time(DT_TYPE_DATE);
    if (OB_FAIL(int_to_ob_time_with_date(int64, ob_time))) {
      LOG_WARN("failed to convert integer to date", K(ret));
    } else {
      value = ob_time.parts_[DT_DATE];  // value = int32_min when all parts are zero
    }
  }
  return ret;
}

X
xy0 已提交
473
int ObTimeConverter::int_to_time(int64_t int64, int64_t &value)
O
oceanbase-admin 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
{
  int ret = OB_SUCCESS;
  ObTime ob_time(DT_TYPE_TIME);
  if (OB_FAIL(int_to_ob_time_without_date(int64, ob_time))) {
    LOG_WARN("failed to convert integer to time", K(ret));
  } else {
    value = ob_time_to_time(ob_time);
    ret = time_overflow_trunc(value);
    if (DT_MODE_NEG & ob_time.mode_) {
      value = -value;
    }
  }
  return ret;
}

X
xy0 已提交
489
int ObTimeConverter::int_to_year(int64_t int_val, uint8_t &value)
O
oceanbase-admin 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
{
  int ret = OB_SUCCESS;
  if (0 == int_val) {
    value = ZERO_YEAR;
  } else if (int_val < 0) {
    ret = OB_DATA_OUT_OF_RANGE;
    LOG_WARN("invalid year value", K(ret));
  } else {
    apply_date_year2_rule(int_val);
    if (OB_FAIL(validate_year(int_val))) {
      LOG_WARN("year integer is invalid or out of range", K(ret));
    } else {
      value = static_cast<uint8_t>(int_val - YEAR_BASE_YEAR);
    }
  }
  return ret;
}

int ObTimeConverter::str_to_datetime(
X
xy0 已提交
509
    const ObString &str, const ObTimeConvertCtx &cvrt_ctx, int64_t &value, int16_t *scale)
O
oceanbase-admin 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
{
  int ret = OB_SUCCESS;
  ObTime ob_time(DT_TYPE_DATETIME);
  if (OB_FAIL(str_to_ob_time_with_date(str, ob_time, scale))) {
    LOG_WARN("failed to convert string to datetime", K(ret));
  } else if (OB_FAIL(validate_datetime(ob_time))) {
    LOG_WARN("invalid datetime", K(ret));
  } else if (!cvrt_ctx.is_timestamp_ && ob_time.is_tz_name_valid_) {
    // only enable time zone data type can has tz name and tz addr
    LOG_WARN("DATETIME should not has time zone attr", K(ret));
    // for MySql non-strict sql_mode: still do the convert but without tz info
    ob_time.is_tz_name_valid_ = false;
    if (OB_FAIL(ob_time_to_datetime(ob_time, cvrt_ctx, value))) {
      LOG_WARN("failed to convert datetime to seconds", K(ret), K(ob_time), K(value));
    } else {
      ret = OB_ERR_UNEXPECTED_TZ_TRANSITION;
    }
  } else if (OB_FAIL(ob_time_to_datetime(ob_time, cvrt_ctx, value))) {
    LOG_WARN("failed to convert ob time to datetime", K(ret));
  } else {
    LOG_DEBUG("succ to str_to_datetime", K(str), K(ob_time), K(cvrt_ctx.is_timestamp_), K(value));
  }
  return ret;
}

/**
 * @brief validate oracle literal DATE'' and cast to ObDateTime
 * @param in:   str       input string
 * @param in:   cvrt_ctx
 * @param out:  value     oracle date result value
 */
int ObTimeConverter::literal_date_validate_oracle(
X
xy0 已提交
542
    const ObString &str, const ObTimeConvertCtx &cvrt_ctx, ObDateTime &value)
O
oceanbase-admin 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  ObScale scale = 0;  // not used

  if (OB_FAIL(str_to_ob_time_oracle_strict(str, cvrt_ctx, false, ob_time, scale))) {
    LOG_WARN("failed to convert string to date oracle", K(ret));
  } else if (OB_FAIL(ob_time_to_datetime(ob_time, cvrt_ctx, value))) {
    LOG_WARN("failed to convert ob time to date oracle", K(ret));
  } else {
    LOG_DEBUG("succ to validate oracle literal date", K(str), K(ob_time), K(value), K(scale), K(lbt()));
  }
  return ret;
}

/**
 * @brief validate oracle literal TIMESTAMP'' and cast to ObOTimestampData
 * @param in:     str       input string
 * @param in:     cvrt_ctx
 * @param in:     obj_type  input ytpe, includes ObTimestampTZType ObTimestampNanoType ObTimestampLTZType
 * @param out:    value     ObOTimestampData result value
 */
int ObTimeConverter::literal_timestamp_validate_oracle(
X
xy0 已提交
566
    const ObString &str, const ObTimeConvertCtx &cvrt_ctx, ObObjType &obj_type, ObOTimestampData &value)
O
oceanbase-admin 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
{
  int ret = OB_SUCCESS;
  value.reset();
  ObTime ob_time;
  ObScale scale = 0;  // not used

  if (OB_FAIL(str_to_ob_time_oracle_strict(str, cvrt_ctx, true, ob_time, scale))) {
    LOG_WARN("failed to convert string to datetime", K(ret));
  } else {
    obj_type = HAS_TYPE_TIMEZONE(ob_time.mode_) ? ObTimestampTZType : ObTimestampNanoType;
    if (OB_FAIL(ob_time_to_utc(obj_type, cvrt_ctx, ob_time))) {
      LOG_WARN("failed to convert ob_time to utc", K(ret));
    } else if (OB_FAIL(ob_time_to_otimestamp(ob_time, value))) {
      LOG_WARN("failed to convert obtime to timestamp_tz", K(ret));
    } else {
      LOG_DEBUG("succ to validate oracle literal timestamp", K(obj_type), K(str), K(ob_time), K(value), K(lbt()));
    }
  }
  return ret;
}

int ObTimeConverter::literal_interval_ym_validate_oracle(
X
xy0 已提交
589
    const ObString &str, ObIntervalYMValue &value, ObScale &scale, ObDateUnitType part_begin, ObDateUnitType part_end)
O
oceanbase-admin 已提交
590 591 592 593 594
{
  return str_to_interval_ym(str, value, scale, part_begin, part_end);
}

int ObTimeConverter::literal_interval_ds_validate_oracle(
X
xy0 已提交
595
    const ObString &str, ObIntervalDSValue &value, ObScale &scale, ObDateUnitType part_begin, ObDateUnitType part_end)
O
oceanbase-admin 已提交
596 597 598 599 600 601 602 603 604 605 606
{
  return str_to_interval_ds(str, value, scale, part_begin, part_end);
}

/**
 * @brief cast str to oracle date
 * @param in:     str       input string
 * @param in:     cvrt_ctx
 * @param out:    value     oracle DATE result
 * @return
 */
X
xy0 已提交
607
int ObTimeConverter::str_to_date_oracle(const ObString &str, const ObTimeConvertCtx &cvrt_ctx, ObDateTime &value)
O
oceanbase-admin 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  ObDateTime result_value = 0;
  ObScale scale = 0;  // not used
  if (OB_FAIL(str_to_ob_time_oracle_dfm(str, cvrt_ctx, ObDateTimeType, ob_time, scale))) {
    LOG_WARN("failed to convert str to ob_time", K(str), K(cvrt_ctx.oracle_nls_format_));
  } else if (OB_FAIL(ob_time_to_datetime(ob_time, cvrt_ctx, result_value))) {
    LOG_WARN("convert ob_time to datetime failed", K(ret), K(ob_time));
  } else {
    value = result_value;
  }
  return ret;
}

/**
 * @brief cast str to oracle timestamp(3 possible types)
 * @param in:     str           input string
 * @param in:     cvrt_ctx
 * @param in:     target_type   target types, includes ObTimestampTZType ObTimestampNanoType ObTimestampLTZType
 * @param out:    value         result ObOTimestampData
 * @param out:    scale         scale of fractional seconds
 * @return
 */
X
xy0 已提交
632 633
int ObTimeConverter::str_to_otimestamp(const ObString &str, const ObTimeConvertCtx &cvrt_ctx,
    const ObObjType target_type, ObOTimestampData &value, ObScale &scale)
O
oceanbase-admin 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
{
  int ret = OB_SUCCESS;
  value.reset();

  // NOTE::current format is fixed like "2012-12-02 12:00:00.123456".
  //      it is not enough for oracle when format variables supported.
  // UPDATE: complex format has supported.
  if (OB_UNLIKELY(!ob_is_otimestamp_type(target_type))) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("it is not otimestamp type", K(target_type), K(ret));
  } else if (str.empty()) {
    value.set_null_value();
    scale = OB_MAX_TIMESTAMP_TZ_PRECISION;
    LOG_DEBUG("succ to convert null str to otimestamp", K(target_type), K(str), K(value), K(scale), K(lbt()));
  } else {
    ObTime ob_time;
    if (OB_FAIL(str_to_ob_time_oracle_dfm(str, cvrt_ctx, target_type, ob_time, scale))) {
      LOG_WARN("failed to convert to ob_time by dfm", "format_str", cvrt_ctx.oracle_nls_format_, K(ret));
    } else if (OB_FAIL(ob_time_to_utc(target_type, cvrt_ctx, ob_time))) {
      LOG_WARN("failed to convert ob_time to utc", K(ret));
    } else if (OB_FAIL(ob_time_to_otimestamp(ob_time, value))) {
      LOG_WARN("failed to convert obtime to timestamp_tz", K(ret));
    } else {
      LOG_DEBUG("succ to convert str to otimestamp",
          K(target_type),
          K(str),
          K(ob_time),
          K(value),
          K(scale),
          "format_str",
          cvrt_ctx.oracle_nls_format_,
          K(lbt()));
    }
  }

  return ret;
}

/**
 * @brief calcs tz offset value by time zone name from the input ob_time, fills the result back to ob_time
 * @param in:         cvrt_ctx
 * @param in, out:    ob_time
 * @return
 */
X
xy0 已提交
678
int ObTimeConverter::calc_tz_offset_by_tz_name(const ObTimeConvertCtx &cvrt_ctx, ObTime &ob_time)
O
oceanbase-admin 已提交
679 680 681
{
  int ret = OB_SUCCESS;
  int64_t usec = ob_time.parts_[DT_DATE] * USECS_PER_DAY + ob_time_to_time(ob_time);
X
xy0 已提交
682 683 684
  const ObTimeZoneInfo *tz_info = NULL;
  ObTimeZoneInfoPos *literal_tz_info = NULL;
  ObTZInfoIDPosMap *tz_id_pos_map = NULL;
O
oceanbase-admin 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
  int32_t tz_id = OB_INVALID_INDEX;
  int32_t tran_type_id = OB_INVALID_INDEX;
  int32_t offset_min = 0;
  if (OB_FAIL(check_and_get_tz_info(ob_time, cvrt_ctx, tz_info, literal_tz_info, tz_id_pos_map))) {
    LOG_WARN("fail to check time zone info", K(ob_time));
  } else if (OB_ISNULL(tz_info)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("tz_info shoule not be null", K(ret));
  } else if (OB_FAIL(
                 sub_timezone_offset(*tz_info, ob_time.get_tzd_abbr_str(), usec, offset_min, tz_id, tran_type_id))) {
    LOG_WARN("failed to adjust value with time zone offset", K(ret));
  } else {
    ob_time.parts_[DT_OFFSET_MIN] = offset_min;
    ob_time.time_zone_id_ = tz_id;
    ob_time.transition_type_id_ = tran_type_id;
  }

  if (NULL != literal_tz_info && NULL != tz_id_pos_map) {
    tz_id_pos_map->revert(literal_tz_info);
    tz_id_pos_map = NULL;
    literal_tz_info = NULL;
  }
  return ret;
}

int ObTimeConverter::get_oracle_err_when_datetime_out_of_range(int64_t part_idx)
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(part_idx >= DATETIME_PART_CNT || part_idx < 0)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("part index is out of range", K(ret), K(part_idx));
  } else {
    ret = TZ_PART_ERR[part_idx];
  }
  return ret;
}

int ObTimeConverter::get_oracle_err_when_datetime_parts_conflict(int64_t part_idx)
{
  int ret = OB_SUCCESS;
  switch (part_idx) {
    case DT_YEAR:
      ret = OB_ERR_UNEXPECTED;  // never goes here for now
      break;
    case DT_MON:
      ret = OB_ERR_MONTH_CONFLICTS_WITH_JULIAN_DATE;  // ORA-01833: month conflicts with Julian date
      break;
    case DT_MDAY:
      ret = OB_ERR_DAY_OF_MONTH_CONFLICTS_WITH_JULIAN_DATE;  // ORA-01834: day of month conflicts with Julian date
      break;
    case DT_HOUR:
      ret = OB_ERR_HOUR_CONFLICTS_WITH_SECONDS_IN_DAY;  // ORA-01836: hour conflicts with seconds in day
      break;
    case DT_MIN:
      ret = OB_ERR_MINUTES_OF_HOUR_CONFLICTS_WITH_SECONDS_IN_DAY;  // ORA-01837: minutes of hour conflicts with seconds
                                                                   // in day
      break;
    case DT_SEC:
      ret = OB_ERR_SECONDS_OF_MINUTE_CONFLICTS_WITH_SECONDS_IN_DAY;  // ORA-01838: seconds of minute conflicts with
                                                                     // seconds in day
      break;
    default:
      ret = OB_ERR_UNEXPECTED;
  }
  return ret;
}

X
xy0 已提交
752
int ObTimeConverter::str_to_tz_offset(const ObTimeConvertCtx &cvrt_ctx, ObTime &ob_time)
O
oceanbase-admin 已提交
753 754 755 756 757 758 759 760 761 762
{
  int ret = OB_SUCCESS;
  if (!ob_time.is_tz_name_valid_) {
    // do nothing
  } else if (OB_FAIL(calc_tz_offset_by_tz_name(cvrt_ctx, ob_time))) {
    LOG_WARN("failed to convert tz name to offset", K(ret), K(ob_time));
  }
  return ret;
}

X
xy0 已提交
763
int ObTimeConverter::ob_time_to_utc(const ObObjType obj_type, const ObTimeConvertCtx &cvrt_ctx, ObTime &ob_time)
O
oceanbase-admin 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
{
  int ret = OB_SUCCESS;
  if (ObTimestampNanoType == obj_type) {
    // ignore offset
  } else if (HAS_TYPE_STORE_UTC(ob_time.mode_)) {
    // has store utc time
  } else {
    int64_t usec = ob_time.parts_[DT_DATE] * USECS_PER_DAY + ob_time_to_time(ob_time);
    const int64_t old_usec = usec;
    if (HAS_TYPE_TIMEZONE(ob_time.mode_)) {
      usec -= (ob_time.parts_[DT_OFFSET_MIN] * USECS_PER_MIN);
    } else {
      int32_t offset_min = 0;
      int32_t tz_id = OB_INVALID_INDEX;
      int32_t tran_type_id = OB_INVALID_INDEX;
      if (OB_ISNULL(cvrt_ctx.tz_info_)) {
        ret = OB_ERR_NULL_VALUE;
        LOG_WARN("tz_info is null", K(ret));
      } else if (OB_FAIL(sub_timezone_offset(*cvrt_ctx.tz_info_, ObString(), usec, offset_min, tz_id, tran_type_id))) {
        LOG_WARN("failed to adjust value with time zone offset", K(ret));
      } else {
        ob_time.parts_[DT_OFFSET_MIN] = offset_min;
        ob_time.time_zone_id_ = tz_id;
        ob_time.transition_type_id_ = tran_type_id;
      }
    }

    if (OB_SUCC(ret)) {
      if (old_usec == usec) {
        // no need convert
      } else {
        const int32_t nanosecond = ob_time.parts_[DT_USEC];
        if (OB_FAIL(usec_to_ob_time(usec, ob_time))) {
          LOG_WARN("failed to usec_to_ob_time", K(ret));
        } else {
          ob_time.parts_[DT_USEC] = nanosecond;
          ob_time.parts_[DT_DATE] = ob_time_to_date(ob_time);
        }
      }
    }

    if (OB_SUCC(ret)) {
      ob_time.mode_ |= DT_TYPE_STORE_UTC;
    }
  }
  return ret;
}

int ObTimeConverter::str_to_datetime_format(
X
xy0 已提交
813
    const ObString &str, const ObString &fmt, const ObTimeConvertCtx &cvrt_ctx, int64_t &value, int16_t *scale)
O
oceanbase-admin 已提交
814 815 816 817 818 819 820 821 822 823 824
{
  int ret = OB_SUCCESS;
  ObTime ob_time(DT_TYPE_DATETIME);
  if (OB_FAIL(str_to_ob_time_format(str, fmt, ob_time, scale))) {
    LOG_WARN("failed to convert string to datetime", K(ret));
  } else if (OB_FAIL(ob_time_to_datetime(ob_time, cvrt_ctx, value))) {
    LOG_WARN("failed to convert datetime to seconds", K(ret));
  }
  return ret;
}

X
xy0 已提交
825
int ObTimeConverter::str_to_date(const ObString &str, int32_t &value)
O
oceanbase-admin 已提交
826 827 828 829 830 831 832 833 834 835 836
{
  int ret = OB_SUCCESS;
  ObTime ob_time(DT_TYPE_DATE);
  if (OB_FAIL(str_to_ob_time_with_date(str, ob_time))) {
    LOG_WARN("failed to convert string to date", K(ret));
  } else {
    value = ob_time.parts_[DT_DATE];
  }
  return ret;
}

X
xy0 已提交
837
int ObTimeConverter::str_to_time(const ObString &str, int64_t &value, int16_t *scale)
O
oceanbase-admin 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
{
  int ret = OB_SUCCESS;
  ObTime ob_time(DT_TYPE_TIME);
  if (OB_FAIL(str_to_ob_time_without_date(str, ob_time, scale))) {
    LOG_WARN("failed to convert string to time", K(ret));
  } else {
    value = ob_time_to_time(ob_time);
    ret = time_overflow_trunc(value);
    if (DT_MODE_NEG & ob_time.mode_) {
      value = -value;
    }
  }
  return ret;
}

X
xy0 已提交
853
int ObTimeConverter::str_to_year(const ObString &str, uint8_t &value)
O
oceanbase-admin 已提交
854 855 856 857 858 859
{
  int ret = OB_SUCCESS;
  if (NULL == str.ptr() || 0 == str.length()) {
    ret = OB_ALLOCATE_MEMORY_FAILED;
    LOG_ERROR("failed to alloc memory", K(ret));
  } else {
X
xy0 已提交
860 861
    const char *pos = str.ptr();
    const char *end = pos + str.length();
O
oceanbase-admin 已提交
862
    ObTimeDigits digits;
S
st0 已提交
863
    for (; pos < end && !isdigit(*pos); ++pos) {}
O
oceanbase-admin 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876 877
    if (OB_FAIL(get_datetime_digits(pos, end, INT32_MAX, digits))) {
      LOG_WARN("failed to get digits from year string", K(ret));
    } else {
      apply_date_year2_rule(digits);
      if (OB_FAIL(validate_year(digits.value_))) {
        LOG_WARN("year integer is invalid or out of range", K(ret));
      } else {
        value = (0 == digits.value_) ? ZERO_YEAR : static_cast<uint8_t>(digits.value_ - YEAR_BASE_YEAR);
      }
    }
  }
  return ret;
}

X
xy0 已提交
878
int ObTimeConverter::str_to_interval(const ObString &str, ObDateUnitType unit_type, int64_t &value)
O
oceanbase-admin 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891 892
{
  int ret = OB_SUCCESS;
  ObInterval ob_interval;
  if (OB_FAIL(str_to_ob_interval(str, unit_type, ob_interval))) {
    LOG_WARN("failed to convert string to ob interval", K(ret));
  } else if (OB_FAIL(ob_interval_to_interval(ob_interval, value))) {
    LOG_WARN("failed to convert ob interval to interval", K(ret));
  }
  return ret;
}

////////////////////////////////
// int / double / uint / string <- datetime / date / time / year.

X
xy0 已提交
893
int ObTimeConverter::datetime_to_int(int64_t value, const ObTimeZoneInfo *tz_info, int64_t &int64)
O
oceanbase-admin 已提交
894 895 896 897 898 899 900 901 902 903 904
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  if (OB_FAIL(datetime_to_ob_time(value, tz_info, ob_time))) {
    LOG_WARN("failed to convert seconds to ob time", K(ret));
  } else {
    int64 = ob_time_to_int(ob_time, DT_TYPE_DATETIME);
  }
  return ret;
}

X
xy0 已提交
905
int ObTimeConverter::datetime_to_double(int64_t value, const ObTimeZoneInfo *tz_info, double &dbl)
O
oceanbase-admin 已提交
906 907 908 909 910 911 912 913 914 915 916 917
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  if (OB_FAIL(datetime_to_ob_time(value, tz_info, ob_time))) {
    LOG_WARN("failed to convert seconds to ob time", K(ret));
  } else {
    dbl = static_cast<double>(ob_time_to_int(ob_time, DT_TYPE_DATETIME)) +
          ob_time.parts_[DT_USEC] / static_cast<double>(USECS_PER_SEC);
  }
  return ret;
}

918
// ObDatetimeType: tz_info = NULL. ObTimestampType: tz_info != NULL.
X
xy0 已提交
919 920
int ObTimeConverter::datetime_to_str(int64_t value, const ObTimeZoneInfo *tz_info, const ObString &nls_format,
    int16_t scale, char *buf, int64_t buf_len, int64_t &pos, bool with_delim)
O
oceanbase-admin 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  round_datetime(scale, value);
  if (OB_FAIL(datetime_to_ob_time(value, tz_info, ob_time))) {
    LOG_WARN("failed to convert seconds to ob time", K(ret));
  } else if (nls_format.empty()) {
    if (OB_FAIL(ob_time_to_str(ob_time, DT_TYPE_DATETIME, scale, buf, buf_len, pos, with_delim))) {
      LOG_WARN("failed to convert ob time to string", K(ret));
    }
  } else {
    if (OB_FAIL(ob_time_to_str_oracle_dfm(ob_time, scale, nls_format, buf, buf_len, pos))) {
      LOG_WARN("failed to convert ob time to string", K(ob_time), K(nls_format), K(buf_len), K(pos), K(ret), K(lbt()));
    } else {
      LOG_DEBUG("succ to datetime_to_str", K(value), K(scale), K(ob_time), K(nls_format), K(lbt()));
    }
  }
  return ret;
}

X
xy0 已提交
941 942
int ObTimeConverter::otimestamp_to_str(const ObOTimestampData &ot_data, const ObDataTypeCastParams &dtc_params,
    const int16_t scale, const ObObjType type, char *buf, int64_t buf_len, int64_t &pos)
O
oceanbase-admin 已提交
943 944 945 946 947
{
  int ret = OB_SUCCESS;
  if (ot_data.is_null_value()) {
    LOG_DEBUG("succ to null otimestamp_to_str", K(ot_data), K(type), K(scale), K(lbt()));
  } else {
X
xy0 已提交
948
    const ObOTimestampData &tmp_ot_data = round_otimestamp(scale, ot_data);
O
oceanbase-admin 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
    const bool store_utc_time = false;
    ObTime ob_time(DT_TYPE_ORACLE_TIMESTAMP);
    const int64_t old_pos = pos;
    const ObString format_str = dtc_params.get_nls_format(type);
    if (OB_FAIL(otimestamp_to_ob_time(type, tmp_ot_data, dtc_params.tz_info_, ob_time, store_utc_time))) {
      LOG_WARN("failed to convert otimestamp to ob time", K(ret), K(lbt()));
    } else if (OB_FAIL(dtc_params.force_use_standard_format_
                           ? ob_time_to_str(ob_time, DT_TYPE_DATETIME, scale, buf, buf_len, pos, true)
                           : ob_time_to_str_oracle_dfm(ob_time, scale, format_str, buf, buf_len, pos))) {
      LOG_WARN("failed to convert ob time to string", K(format_str), K(ob_time), K(ret));
    } else {
      ObString tmp(pos - old_pos, buf + old_pos);
      LOG_DEBUG(
          "succ to otimestamp_to_str", K(ot_data), K(type), K(scale), K(tmp_ot_data), K(tmp), K(format_str), K(lbt()));
    }
  }
  return ret;
}

X
xy0 已提交
968
int ObTimeConverter::date_to_int(int32_t value, int64_t &int64)
O
oceanbase-admin 已提交
969 970 971 972 973 974 975 976 977 978 979
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  if (OB_FAIL(date_to_ob_time(value, ob_time))) {
    LOG_WARN("failed to convert days to ob time", K(ret));
  } else {
    int64 = ob_time_to_int(ob_time, DT_TYPE_DATE);
  }
  return ret;
}

X
xy0 已提交
980
int ObTimeConverter::date_to_str(int32_t value, char *buf, int64_t buf_len, int64_t &pos)
O
oceanbase-admin 已提交
981 982 983 984 985 986 987 988 989 990 991
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  if (OB_FAIL(date_to_ob_time(value, ob_time))) {
    LOG_WARN("failed to convert days to ob time", K(ret));
  } else if (OB_FAIL(ob_time_to_str(ob_time, DT_TYPE_DATE, 0, buf, buf_len, pos, true))) {
    LOG_WARN("failed to convert ob time to string", K(ret));
  }
  return ret;
}

X
xy0 已提交
992
int ObTimeConverter::time_to_int(int64_t value, int64_t &int64)
O
oceanbase-admin 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  if (OB_FAIL(time_to_ob_time(value, ob_time))) {
    LOG_WARN("failed to convert seconds to ob time", K(ret));
  } else {
    int64 = ob_time_to_int(ob_time, DT_TYPE_TIME);
    if (DT_MODE_NEG & ob_time.mode_) {
      int64 = -int64;
    }
  }
  return ret;
}

X
xy0 已提交
1007
int ObTimeConverter::time_to_double(int64_t value, double &dbl)
O
oceanbase-admin 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  if (OB_FAIL(time_to_ob_time(value, ob_time))) {
    LOG_WARN("failed to convert seconds to ob time", K(ret));
  } else {
    dbl = static_cast<double>(ob_time_to_int(ob_time, DT_TYPE_TIME)) +
          ob_time.parts_[DT_USEC] / static_cast<double>(USECS_PER_SEC);
    if (DT_MODE_NEG & ob_time.mode_) {
      dbl = -dbl;
    }
  }
  return ret;
}
X
xy0 已提交
1022 1023
int ObTimeConverter::time_to_datetime(int64_t t_value, int64_t cur_dt_value, const ObTimeZoneInfo *tz_info,
    int64_t &dt_value, const ObObjType expect_type)
O
oceanbase-admin 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
{
  int ret = OB_SUCCESS;
  if (ObTimestampType == expect_type) {
    if (OB_FAIL(add_timezone_offset(tz_info, cur_dt_value))) {
      LOG_WARN("failed to adjust value with time zone offset", K(ret));
    }
    int64_t days = cur_dt_value / USECS_PER_DAY;
    if (days < 0) {
      dt_value = (--days) * USECS_PER_DAY + t_value;
    } else {
      dt_value = days * USECS_PER_DAY + t_value;
    }
    if (OB_FAIL(sub_timezone_offset(tz_info, true, ObString(), dt_value))) {
      LOG_WARN("failed to adjust value with time zone offset", K(ret));
    }
  } else {
    if (OB_FAIL(add_timezone_offset(tz_info, cur_dt_value))) {
      LOG_WARN("failed to adjust value with time zone offset", K(ret));
    } else {
      int64_t days = cur_dt_value / USECS_PER_DAY;
      if (days < 0) {
        dt_value = (--days) * USECS_PER_DAY + t_value;
      } else {
        dt_value = days * USECS_PER_DAY + t_value;
      }
    }
  }
  return ret;
}

int ObTimeConverter::time_to_str(
X
xy0 已提交
1055
    int64_t value, int16_t scale, char *buf, int64_t buf_len, int64_t &pos, bool with_delim)
O
oceanbase-admin 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  round_datetime(scale, value);
  if (OB_FAIL(time_to_ob_time(value, ob_time))) {
    LOG_WARN("failed to convert seconds to ob time", K(ret));
  } else if (OB_FAIL(ob_time_to_str(ob_time, DT_TYPE_TIME, scale, buf, buf_len, pos, with_delim))) {
    LOG_WARN("failed to convert ob time to string", K(ret));
  }
  return ret;
}

X
xy0 已提交
1068
int ObTimeConverter::year_to_int(uint8_t value, int64_t &int64)
O
oceanbase-admin 已提交
1069 1070 1071 1072 1073 1074 1075 1076
{
  int64 = (ZERO_YEAR == value) ? 0 : value + YEAR_BASE_YEAR;
  return OB_SUCCESS;
}

#define YEAR_STR_FMT "%.4d"
#define YEAR_STR_LEN 4

X
xy0 已提交
1077
int ObTimeConverter::year_to_str(uint8_t value, char *buf, int64_t buf_len, int64_t &pos)
O
oceanbase-admin 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(buf) || OB_UNLIKELY(buf_len <= 0 || pos < 0)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("buffer is invalid", K(ret), K(buf), K(buf_len), K(pos));
  } else if (OB_FAIL(databuff_printf(buf, buf_len, pos, YEAR_STR_FMT, (value > 0) ? value + YEAR_BASE_YEAR : 0))) {
    LOG_WARN("failed to print year str", K(ret));
  }
  return ret;
}

////////////////////////////////
// inner cast between datetime, timestamp, date, time, year.

X
xy0 已提交
1092
int ObTimeConverter::datetime_to_timestamp(int64_t dt_value, const ObTimeZoneInfo *tz_info, int64_t &ts_value)
O
oceanbase-admin 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
{
  int ret = OB_SUCCESS;
  ts_value = dt_value;
  bool is_timestamp = (tz_info != NULL);
  if (OB_FAIL(sub_timezone_offset(tz_info, is_timestamp, ObString(), ts_value))) {
    LOG_WARN("failed to adjust value with time zone offset", K(ret));
  }
  return ret;
}

X
xy0 已提交
1103
int ObTimeConverter::timestamp_to_datetime(int64_t ts_value, const ObTimeZoneInfo *tz_info, int64_t &dt_value)
O
oceanbase-admin 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
{
  int ret = OB_SUCCESS;
  dt_value = ts_value;
  if (OB_FAIL(add_timezone_offset(tz_info, dt_value))) {
    LOG_WARN("failed to adjust value with time zone offset", K(ret));
  }
  return ret;
}

// date(local) --> timestamp tz(utc, offset)
//                timestamp ltz(utc)
//                timestamp (local)
int ObTimeConverter::odate_to_otimestamp(
X
xy0 已提交
1117
    int64_t in_value_us, const ObTimeZoneInfo *tz_info, const ObObjType out_type, ObOTimestampData &out_value)
O
oceanbase-admin 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
{
  int ret = OB_SUCCESS;
  if (ZERO_DATETIME == in_value_us) {
    out_value.set_null_value();
    LOG_DEBUG("null odate_to_otimestamp", K(ret), K(in_value_us), K(out_type), K(lbt()));
  } else if (OB_ISNULL(tz_info)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("tz_info is null, it should not happened", K(ret));
  } else if (ObTimestampTZType == out_type) {
    int32_t offset_min = 0;
    int32_t tz_id = OB_INVALID_INDEX;
    int32_t tran_type_id = OB_INVALID_INDEX;
    if (OB_FAIL(sub_timezone_offset(*tz_info, ObString(), in_value_us, offset_min, tz_id, tran_type_id))) {
      LOG_WARN("failed to adjust value with time zone offset", K(ret));
    } else {
      out_value.time_us_ = in_value_us;
      out_value.time_ctx_.tail_nsec_ = 0;
      if (OB_INVALID_INDEX == tz_id) {
        out_value.time_ctx_.store_tz_id_ = 0;
        out_value.time_ctx_.set_offset_min(offset_min);
      } else {
        out_value.time_ctx_.store_tz_id_ = 1;
        out_value.time_ctx_.set_tz_id(tz_id);
        out_value.time_ctx_.set_tran_type_id(tran_type_id);
      }
    }
  } else if (ObTimestampLTZType == out_type) {
    if (OB_FAIL(sub_timezone_offset(tz_info, true, ObString(), in_value_us, true))) {
      LOG_WARN("failed to adjust value with time zone offset", K(ret));
    } else {
      out_value.time_us_ = in_value_us;
      out_value.time_ctx_.tail_nsec_ = 0;
    }
  } else if (ObTimestampNanoType == out_type) {
    out_value.time_us_ = in_value_us;
    out_value.time_ctx_.tail_nsec_ = 0;
  } else {
    ret = OB_ERR_UNEXPECTED;
  }
  return ret;
}

// timestamp tz(utc, offset) --> date(local)
// timestamp ltz(utc)        --> date(local)
// timestamp (local)         --> date(local)
int ObTimeConverter::otimestamp_to_odate(
X
xy0 已提交
1164
    const ObObjType in_type, const ObOTimestampData &in_value, const ObTimeZoneInfo *tz_info, int64_t &out_usec)
O
oceanbase-admin 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
{
  int ret = OB_SUCCESS;
  if (in_value.is_null_value()) {
    out_usec = ZERO_DATETIME;
  } else if (OB_ISNULL(tz_info)) {
    ret = OB_ERR_NULL_VALUE;
    LOG_WARN("tz_info is null", K(ret));
  } else if (ObTimestampTZType == in_type) {
    int32_t offset_min = 0;
    ObTime ob_time(DT_TYPE_ORACLE_TIMESTAMP);
    if (OB_FAIL(extract_offset_from_otimestamp(in_value, tz_info, offset_min, ob_time))) {
      LOG_WARN("failed to extract_offset_from_otimestamp", K(ret));
    } else {
      out_usec = in_value.time_us_ + MIN_TO_USEC(offset_min);
    }
  } else if (ObTimestampLTZType == in_type) {
    out_usec = in_value.time_us_;
    if (OB_FAIL(add_timezone_offset(tz_info, out_usec))) {
      LOG_WARN("failed to adjust value with time zone offset", K(ret));
    }
  } else if (ObTimestampNanoType == in_type) {
    out_usec = in_value.time_us_;
  } else {
    ret = OB_ERR_UNEXPECTED;
  }
  LOG_DEBUG("succ otimestamp_to_odate", K(ret), K(in_type), K(in_value), K(out_usec));
  return ret;
}

X
xy0 已提交
1194 1195
int ObTimeConverter::otimestamp_to_otimestamp(const ObObjType in_type, const ObOTimestampData &in_value,
    const ObTimeZoneInfo *tz_info, const ObObjType out_type, ObOTimestampData &out_value)
O
oceanbase-admin 已提交
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
{
  int ret = OB_SUCCESS;
  out_value.reset();
  if (in_value.is_null_value()) {
    out_value.set_null_value();
  } else if (out_type == in_type) {
    out_value = in_value;
  } else if (ObTimestampLTZType == in_type && ObTimestampTZType == out_type) {
    int64_t in_value_us = in_value.time_us_;
    ObString tz_abbr_str;
    int32_t offset_sec = 0;
    int32_t tz_id = OB_INVALID_INDEX;
    int32_t tran_type_id = OB_INVALID_INDEX;
    if (OB_ISNULL(tz_info)) {
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("tz_info should not be null", K(ret), K(lbt()));
    } else if (OB_FAIL(tz_info->get_timezone_offset(in_value_us, offset_sec, tz_abbr_str, tran_type_id))) {
      LOG_WARN("failed to adjust value with time zone offset", K(ret));
    } else {
      out_value.time_us_ = in_value_us;
      out_value.time_ctx_.tail_nsec_ = in_value.time_ctx_.tail_nsec_;
      if (common::OB_INVALID_TZ_ID == tz_info->get_tz_id()) {
        out_value.time_ctx_.store_tz_id_ = 0;
        out_value.time_ctx_.set_offset_min(static_cast<int32_t>(SEC_TO_MIN(offset_sec)));
      } else {
        out_value.time_ctx_.store_tz_id_ = 1;
        out_value.time_ctx_.set_tz_id(tz_id);
        out_value.time_ctx_.set_tran_type_id(tran_type_id);
      }
    }
  } else if (ObTimestampTZType == in_type && ObTimestampLTZType == out_type) {
    out_value.time_us_ = in_value.time_us_;
    out_value.time_ctx_.tail_nsec_ = in_value.time_ctx_.tail_nsec_;
  }
  LOG_DEBUG("succ otimestamp_to_otimestamp", K(ret), K(in_type), K(in_value), K(out_type), K(out_value));
  return ret;
}

int ObTimeConverter::extract_offset_from_otimestamp(
X
xy0 已提交
1235
    const ObOTimestampData &in_value, const ObTimeZoneInfo *tz_info, int32_t &offset_min, ObTime &ob_time)
O
oceanbase-admin 已提交
1236 1237 1238
{
  int ret = OB_SUCCESS;
  if (in_value.time_ctx_.store_tz_id_) {
X
xy0 已提交
1239 1240
    ObTZInfoMap *tz_info_map = NULL;
    ObTimeZoneInfoPos *literal_tz_info = NULL;
O
oceanbase-admin 已提交
1241 1242 1243 1244 1245 1246
    ObString tz_name_str;
    ObString tz_abbr_str;
    int32_t offset_sec = 0;
    if (OB_ISNULL(tz_info)) {
      ret = OB_ERR_NULL_VALUE;
      LOG_WARN("tz_info is null", K(ret));
X
xy0 已提交
1247
    } else if (OB_ISNULL(tz_info_map = const_cast<ObTZInfoMap *>(tz_info->get_tz_info_map()))) {
O
oceanbase-admin 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("tz_info_map is NULL", K(ret));
    } else if (OB_FAIL(tz_info_map->get_tz_info_by_id(in_value.time_ctx_.tz_id_, literal_tz_info))) {
      LOG_WARN("fail to get_tz_info_by_id", "tz_id", in_value.time_ctx_.tz_id_, K(ret));
      ret = OB_ERR_INVALID_TIMEZONE_REGION_ID;
    } else if (OB_FAIL(
                   literal_tz_info->get_timezone_offset(in_value.time_ctx_.tran_type_id_, tz_abbr_str, offset_sec))) {
      LOG_WARN("fail to get_timezone_offset", K(in_value), K(ret));
      ret = OB_ERR_INVALID_TIMEZONE_REGION_ID;
    } else if (OB_FAIL(literal_tz_info->get_tz_name(tz_name_str))) {
      LOG_WARN("fail to get_tz_name", K(tz_name_str), K(ret));
    } else if (OB_FAIL(ob_time.set_tz_name(tz_name_str))) {
      LOG_WARN("fail to set_tz_name", K(tz_name_str), K(ret));
    } else if (OB_FAIL(ob_time.set_tzd_abbr(tz_abbr_str))) {
      LOG_WARN("fail to set_tz_abbr", K(tz_abbr_str), K(ret));
    } else {
      offset_min = static_cast<int32_t>(SEC_TO_MIN(offset_sec));
      ob_time.time_zone_id_ = in_value.time_ctx_.tz_id_;
      ob_time.transition_type_id_ = in_value.time_ctx_.tran_type_id_;
      ob_time.parts_[DT_OFFSET_MIN] = offset_min;
    }
    LOG_DEBUG("extract_offset_from_otimestamp", K(ob_time), K(offset_min), K(offset_sec), K(ret));

    if (NULL != tz_info_map && NULL != literal_tz_info) {
      tz_info_map->free_tz_info_pos(literal_tz_info);
    }
  } else {
    offset_min = in_value.time_ctx_.get_offset_min();
    ob_time.parts_[DT_OFFSET_MIN] = offset_min;
  }
  LOG_DEBUG("extract_offset_from_otimestamp", K(ob_time), K(offset_min), K(ret));
  return ret;
}

X
xy0 已提交
1282
int ObTimeConverter::datetime_to_date(int64_t dt_value, const ObTimeZoneInfo *tz_info, int32_t &d_value)
O
oceanbase-admin 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
{
  int ret = OB_SUCCESS;
  if (ZERO_DATETIME == dt_value) {
    d_value = ZERO_DATE;
  } else if (OB_FAIL(add_timezone_offset(tz_info, dt_value))) {
    LOG_WARN("failed to adjust value with time zone offset", K(ret));
  } else {
    d_value = static_cast<int32_t>(dt_value / USECS_PER_DAY);
    if ((dt_value % USECS_PER_DAY) < 0) {
      --d_value;
    }
  }
  return ret;
}

X
xy0 已提交
1298
int ObTimeConverter::datetime_to_time(int64_t dt_value, const ObTimeZoneInfo *tz_info, int64_t &t_value)
O
oceanbase-admin 已提交
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(add_timezone_offset(tz_info, dt_value))) {
    LOG_WARN("failed to adjust value with time zone offset", K(ret));
  } else {
    t_value = dt_value % USECS_PER_DAY;
    if (t_value < 0) {
      t_value += USECS_PER_DAY;
    }
  }
  return ret;
}

X
xy0 已提交
1312
int ObTimeConverter::datetime_to_year(int64_t dt_value, const ObTimeZoneInfo *tz_info, uint8_t &y_value)
O
oceanbase-admin 已提交
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  if (ZERO_DATETIME == dt_value) {
    y_value = ZERO_YEAR;
  } else if (OB_FAIL(datetime_to_ob_time(dt_value, tz_info, ob_time))) {
    LOG_WARN("failed to convert datetime to ob time", K(ret));
  } else if (OB_FAIL(validate_year(ob_time.parts_[DT_YEAR]))) {
    LOG_WARN("year integer is invalid or out of range", K(ret));
  } else {
    y_value = static_cast<uint8_t>(ob_time.parts_[DT_YEAR] - YEAR_BASE_YEAR);
  }
  return ret;
}

X
xy0 已提交
1328
int ObTimeConverter::date_to_datetime(int32_t d_value, const ObTimeConvertCtx &cvrt_ctx, int64_t &dt_value)
O
oceanbase-admin 已提交
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  if (ZERO_DATE == d_value) {
    dt_value = ZERO_DATETIME;
  } else if (OB_FAIL(date_to_ob_time(d_value, ob_time))) {
    LOG_WARN("failed to convert date to ob time", K(ret));
  } else if (OB_FAIL(ob_time_to_datetime(ob_time, cvrt_ctx, dt_value))) {
    LOG_WARN("failed to convert ob time to datetime", K(ret));
  }
  return ret;
}

X
xy0 已提交
1342
int ObTimeConverter::date_to_year(int32_t d_value, uint8_t &y_value)
O
oceanbase-admin 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  if (ZERO_DATE == d_value) {
    y_value = ZERO_YEAR;
  } else if (OB_FAIL(date_to_ob_time(d_value, ob_time))) {
    LOG_WARN("failed to convert date to ob time", K(ret));
  } else if (OB_FAIL(validate_year(ob_time.parts_[DT_YEAR]))) {
    LOG_WARN("year integer is invalid or out of range", K(ret));
  } else {
    y_value = static_cast<uint8_t>(ob_time.parts_[DT_YEAR] - YEAR_BASE_YEAR);
  }
  return ret;
}

X
xy0 已提交
1358
int ObTimeConverter::check_leading_precision(const ObTimeDigits &digits)
O
oceanbase-admin 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
{
  int ret = OB_SUCCESS;
  const int64_t oracle_max_leading_precision = 9;
  if (digits.value_ != 0 && digits.len_ > oracle_max_leading_precision) {
    int64_t leading_zero_count = 0;
    for (int64_t i = 0; i < digits.len_ && '0' == digits.ptr_[i]; i++) {
      leading_zero_count++;
    }
    if (leading_zero_count >= oracle_max_leading_precision) {
      ret = OB_ERR_THE_LEADING_PRECISION_OF_THE_INTERVAL_IS_TOO_SMALL;
    }
  }
  return ret;
}

////////////////////////////////
// string -> offset.

#define OFFSET_MIN static_cast<int32_t>(-(12 * MINS_PER_HOUR + 59) * SECS_PER_MIN)         // -12:59 .
#define OFFSET_MAX static_cast<int32_t>(13 * SECS_PER_HOUR)                                // +13:00 .
#define ORACLE_OFFSET_MIN static_cast<int32_t>(-(15 * MINS_PER_HOUR + 59) * SECS_PER_MIN)  // -15:59 .
#define ORACLE_OFFSET_MAX static_cast<int32_t>(15 * SECS_PER_HOUR)                         // +15:00 .
#define ORACLE_OFFSET_MAX_HOUR 15

/* str_to_offset usually return OB_ERR_UNKNOWN_TIME_ZONE when failed.
 * Then str is treated as a position and searched in time_zone_map, such 'Asia/Shanghai'.
 * If search failed, mysql reports OB_ERR_UNKNOWN_TIME_ZONE while oracle may return different error code.
 * Such as OB_ERR_INVALID_TIME_ZONE_HOUR when hour is greater than 15,
 * and OB_ERR_INVALID_TIME_ZONE_MINUTE when minute is greater than 59.
 * ret_more records this error code, and if search time_zone_map failed, we choose whether
 * set ret = ret_more according to compatible mode.
 */
X
xy0 已提交
1391
int ObTimeConverter::str_to_offset(const ObString &str, int32_t &value, int &ret_more, const bool is_oracle_mode,
O
oceanbase-admin 已提交
1392 1393 1394 1395
    const bool need_check_valid /* false */)
{
  int ret = OB_SUCCESS;
  ret_more = OB_SUCCESS;
X
xy0 已提交
1396
  const ObString tmp_str = (is_oracle_mode ? const_cast<ObString &>(str).trim() : str);
O
oceanbase-admin 已提交
1397 1398 1399 1400
  if (OB_ISNULL(tmp_str.ptr()) || OB_UNLIKELY(tmp_str.length() <= 0)) {
    ret = OB_ERR_UNKNOWN_TIME_ZONE;
    LOG_WARN("invalid time zone offset", K(ret), K(str), K(tmp_str));
  } else {
X
xy0 已提交
1401 1402
    const char *pos = tmp_str.ptr();
    const char *end = pos + tmp_str.length();
O
oceanbase-admin 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
    char sign = *pos;
    if (is_oracle_mode) {
      // default is +
      if ('+' != sign && '-' != sign) {
        sign = '+';
      } else {
        pos++;
      }
    } else {
      pos++;
    }

    ObTimeDigits hour;
    ObTimeDigits minute;
    ObTimeDelims colon;
    ObTimeDelims none;
    if (OB_FAIL(get_datetime_digits_delims(pos, end, INT32_MAX, hour, colon))) {
      LOG_WARN("failed to get offset", K(ret), K(str));
    } else if (OB_FAIL(get_datetime_digits_delims(pos, end, INT32_MAX, minute, none))) {
      LOG_WARN("failed to get offset", K(ret), K(str));
    } else if (!('+' == sign || '-' == sign) || 0 == hour.len_ ||
               0 == minute.len_
               // oracle just ignore decimal part and invalid char in the end.
               || !is_single_colon(colon) || (none.len_ > 0 && !is_oracle_mode)) {
      ret = OB_ERR_UNKNOWN_TIME_ZONE;
    } else if (!need_check_valid) {
      /* sometimes no need check validation, such as session deserialization
       * and load time_zone system variable to session.
       * We only need check validation when set sys variable.
       */
      value = static_cast<int32_t>(((hour.value_ * MINS_PER_HOUR) + minute.value_) * SECS_PER_MIN);
      if ('-' == sign) {
        value = -value;
      }
    } else if (is_oracle_mode && OB_FAIL(check_leading_precision(hour))) {
      LOG_WARN("check hour leading precision failed", K(ret), K(hour));
    } else if (is_oracle_mode && OB_FAIL(check_leading_precision(minute))) {
      LOG_WARN("check minute leading precision failed", K(ret), K(minute));
    } else if (OB_UNLIKELY(minute.value_ >= MINS_PER_HOUR || minute.value_ < 0)) {
      ret = OB_ERR_UNKNOWN_TIME_ZONE;
      // Oracle reports hour out of range when both hour and minute out of range.
      ret_more = is_oracle_mode ? (hour.value_ > ORACLE_OFFSET_MAX_HOUR ? OB_ERR_INVALID_TIME_ZONE_HOUR
                                                                        : OB_ERR_INVALID_TIME_ZONE_MINUTE)
                                : ret_more;
    } else {
      value = static_cast<int32_t>(((hour.value_ * MINS_PER_HOUR) + minute.value_) * SECS_PER_MIN);
      if ('-' == sign) {
        value = -value;
      }

      if (is_oracle_mode) {
        if (OB_UNLIKELY(!(ORACLE_OFFSET_MIN <= value && value <= ORACLE_OFFSET_MAX))) {
          ret_more =
              hour.value_ > ORACLE_OFFSET_MAX_HOUR ? OB_ERR_INVALID_TIME_ZONE_HOUR : OB_ERR_INVALID_TIME_ZONE_MINUTE;
          ret = OB_ERR_UNKNOWN_TIME_ZONE;
          LOG_WARN("invalid time zone offset", K(ret), K(minute.value_), K(str));
        }
        LOG_DEBUG("finish str_to_offset", K(str), K(value), K(ret), K(lbt()));
      } else {
        if (OB_UNLIKELY(!(OFFSET_MIN <= value && value <= OFFSET_MAX))) {
          ret_more =
              (minute.value_ >= DT_PART_BASE[DT_MIN] ? OB_ERR_INVALID_TIME_ZONE_MINUTE : OB_ERR_INVALID_TIME_ZONE_HOUR);
          ret = OB_ERR_UNKNOWN_TIME_ZONE;
          LOG_WARN("invalid time zone offset", K(ret), K(minute.value_), K(str));
        }
      }
    }
  }
  LOG_DEBUG("finish str_to_offset", K(ret), K(ret_more), K(str), K(value));
  return ret;
}

////////////////////////////////
// year / month / day / quarter / week / hour / minite / second / microsecond.

X
xy0 已提交
1478
int ObTimeConverter::int_to_week(int64_t int64, int64_t mode, int32_t &value)
O
oceanbase-admin 已提交
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
{
  int ret = OB_SUCCESS;
  ObTime ob_time;
  if (OB_FAIL(int_to_ob_time_with_date(int64, ob_time))) {
    LOG_WARN("failed to convert integer to datetime", K(ret));
  } else {
    value = ob_time_to_week(ob_time, WEEK_MODE[mode % WEEK_MODE_CNT]);
  }
  return ret;
}

////////////////////////////////
// other functions.

X
xy0 已提交
1493
void ObTimeConverter::round_datetime(int16_t scale, int64_t &value)
O
oceanbase-admin 已提交
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
{
  if (0 <= scale && scale < 6) {
    int32_t power_of_precision = static_cast<int32_t>(power_of_10[6 - scale]);
    int32_t usec = static_cast<int32_t>(value % power_of_precision);
    int32_t ceil_diff = (usec < 0) ? (-usec) : (power_of_precision - usec);
    if (ceil_diff <= power_of_precision / 2) {
      value += ceil_diff;
    } else {
      value -= (power_of_precision - ceil_diff);
    }
  }  // others, just return the original value.
}

X
xy0 已提交
1507
ObOTimestampData ObTimeConverter::round_otimestamp(const int16_t scale, const ObOTimestampData &in_ot_data)
O
oceanbase-admin 已提交
1508 1509 1510 1511 1512 1513 1514
{
  ObOTimestampData ret_ot_data = in_ot_data;
  const int16_t MIN_SCALE = 0;
  const int16_t MAX_US_SCALE = 6;
  const int16_t MAX_NS_SCALE = 9;
  if (MIN_SCALE <= scale && scale <= MAX_NS_SCALE) {
    if (scale < MAX_US_SCALE) {
X
xy0 已提交
1515
      round_datetime(scale, *(int64_t *)&ret_ot_data.time_us_);
O
oceanbase-admin 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
      ret_ot_data.time_ctx_.tail_nsec_ = 0;
    } else if (MAX_US_SCALE == scale) {
      if (ret_ot_data.time_ctx_.tail_nsec_ >= power_of_10[MAX_NS_SCALE - MAX_US_SCALE] / 2) {
        ++ret_ot_data.time_us_;
      }
      ret_ot_data.time_ctx_.tail_nsec_ = 0;
    } else if (scale < MAX_NS_SCALE) {
      int32_t power_of_precision =
          static_cast<int32_t>(power_of_10[MAX_NS_SCALE - MAX_US_SCALE - (scale - MAX_US_SCALE)]);
      int32_t residue = ret_ot_data.time_ctx_.tail_nsec_ % power_of_precision;
      if (residue >= power_of_precision / 2) {
        ret_ot_data.time_ctx_.set_tail_nsec(ret_ot_data.time_ctx_.tail_nsec_ + power_of_precision - residue);
      } else {
        ret_ot_data.time_ctx_.set_tail_nsec(ret_ot_data.time_ctx_.tail_nsec_ - residue);
      }
      if (ret_ot_data.time_ctx_.tail_nsec_ >= power_of_10[MAX_NS_SCALE - MAX_US_SCALE]) {
        ++ret_ot_data.time_us_;
        ret_ot_data.time_ctx_.set_tail_nsec(
            ret_ot_data.time_ctx_.tail_nsec_ - static_cast<int32_t>(power_of_10[MAX_NS_SCALE - MAX_US_SCALE]));
      }
    }
  }  // others, just return the original value.
  return ret_ot_data;
}

X
xy0 已提交
1541
int ObTimeConverter::round_interval_ds(const ObScale scale, ObIntervalDSValue &value)
O
oceanbase-admin 已提交
1542 1543 1544 1545 1546 1547 1548 1549
{
  int ret = OB_SUCCESS;
  if (MIN_SCALE_FOR_TEMPORAL <= scale && scale <= MAX_SCALE_FOR_ORACLE_TEMPORAL) {
    ret = value.round_fractional_second(static_cast<int32_t>(power_of_10[MAX_SCALE_FOR_ORACLE_TEMPORAL - scale]));
  }
  return ret;
}

X
xy0 已提交
1550
void ObTimeConverter::trunc_datetime(int16_t scale, int64_t &value)
O
oceanbase-admin 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
{
  if (0 <= scale && scale <= 6) {
    int32_t usec = static_cast<int32_t>(value % power_of_10[6 - scale]);
    value = (usec >= 0 ? (value - usec) : (value - usec - power_of_10[6 - scale]));
  }  // others, just return the original value.
}

////////////////////////////////
// date add / sub / diff.

int ObTimeConverter::date_adjust(
X
xy0 已提交
1562
    const int64_t base_value, const ObString &interval_str, ObDateUnitType unit_type, int64_t &value, bool is_add)
O
oceanbase-admin 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(unit_type < 0 || unit_type >= DATE_UNIT_MAX)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("unit type is invalid", K(ret), K(unit_type));
  } else if (INTERVAL_INDEX[unit_type].calc_with_usecond_) {
    if (OB_FAIL(merge_date_interval(base_value, interval_str, unit_type, value, is_add))) {
      LOG_WARN("failed to merge date and interval", K(ret));
    }
  } else {
    ObTime base_time;
    if (OB_FAIL(datetime_to_ob_time(base_value, NULL, base_time))) {
      LOG_WARN("failed to convert datetime to ob time", K(ret));
    } else if (OB_FAIL(merge_date_interval(base_time, interval_str, unit_type, value, is_add))) {
      LOG_WARN("failed to merge date and interval", K(ret));
    }
  }
  return ret;
}

int ObTimeConverter::date_adjust(
X
xy0 已提交
1584
    const ObString &base_str, const ObString &interval_str, ObDateUnitType unit_type, int64_t &value, bool is_add)
O
oceanbase-admin 已提交
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(unit_type < 0 || unit_type >= DATE_UNIT_MAX)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("unit type is invalid", K(ret), K(unit_type));
  } else if (INTERVAL_INDEX[unit_type].calc_with_usecond_) {
    int64_t base_value = 0;
    ObTimeConvertCtx cvrt_ctx(NULL, false);
    if (OB_FAIL(str_to_datetime(base_str, cvrt_ctx, base_value))) {
      LOG_WARN("failed to convert string to datetime", K(ret));
    } else if (OB_FAIL(merge_date_interval(base_value, interval_str, unit_type, value, is_add))) {
      LOG_WARN("failed to merge date and interval", K(ret));
    }
  } else {
    ObTime base_time;
    if (OB_FAIL(str_to_ob_time_with_date(base_str, base_time))) {
      LOG_WARN("failed to convert string to ob time", K(ret));
    } else if (OB_FAIL(merge_date_interval(base_time, interval_str, unit_type, value, is_add))) {
      LOG_WARN("failed to merge date and interval", K(ret));
    }
  }
  return ret;
}

int ObTimeConverter::merge_date_interval(
X
xy0 已提交
1610
    int64_t base_value, const ObString &interval_str, ObDateUnitType unit_type, int64_t &value, bool is_add)
O
oceanbase-admin 已提交
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
{
  int ret = OB_SUCCESS;
  int64_t interval_value = 0;
  if (OB_FAIL(str_to_interval(interval_str, unit_type, interval_value))) {
    LOG_WARN("failed to convert string to interval", K(ret));
  } else {
    value = base_value + (is_add ? interval_value : -interval_value);
    if (ZERO_DATETIME != value && (value > DATETIME_MAX_VAL || value < DATETIME_MIN_VAL)) {
      ret = OB_INVALID_DATE_VALUE;
      LOG_WARN("invalid date", K(ret), K(value));
    }
  }
  return ret;
}

int ObTimeConverter::merge_date_interval(
X
xy0 已提交
1627
    /*const*/ ObTime &base_time, const ObString &interval_str, ObDateUnitType unit_type, int64_t &value, bool is_add)
O
oceanbase-admin 已提交
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
{
  int ret = OB_SUCCESS;
  ObInterval interval_time;
  if (OB_FAIL(str_to_ob_interval(interval_str, unit_type, interval_time))) {
    LOG_WARN("failed to convert string to ob interval", K(ret));
  } else if (INTERVAL_INDEX[unit_type].end_ > DT_MON) {
    // we use this function only when can't convert ob_interval to useconds exactly,
    // so unit must be year / quarter / month / year_month.
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("unit type is invalid", K(ret), K(unit_type));
  } else {
    int64_t year = 0;
    int64_t month = 0;
    if (is_add == static_cast<bool>(DT_MODE_NEG & interval_time.mode_)) {
      year = static_cast<int64_t>(base_time.parts_[DT_YEAR]) - interval_time.parts_[DT_YEAR];
      month = static_cast<int64_t>(base_time.parts_[DT_MON]) - interval_time.parts_[DT_MON];
    } else {
      year = static_cast<int64_t>(base_time.parts_[DT_YEAR]) + interval_time.parts_[DT_YEAR];
      month = static_cast<int64_t>(base_time.parts_[DT_MON]) + interval_time.parts_[DT_MON];
    }
    if (DT_MON == INTERVAL_INDEX[unit_type].end_) {
      if (month <= 0) {
        // 0 => 12, -1 => 11 ... -11 => 1, -12 => 12, -13 => 11 ... -23 => 1.
        // | borrow = 1                  | borrow = 2                       |
        int32_t borrow = static_cast<int32_t>(-month / MONS_PER_YEAR) + 1;
        year -= borrow;
        month += (MONS_PER_YEAR * borrow);
      } else if (month > MONS_PER_YEAR) {
        // 13 => 1, 14 => 2 ... 24 => 12, 25 => 1, 26 => 2 ... 36 => 12.
        // | carry = 1                  | carry = 2                    |
        int32_t carry = static_cast<int32_t>((month - 1) / MONS_PER_YEAR);
        year += carry;
        month -= (MONS_PER_YEAR * carry);
      }
    }
    ObTime res_time;
    res_time.parts_[DT_YEAR] = static_cast<int32_t>(year);
    res_time.parts_[DT_MON] = static_cast<int32_t>(month);
    res_time.parts_[DT_MDAY] = base_time.parts_[DT_MDAY];
    res_time.parts_[DT_HOUR] = base_time.parts_[DT_HOUR];
    res_time.parts_[DT_MIN] = base_time.parts_[DT_MIN];
    res_time.parts_[DT_SEC] = base_time.parts_[DT_SEC];
    res_time.parts_[DT_USEC] = base_time.parts_[DT_USEC];
    // date_add('2000-2-29', interval '1' year) => 2001-02-28.
    int32_t days = DAYS_PER_MON[IS_LEAP_YEAR(year)][month];
    if (res_time.parts_[DT_MDAY] > days) {
      res_time.parts_[DT_MDAY] = days;
    }
    res_time.parts_[DT_DATE] = ob_time_to_date(res_time);
    ObTimeConvertCtx cvrt_ctx(NULL, false);
    if (OB_FAIL(validate_datetime(res_time))) {
      LOG_WARN("invalid datetime", K(ret));
    } else if (OB_FAIL(ob_time_to_datetime(res_time, cvrt_ctx, value))) {
      LOG_WARN("failed to convert ob time to datetime", K(ret));
    }
  }
  return ret;
}

////////////////////////////////
// int / uint / string -> ObTime / ObInterval <- datetime / date / time.

X
xy0 已提交
1690
int ObTimeConverter::int_to_ob_time_with_date(int64_t int64, ObTime &ob_time, bool is_dayofmonth)
O
oceanbase-admin 已提交
1691 1692
{
  int ret = OB_SUCCESS;
X
xy0 已提交
1693
  int32_t *parts = ob_time.parts_;
O
oceanbase-admin 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
  if (is_dayofmonth && 0 == int64) {
    parts[DT_SEC] = 0;
    parts[DT_MIN] = 0;
    parts[DT_HOUR] = 0;
    parts[DT_MDAY] = 0;
    parts[DT_MON] = 0;
    parts[DT_YEAR] = 0;
  } else if (int64 < power_of_10[2]) {
    ret = OB_INVALID_DATE_VALUE;
    LOG_WARN("datetime integer is out of range", K(ret), K(int64));
  } else if (int64 < power_of_10[8]) {
    // YYYYMMDD.
    parts[DT_MDAY] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_MON] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_YEAR] = static_cast<int32_t>(int64 % power_of_10[4]);
  } else if (int64 / power_of_10[6] < power_of_10[8]) {
    // YYYYMMDDHHMMSS.
    parts[DT_SEC] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_MIN] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_HOUR] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_MDAY] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_MON] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_YEAR] = static_cast<int32_t>(int64 % power_of_10[4]);
  } else {
    ret = OB_INVALID_DATE_VALUE;
    LOG_WARN("datetime integer is out of range", K(ret), K(int64));
  }
  if (OB_SUCC(ret)) {
    apply_date_year2_rule(parts[0]);
    if (OB_FAIL(validate_datetime(ob_time, is_dayofmonth))) {
      LOG_WARN("datetime is invalid or out of range", K(ret), K(int64));
    } else if (ZERO_DATE != parts[DT_DATE]) {
      parts[DT_DATE] = ob_time_to_date(ob_time);
    }
  }
  return ret;
}

X
xy0 已提交
1739
int ObTimeConverter::int_to_ob_time_without_date(int64_t int64, ObTime &ob_time)
O
oceanbase-admin 已提交
1740 1741
{
  int ret = OB_SUCCESS;
X
xy0 已提交
1742
  int32_t *parts = ob_time.parts_;
O
oceanbase-admin 已提交
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
  ObDTMode mode = DT_TYPE_TIME;
  if (int64 < 0) {
    ob_time.mode_ |= DT_MODE_NEG;
    if (INT64_MIN == int64) {
      int64 = INT64_MAX;
    } else {
      int64 = -int64;
    }
  }
  if (int64 / power_of_10[4] < power_of_10[6]) {
    // [H]HHMMSS, like 123:45:56.
    parts[DT_SEC] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_MIN] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_HOUR] = static_cast<int32_t>(int64 % power_of_10[6]);
    if (OB_FAIL(validate_time(ob_time))) {
      LOG_WARN("time integer is invalid", K(ret), K(int64));
    }
  } else if (int64 / power_of_10[6] < power_of_10[8]) {
    // HHHHMMDDHHMMSS.
    mode = DT_TYPE_DATETIME;
    parts[DT_SEC] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_MIN] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_HOUR] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_MDAY] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_MON] = static_cast<int32_t>(int64 % power_of_10[2]);
    int64 /= power_of_10[2];
    parts[DT_YEAR] = static_cast<int32_t>(int64 % power_of_10[4]);
    apply_date_year2_rule(parts[DT_YEAR]);
    if (OB_FAIL(validate_datetime(ob_time))) {
      LOG_WARN("datetime is invalid or out of range", K(ret), K(int64));
    } else if (ZERO_DATE != parts[DT_DATE]) {
      parts[DT_DATE] = ob_time_to_date(ob_time);
    }
  } else {
    parts[DT_HOUR] = TIME_MAX_HOUR + 1;
  }
  UNUSED(mode);
  return ret;
}

// these 2 functions are in .h file.
// int ObTimeConverter::uint_to_ob_time_with_date(int64_t int64, ObTime &ob_time)
// int ObTimeConverter::uint_to_ob_time_without_date(int64_t int64, ObTime &ob_time)

static const int32_t DATETIME_PART_LENS_MAX[] = {INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, 7};
static const int32_t DATETIME_PART_LENS_YEAR4[] = {4, 2, 2, 2, 2, 2, 7};
static const int32_t DATETIME_PART_LENS_YEAR2[] = {2, 2, 2, 2, 2, 2, 7};

static const int32_t TIMESTAMPTZ_PART_LENS_MAX[] = {
    INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, 10};
static const int32_t TIMESTAMPTZ_PART_LENS_YEAR4[] = {4, 2, 2, 2, 2, 2, 10};
static const int32_t TIMESTAMPTZ_PART_LENS_YEAR2[] = {2, 2, 2, 2, 2, 2, 10};

X
xy0 已提交
1802
int ObTimeConverter::get_time_zone(const ObTimeDelims *delims, ObTime &ob_time, const char *end_ptr)
O
oceanbase-admin 已提交
1803 1804 1805 1806 1807 1808 1809
{
  int ret = OB_SUCCESS;
  int64_t i = 0;
  if (OB_ISNULL(delims)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("delimis is NULL", K(ret));
  } else {
X
xy0 已提交
1810 1811
    const char *pos = NULL;
    const char *end = NULL;
O
oceanbase-admin 已提交
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
    if (NULL != delims[DT_SEC].ptr_ && isspace(*delims[DT_SEC].ptr_)) {
      pos = delims[DT_SEC].ptr_;
      end = pos + delims[DT_SEC].len_;
    } else if (NULL != delims[DT_USEC].ptr_ && isspace(*delims[DT_USEC].ptr_)) {
      pos = delims[DT_USEC].ptr_;
      end = pos + delims[DT_USEC].len_;
    }
    // oracle support offset and position
    if (NULL != end_ptr) {
      end = end_ptr;
    }
    if (pos != NULL && end != NULL) {
      // skip space
      for (; pos < end && isspace(*pos); ++pos) {}
      // find time zone name
      for (i = 0; pos < end && i < OB_MAX_TZ_NAME_LEN - 1 && !isspace(*pos); ++pos, ++i) {
        ob_time.tz_name_[i] = static_cast<char>(tolower(*pos));
      }
      ob_time.tz_name_[i] = 0;
      ob_time.is_tz_name_valid_ = (i > 0);  // tz_name is not empty

      // skip space
      for (; pos < end && isspace(*pos); ++pos) {}
      // find time zone region abbr
      for (i = 0; pos < end && i < OB_MAX_TZ_ABBR_LEN - 1 && !isspace(*pos); ++pos, ++i) {
        ob_time.tzd_abbr_[i] = *pos;
      }
      ob_time.tzd_abbr_[i] = 0;
    }
  }
  return ret;
}

X
xy0 已提交
1845
int ObTimeConverter::str_to_digit_with_date(const ObString &str, ObTimeDigits *digits, ObTime &ob_time)
O
oceanbase-admin 已提交
1846 1847 1848 1849 1850 1851
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(str.ptr()) || OB_UNLIKELY(str.length() <= 0) || OB_ISNULL(digits)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid argument", K(ret));
  } else {
X
xy0 已提交
1852 1853 1854
    const char *pos = str.ptr();
    const char *end = pos + str.length();
    const int32_t *expect_lens = NULL;
O
oceanbase-admin 已提交
1855 1856
    ObTimeDelims delims[DATETIME_PART_CNT];
    // find first digit and delimiter.
S
st0 已提交
1857
    for (; pos < end && isspace(*pos); ++pos) {}
O
oceanbase-admin 已提交
1858 1859 1860
    if (!isdigit(*pos)) {
      ret = OB_INVALID_DATE_FORMAT;
    } else {
X
xy0 已提交
1861
      const char *first_digit = pos;
S
st0 已提交
1862
      for (; pos < end && isdigit(*pos); ++pos) {}
X
xy0 已提交
1863
      const char *first_delim = pos;
O
oceanbase-admin 已提交
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
      // year is separated by delimiter, or 4 digits, or 2?
      /*if (HAS_TYPE_ORACLE(ob_time.mode_)) {
        if ('.' != *first_delim && first_delim < end) {
          expect_lens = TIMESTAMPTZ_PART_LENS_MAX;
        } else {
          expect_lens = is_year4(first_delim - first_digit)
                        ? TIMESTAMPTZ_PART_LENS_YEAR4
                        : TIMESTAMPTZ_PART_LENS_YEAR2;
        }
      } else {} */
      if ('.' != *first_delim && first_delim < end) {
        expect_lens = DATETIME_PART_LENS_MAX;
      } else {
        expect_lens = is_year4(first_delim - first_digit) ? DATETIME_PART_LENS_YEAR4 : DATETIME_PART_LENS_YEAR2;
      }

      // parse datetime parts.
      pos = first_digit;
      int64_t part_cnt = DATETIME_PART_CNT;
      /*
      if (lib::is_oracle_mode()) {
        if (HAS_TYPE_ORACLE(ob_time.mode_)) {
          part_cnt = DATETIME_PART_CNT;
        } else if (HAS_TYPE_TIME(ob_time.mode_)) {
          part_cnt = ORACLE_DATE_PART_CNT;
        } else {
          part_cnt = DATE_PART_CNT;
        }
      }
      */
      for (int i = 0; OB_SUCC(ret) && i < part_cnt && pos < end; ++i) {
        if (OB_FAIL(get_datetime_digits_delims(pos, end, expect_lens[i], digits[i], delims[i]))) {
          LOG_WARN("failed to get datetime digits from string", K(ret));
        } else if ((DT_YEAR == i || DT_MON == i) && pos == end) {
          ret = OB_INVALID_DATE_VALUE;
          LOG_WARN("datetime format too short", K(ret), K(str));
        }
      }

      if (OB_SUCC(ret)) {
X
xy0 已提交
1904
        const char *end_ptr = HAS_TYPE_ORACLE(ob_time.mode_) ? end : NULL;
O
oceanbase-admin 已提交
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
        /*if (lib::is_oracle_mode() && !HAS_TYPE_ORACLE(ob_time.mode_) && pos < end) {
          //for date type
          ret = OB_INVALID_DATE_FORMAT_END;
          LOG_WARN("invalid date format", K(pos - first_digit),  K(end - first_digit), K(str.length()));
        } else */
        if (OB_FAIL(get_time_zone(delims, ob_time, end_ptr))) {
          LOG_WARN("fail to get time zone", "delims", ObArrayWrap<ObTimeDelims>(delims, DATETIME_PART_CNT), K(ret));
        }
      }
    }

    // apply all kinds of rules of mysql.
    if (OB_SUCC(ret)) {
      if (OB_FAIL(apply_date_space_rule(delims))) {
        LOG_WARN("invalid datetime string", K(ret), K(str));
      } else {
        if (0 == digits[DT_YEAR].value_ && 0 == digits[DT_MON].value_ && 0 == digits[DT_MDAY].value_) {
          // zero date, do nothing
        } else {
          apply_date_year2_rule(digits[DT_YEAR]);
        }

        // if HAS_TYPE_ORACLE, digits[DT_USEC] store nanosecond in fact
        const int64_t max_precision =
            (HAS_TYPE_ORACLE(ob_time.mode_) ? OB_MAX_TIMESTAMP_TZ_PRECISION : OB_MAX_DATETIME_PRECISION);
        const bool use_strict_check = HAS_TYPE_ORACLE(ob_time.mode_);
        if (OB_FAIL(apply_usecond_delim_rule(delims[DT_SEC], digits[DT_USEC], max_precision, use_strict_check))) {
          LOG_WARN("failed to apply rule", K(use_strict_check), K(ret));
        } else if (!(DT_TYPE_DATE & ob_time.mode_) && (DT_TYPE_TIME & ob_time.mode_)) {
          if (OB_FAIL(apply_datetime_for_time_rule(ob_time, digits, delims))) {
            LOG_WARN("failed to apply rule", K(ret));
          }
        }
      }
    }

    if (OB_SUCC(ret)) {
      for (int i = 0; i < DATETIME_PART_CNT; ++i) {
        ob_time.parts_[i] = digits[i].value_;
      }
    }
  }
  return ret;
}

X
xy0 已提交
1950
int ObTimeConverter::str_to_ob_time_with_date(const ObString &str, ObTime &ob_time, int16_t *scale, bool is_dayofmonth)
O
oceanbase-admin 已提交
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(str.ptr()) || OB_UNLIKELY(str.length() <= 0)) {
    ret = OB_INVALID_DATE_VALUE;
    LOG_WARN("datetime string is invalid", K(ret), K(str));
  } else {
    ObTimeDigits digits[DATETIME_PART_CNT];
    if (OB_FAIL(str_to_digit_with_date(str, digits, ob_time))) {
      LOG_WARN("failed to get digits", K(ret), K(str));
    } else if (OB_FAIL(validate_datetime(ob_time, is_dayofmonth))) {
      // OK, it seems like a valid format, now we need check its value.
J
jg0 已提交
1962
      LOG_WARN("datetime is invalid or out of range", K(ret), K(str), K(ob_time), K(lbt()));
O
oceanbase-admin 已提交
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
    } else {
      ob_time.parts_[DT_DATE] = ob_time_to_date(ob_time);
    }
    if (NULL != scale) {
      // if HAS_TYPE_ORACLE, digits[DT_USEC] store nanosecond
      if (HAS_TYPE_ORACLE(ob_time.mode_)) {
        *scale = static_cast<int16_t>(MIN(digits[DT_USEC].len_, OB_MAX_TIMESTAMP_TZ_PRECISION));
      } else {
        *scale = static_cast<int16_t>(MIN(digits[DT_USEC].len_, OB_MAX_DATETIME_PRECISION));
      }
    }
  }
  return ret;
}

X
xy0 已提交
1978
int ObTimeConverter::str_is_date_format(const ObString &str, bool &date_flag)
O
oceanbase-admin 已提交
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(str.ptr()) || OB_UNLIKELY(str.length() <= 0)) {
    ret = OB_INVALID_DATE_VALUE;
    LOG_WARN("datetime string is invalid", K(ret), K(str));
  } else {
    ObTimeDigits digits[DATETIME_PART_CNT];
    ObTime ob_time(DT_TYPE_DATE);
    if (OB_FAIL(str_to_digit_with_date(str, digits, ob_time))) {
      LOG_WARN("failed to get digits", K(ret), K(str));
    } else {
      // OK, it seems like a valid format, now we need check its value.
      if ((MIN(digits[DT_HOUR].len_, 6)) > 0) {
        date_flag = false;
      } else {  // is date type
        date_flag = true;
      }
    }
  }
  return ret;
}

X
xy0 已提交
2001
int ObTimeConverter::str_to_ob_time_without_date(const ObString &str, ObTime &ob_time, int16_t *scale)
O
oceanbase-admin 已提交
2002 2003 2004 2005 2006 2007 2008 2009 2010
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(str.ptr()) || OB_UNLIKELY(str.length() <= 0)) {
    ret = OB_INVALID_DATE_VALUE;
    LOG_WARN("time string is invalid", K(ret), K(str));
  } else {
    if (NULL != scale) {
      *scale = 0;
    }
X
xy0 已提交
2011 2012
    const char *pos = str.ptr();
    const char *end = pos + str.length();
O
oceanbase-admin 已提交
2013
    // find first digit.
S
st0 已提交
2014
    for (; pos < end && isspace(*pos); ++pos) {}
X
xy0 已提交
2015
    const char *first_digit = pos;
O
oceanbase-admin 已提交
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
    if (!('-' == *pos || isdigit(*pos))) {
      for (int i = 0; OB_SUCC(ret) && i < TOTAL_PART_CNT; ++i) {
        ob_time.parts_[i] = 0;
      }
      ob_time.parts_[DT_DATE] = ZERO_DATE;
      ret = OB_ERR_TRUNCATED_WRONG_VALUE;
      LOG_WARN("time string is invalid", K(ret), K(str));
    } else {
      if ('-' == *pos) {
        ++pos;
      }
      bool has_done = false;
      // maybe it is an whole datetime string.
      if (end - first_digit >= 12) {
        ret = str_to_ob_time_with_date(str, ob_time, scale);
        if ((DT_TYPE_NONE & ob_time.mode_) || OB_INVALID_DATE_FORMAT == ret) {
          ob_time.mode_ &= (~DT_TYPE_NONE);
          for (int i = 0; i < DATETIME_PART_CNT; ++i) {
            ob_time.parts_[i] = 0;
          }
          ret = OB_SUCCESS;
        } else if (OB_FAIL(ret)) {
          LOG_WARN("failed to convert string to datetime", K(ret));
        } else {
          has_done = true;
        }
      }
      // otherwise it is an time string.
      if (OB_SUCC(ret) && !has_done) {
        pos = str.ptr();
        if (is_negative(pos, end)) {
          ob_time.mode_ |= DT_MODE_NEG;
        }
        ObTimeDigits digits;
        ObTimeDelims delims;
        int32_t idx = -1;
        bool has_day = false;
        // first part, maybe day, or hour, or hour and minute and second.
        if (OB_FAIL(get_datetime_digits_delims(pos, end, INT32_MAX, digits, delims))) {
          LOG_WARN("failed to get digits and delims from datetime string", K(ret));
        } else if (is_all_spaces(delims)) {
          // digits are day.
          idx = DT_MDAY;
          ob_time.parts_[idx++] = digits.value_;
          has_day = true;
          // next part.
          if (OB_FAIL(get_datetime_digits_delims(pos, end, INT32_MAX, digits, delims))) {
            LOG_WARN("failed to get digits and delims from datetime string", K(ret));
          }
        }
        if (OB_SUCC(ret)) {
          bool end_with_single_colon = is_space_end_with_single_colon(delims);
          if (has_day || end_with_single_colon) {
            // digits are hour.
            // '11 12:34:56.789' => 276:34:56.789 .
            // '11 12 :34:56.789' => 276:00:00 .
            // '200 : 59 : 59' => 00:02:00 .
            // '200 :59 :59' => 200:59:00 .
            // '200: 59: 59' => 00:02:00 .
            // '200 ::59 :59' => 00:02:00 .
            idx = DT_HOUR;
            ob_time.parts_[idx++] = digits.value_;  // hour.
            // in any case, a single colon will be fine, if there is no day part,
            // spaces end with single colon will be fine too. see cases above.
            if (is_single_colon(delims) || (!has_day && end_with_single_colon)) {
              if (OB_FAIL(get_datetime_digits_delims(pos, end, INT32_MAX, digits, delims))) {
                LOG_WARN("failed to get digits and delims from datetime string", K(ret));
              } else {
                ob_time.parts_[idx++] = digits.value_;  // minute.
                if (is_single_colon(delims)) {
                  if (OB_FAIL(get_datetime_digits_delims(pos, end, INT32_MAX, digits, delims))) {
                    LOG_WARN("failed to get digits and delims from datetime string", K(ret));
                  } else {
                    ob_time.parts_[idx++] = digits.value_;  // second.
                  }
                }
              }
            }
          } else {
            // digits contain hour, minute, second.
            idx = DT_HOUR;
            ob_time.parts_[idx++] = static_cast<int32_t>(digits.value_ / power_of_10[4]);
            digits.value_ %= static_cast<int32_t>(power_of_10[4]);
            ob_time.parts_[idx++] = static_cast<int32_t>(digits.value_ / power_of_10[2]);
            digits.value_ %= static_cast<int32_t>(power_of_10[2]);
            ob_time.parts_[idx++] = digits.value_;
          }
          // usecond.
          if (OB_SUCC(ret) && is_single_dot(delims)) {
            // 7 is used for rounding to 6 digits.
            if (OB_FAIL(get_datetime_digits(pos, end, 7, digits))) {
              LOG_WARN("failed to get digits from datetime string", K(ret));
            } else if (OB_FAIL(normalize_usecond_round(digits, OB_MAX_DATETIME_PRECISION))) {
              LOG_WARN("failed to round usecond", K(ret));
            } else {
              ob_time.parts_[DT_USEC] = digits.value_;  // usecond.
              if (NULL != scale) {
                *scale = static_cast<int16_t>(MIN(digits.len_, 6));
              }
            }
          }
        }
        if (OB_SUCC(ret) && OB_FAIL(validate_time(ob_time))) {
          LOG_WARN("time value is invalid or out of range", K(ret), K(str));
        }
      }
    }
  }
  return ret;
}

X
xy0 已提交
2127 2128 2129 2130 2131
#define GET_YEAR_WEEK_WDAY(len, var, set_state, elem, max, min)             \
  if (OB_SUCC(get_datetime_digits(str_pos, str_end, len, digits))) {        \
    if (digits.value_ < min || digits.value_ > max) {                       \
      ret = OB_INVALID_DATE_VALUE;                                          \
    } else {                                                                \
J
jg0 已提交
2132
      var.elem##_set_state_ = ObYearWeekWdayElems::ElemSetState::set_state; \
X
xy0 已提交
2133 2134
      var.elem##_value_ = digits.value_;                                    \
    }                                                                       \
J
jg0 已提交
2135 2136
  }

X
xy0 已提交
2137
int ObTimeConverter::str_to_ob_time_format(const ObString &str, const ObString &fmt, ObTime &ob_time, int16_t *scale)
O
oceanbase-admin 已提交
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
{
  int ret = OB_SUCCESS;
  //  bool is_minus = false;
  bool only_white_space = true;
  ObHourFlag hour_flag = HOUR_UNUSE;
  if (OB_ISNULL(str.ptr()) || OB_UNLIKELY(str.length() <= 0)) {
    // no error or warning even in strict mode.
    for (int i = 0; OB_SUCC(ret) && i < TOTAL_PART_CNT; ++i) {
      ob_time.parts_[i] = 0;
    }
    ob_time.parts_[DT_DATE] = ZERO_DATE;
  } else if (OB_ISNULL(fmt.ptr()) || fmt.length() <= 0) {
    ret = OB_INVALID_DATE_FORMAT;
    LOG_WARN("datetime format is invalid", K(ret), K(fmt));
  } else {
X
xy0 已提交
2153 2154 2155 2156
    const char *str_pos = str.ptr();
    const char *str_end = str.ptr() + str.length();
    const char *fmt_pos = fmt.ptr();
    const char *fmt_end = fmt.ptr() + fmt.length();
O
oceanbase-admin 已提交
2157 2158 2159
    ObString name;
    ObTimeDigits digits;
    ObTimeDelims delims;
J
jg0 已提交
2160 2161
    // https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
    ObYearWeekWdayElems week_day_elements;
O
oceanbase-admin 已提交
2162 2163 2164 2165
    if (NULL != scale) {
      *scale = 0;
    }
    while (OB_SUCC(ret) && fmt_pos < fmt_end) {
S
st0 已提交
2166
      for (; fmt_pos < fmt_end && isspace(*fmt_pos); ++fmt_pos)
O
oceanbase-admin 已提交
2167
        ;
S
st0 已提交
2168
      for (; str_pos < str_end && isspace(*str_pos); ++str_pos)
O
oceanbase-admin 已提交
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
        ;
      if (fmt_pos == fmt_end || str_pos == str_end) {
        break;
      }
      only_white_space = false;
      if ('%' == *fmt_pos) {
        fmt_pos++;
        if (fmt_pos >= fmt_end) {
          ret = OB_INVALID_DATE_VALUE;
          break;
        }
        switch (*fmt_pos) {
          case 'a': {
X
xy0 已提交
2182
            name.assign_ptr(const_cast<char *>(str_pos), static_cast<int32_t>(str_end - str_pos));
O
oceanbase-admin 已提交
2183 2184
            if (OB_SUCC(get_str_array_idx(name, WDAY_ABBR_NAMES, DAYS_PER_WEEK, ob_time.parts_[DT_WDAY]))) {
              str_pos += WDAY_ABBR_NAMES[ob_time.parts_[DT_WDAY]].len_;
J
jg0 已提交
2185 2186
              week_day_elements.weekday_set_ = true;
              week_day_elements.weekday_value_ = ob_time.parts_[DT_WDAY] % DAYS_PER_WEEK;
O
oceanbase-admin 已提交
2187 2188 2189 2190
            }
            break;
          }
          case 'b': {
X
xy0 已提交
2191
            name.assign_ptr(const_cast<char *>(str_pos), static_cast<int32_t>(str_end - str_pos));
O
oceanbase-admin 已提交
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
            if (OB_SUCC(get_str_array_idx(
                    name, MON_ABBR_NAMES, static_cast<int32_t>(MONS_PER_YEAR), ob_time.parts_[DT_MON]))) {
              str_pos += MON_ABBR_NAMES[ob_time.parts_[DT_MON]].len_;
            }
            break;
          }
          case 'c':
          case 'm': {
            if (OB_SUCC(get_datetime_digits(str_pos, str_end, 2, digits))) {
              ob_time.parts_[DT_MON] = digits.value_;
            }
            break;
          }
          case 'D': {
X
xy0 已提交
2206
            name.assign_ptr(const_cast<char *>(str_pos), static_cast<int32_t>(str_end - str_pos));
O
oceanbase-admin 已提交
2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
            if (OB_SUCC(get_str_array_idx(name, MDAY_NAMES, static_cast<int32_t>(31), ob_time.parts_[DT_MDAY]))) {
              str_pos += MDAY_NAMES[ob_time.parts_[DT_MDAY]].len_;
            }
            break;
          }
          case 'd':
          case 'e': {
            if (OB_SUCC(get_datetime_digits(str_pos, str_end, 2, digits))) {
              ob_time.parts_[DT_MDAY] = digits.value_;
            }
            break;
          }
          case 'f': {
            if (str_pos == str_end || isdigit(*str_pos)) {
              if (OB_SUCC(get_datetime_digits(str_pos, str_end, 6, digits)) &&
                  OB_SUCC(normalize_usecond_trunc(digits, true))) {
                ob_time.parts_[DT_USEC] = digits.value_;
                if (NULL != scale) {
                  *scale = static_cast<int16_t>(MIN(digits.len_, 6));
                }
              }
            } else {
              ret = OB_INVALID_ARGUMENT;
            }
            break;
          }
          case 'H':
          case 'k':
          case 'h':
          case 'I':
          case 'l': {
            if (OB_SUCC(get_datetime_digits(str_pos, str_end, 2, digits))) {
              ob_time.parts_[DT_HOUR] = digits.value_;
            }
            break;
          }
          case 'i': {
            if (OB_SUCC(get_datetime_digits(str_pos, str_end, 2, digits))) {
              ob_time.parts_[DT_MIN] = digits.value_;
            }
            break;
          }
          case 'j': {
            if (OB_SUCC(get_datetime_digits(str_pos, str_end, 3, digits))) {
              ob_time.parts_[DT_YDAY] = digits.value_;
            }
            break;
          }
          case 'M': {
X
xy0 已提交
2256
            name.assign_ptr(const_cast<char *>(str_pos), static_cast<int32_t>(str_end - str_pos));
O
oceanbase-admin 已提交
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
            if (OB_SUCC(
                    get_str_array_idx(name, MON_NAMES, static_cast<int32_t>(MONS_PER_YEAR), ob_time.parts_[DT_MON]))) {
              str_pos += MON_NAMES[ob_time.parts_[DT_MON]].len_;
            }
            break;
          }
          case 'r':
          case 'T': {
            // HOUR, MINUTE
            for (int i = DT_HOUR; OB_SUCC(ret) && i <= DT_MIN; ++i) {
              if (OB_SUCC(get_datetime_digits_delims(str_pos, str_end, 2, digits, delims))) {
                if (!is_single_colon(delims)) {
                  ret = OB_INVALID_DATE_VALUE;
                } else {
                  ob_time.parts_[i] = digits.value_;
                }
              }
            }
            if (OB_SUCC(ret)) {
              if (OB_FAIL(get_datetime_digits(str_pos, str_end, 2, digits))) {
                LOG_WARN("failed to get digits from datetime string");
              } else {
                ob_time.parts_[DT_SEC] = digits.value_;
              }
            }
            break;
          }
          case 'p': {
            if (HOUR_UNUSE != hour_flag || ob_time.parts_[DT_HOUR] > 12) {
              ret = OB_INVALID_DATE_VALUE;
            } else if (0 == strncasecmp(str_pos, "AM", strlen("AM"))) {
              hour_flag = HOUR_AM;
              str_pos += strlen("AM");
            } else if (0 == strncasecmp(str_pos, "PM", strlen("PM"))) {
              hour_flag = HOUR_PM;
              str_pos += strlen("PM");
            } else {  // invalid date format
              ret = OB_INVALID_DATE_VALUE;
            }
            break;
          }
          case 'S':
          case 's': {
            if (OB_SUCC(get_datetime_digits(str_pos, str_end, 2, digits))) {
              ob_time.parts_[DT_SEC] = digits.value_;
            }
            break;
          }
J
jg0 已提交
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
          case 'U': {
            GET_YEAR_WEEK_WDAY(2, week_day_elements, UPPER_SET, week, 53, 0);
            week_day_elements.week_u_set_ = true;
            break;
          }
          case 'u': {
            GET_YEAR_WEEK_WDAY(2, week_day_elements, LOWER_SET, week, 53, 0);
            week_day_elements.week_u_set_ = true;
            break;
          }
          case 'V': {
            GET_YEAR_WEEK_WDAY(2, week_day_elements, UPPER_SET, week, 53, 1);
            week_day_elements.week_u_set_ = false;
            break;
          }
          case 'v': {
            GET_YEAR_WEEK_WDAY(2, week_day_elements, LOWER_SET, week, 53, 1);
            week_day_elements.week_u_set_ = false;
            break;
          }
          case 'W': {
            name.assign_ptr(const_cast<char *>(str_pos), static_cast<int32_t>(str_end - str_pos));
X
xy0 已提交
2327 2328
            if (OB_SUCC(get_str_array_idx(
                    name, WDAY_NAMES, static_cast<int32_t>(DAYS_PER_WEEK), ob_time.parts_[DT_WDAY]))) {
J
jg0 已提交
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350
              str_pos += WDAY_NAMES[ob_time.parts_[DT_WDAY]].len_;
              week_day_elements.weekday_set_ = true;
              week_day_elements.weekday_value_ = ob_time.parts_[DT_WDAY] % DAYS_PER_WEEK;
            }
            break;
          }
          case 'w': {
            if (OB_SUCC(get_datetime_digits(str_pos, str_end, 1, digits))) {
              if (digits.value_ < 0 || digits.value_ > 6) {
                ret = OB_INVALID_DATE_VALUE;
              } else {
                week_day_elements.weekday_set_ = true;
                week_day_elements.weekday_value_ = digits.value_;
                ob_time.parts_[DT_WDAY] = 0 == digits.value_ ? DAYS_PER_WEEK : digits.value_;
              }
            }
            break;
          }
          case 'X': {
            GET_YEAR_WEEK_WDAY(4, week_day_elements, UPPER_SET, year, 9999, 0);
            break;
          }
O
oceanbase-admin 已提交
2351
          case 'x': {
J
jg0 已提交
2352
            GET_YEAR_WEEK_WDAY(4, week_day_elements, LOWER_SET, year, 9999, 0);
O
oceanbase-admin 已提交
2353 2354 2355
            break;
          }
          case 'Y': {
J
jg0 已提交
2356
            const char *ori_pos = str_pos;
O
oceanbase-admin 已提交
2357
            if (OB_SUCC(get_datetime_digits(str_pos, str_end, 4, digits))) {
J
jg0 已提交
2358 2359 2360
              if (str_pos - ori_pos <= 2) {
                apply_date_year2_rule(digits);
              }
O
oceanbase-admin 已提交
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
              ob_time.parts_[DT_YEAR] = digits.value_;
            }
            break;
          }
          case 'y': {
            if (OB_SUCC(get_datetime_digits(str_pos, str_end, 2, digits))) {
              apply_date_year2_rule(digits);
              ob_time.parts_[DT_YEAR] = digits.value_;
            }
            break;
          }
          case '%':
          default:
            ret = OB_NOT_SUPPORTED;
            break;
        }
        if (OB_SUCC(ret) && fmt_pos < fmt_end) {
          fmt_pos++;
        }
      } else if (*(fmt_pos++) != *(str_pos++)) {
        ret = OB_INVALID_DATE_VALUE;
        break;
      }
    }
    if (OB_SUCC(ret) && only_white_space) {
      ret = OB_INVALID_ARGUMENT;
      LOG_WARN("only white space in format argument", K(ret));
    }
    if (OB_SUCC(ret)) {
      if (HOUR_AM == hour_flag && 12 == ob_time.parts_[DT_HOUR]) {
        ob_time.parts_[DT_HOUR] = 0;
      } else if (HOUR_PM == hour_flag && ob_time.parts_[DT_HOUR] > 0 && ob_time.parts_[DT_HOUR] < 12) {
        ob_time.parts_[DT_HOUR] += 12;
      }
J
jg0 已提交
2395 2396
      if (OB_FAIL(handle_year_week_wday(week_day_elements, ob_time))) {
        LOG_WARN("handle %u %x %v and %w value failed", K(ret));
X
xy0 已提交
2397
      } else if (0 == ob_time.parts_[DT_MON] && 0 == ob_time.parts_[DT_MDAY] && 0 == ob_time.parts_[DT_YEAR]) {
O
oceanbase-admin 已提交
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
        if (OB_FAIL(validate_time(ob_time))) {
          LOG_WARN("time value is invalid or out of range", K(ret), K(str));
        }
      } else {
        if (OB_FAIL(validate_datetime(ob_time))) {
          LOG_WARN("datetime is invalid or out of range", K(ret), K(str));
        } else if (ZERO_DATE != ob_time.parts_[DT_DATE]) {
          ob_time.parts_[DT_DATE] = ob_time_to_date(ob_time);
        }
      }
    }
  }
  return ret;
}

J
jg0 已提交
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423
int ObTimeConverter::calc_date_with_year_week_wday(const ObYearWeekWdayElems &elements, ObTime &ot)
{
  int ret = OB_SUCCESS;
  ObTime tmp_ot;
  tmp_ot.parts_[DT_YEAR] = elements.is_year_set() ? elements.year_value_ : ot.parts_[DT_YEAR];
  tmp_ot.parts_[DT_MON] = 1;
  tmp_ot.parts_[DT_MDAY] = 1;
  tmp_ot.parts_[DT_DATE] = ob_time_to_date(tmp_ot);
  // now we get weekday of %X-01-01: tmp_ot.parts_[DT_WDAY]
  const bool is_sunday_start = elements.is_upper_week_set();
  int32_t week = elements.week_value_;
X
xy0 已提交
2424 2425
  int32_t weekday =
      is_sunday_start ? elements.weekday_value_ : ((elements.weekday_value_ + DAYS_PER_WEEK - 1) % DAYS_PER_WEEK) + 1;
J
jg0 已提交
2426 2427 2428 2429
  // %X%V means week start with sunday and the first week in the year must contain sunday.
  const int32_t first_week_monday_offset_upper[DAYS_PER_WEEK] = {1, 7, 6, 5, 4, 3, 2};
  // %X%V means week start with monday and the first week in the year must contain at least 4 days.
  const int32_t first_week_monday_offset_lower[DAYS_PER_WEEK] = {1, 0, -1, -2, -3, 3, 2};
X
xy0 已提交
2430 2431 2432 2433
  int32_t offset =
      (elements.is_upper_week_set() ? first_week_monday_offset_upper[tmp_ot.parts_[DT_WDAY] % DAYS_PER_WEEK]
                                    : first_week_monday_offset_lower[tmp_ot.parts_[DT_WDAY] % DAYS_PER_WEEK]) +
      (week - 1) * DAYS_PER_WEEK + (weekday - 1);
J
jg0 已提交
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
  int32_t date_value = tmp_ot.parts_[DT_DATE] + offset;
  if (OB_FAIL(date_to_ob_time(date_value, ot))) {
    LOG_WARN("date to ob time failed", K(ret));
  }
  LOG_DEBUG("%x %v", K(ret), K(elements), K(tmp_ot), K(offset), K(date_value), K(ot));
  return ret;
}

int ObTimeConverter::handle_year_week_wday(const ObYearWeekWdayElems &elements, ObTime &ot)
{
  int ret = OB_SUCCESS;
  //%X/%x must be used together with %V/%v
X
xy0 已提交
2446 2447
  if ((elements.is_year_set() || elements.is_week_v_set()) &&
      !(elements.year_set_state_ == elements.week_set_state_ && elements.is_week_v_set())) {
J
jg0 已提交
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475
    ret = OB_INVALID_DATE_VALUE;
    MEMSET(ot.parts_, 0, sizeof(*ot.parts_) * TOTAL_PART_CNT);
    ot.parts_[DT_DATE] = ZERO_DATE;
    LOG_WARN("%x and %v must be used together", K(ret), K(elements));
  } else if (elements.is_year_set()) {
    if (elements.year_value_ <= 0) {
      MEMSET(ot.parts_, 0, sizeof(*ot.parts_) * TOTAL_PART_CNT);
      ot.parts_[DT_DATE] = ZERO_DATE;
    } else if (elements.is_weekday_set()) {
      // calc date with elements %x, %v and %w/a
      if (OB_FAIL(calc_date_with_year_week_wday(elements, ot))) {
        LOG_WARN("calc date with year week and wday failed", K(ret));
      }
    }
  } else if (elements.is_week_u_set() && elements.is_weekday_set()) {
    if (0 == ot.parts_[DT_YEAR]) {
      MEMSET(ot.parts_, 0, sizeof(*ot.parts_) * TOTAL_PART_CNT);
      ot.parts_[DT_DATE] = ZERO_DATE;
    } else {
      // calc date with elements %y %u and %w/a
      if (OB_FAIL(calc_date_with_year_week_wday(elements, ot))) {
        LOG_WARN("calc date with year week and wday failed", K(ret));
      }
    }
  }
  return ret;
}

X
xy0 已提交
2476
int ObTimeConverter::str_to_ob_interval(const ObString &str, ObDateUnitType unit_type, ObInterval &ob_interval)
O
oceanbase-admin 已提交
2477 2478 2479 2480 2481 2482 2483 2484 2485 2486
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(str.ptr()) || OB_UNLIKELY(str.length() <= 0)) {
    for (int i = 0; OB_SUCC(ret) && i < TOTAL_PART_CNT; i++) {
      if (OB_UNLIKELY(0 != ob_interval.parts_[i])) {
        ret = OB_ERR_UNEXPECTED;
        LIB_TIME_LOG(ERROR, "the part of ob_interval is 0", K(ret));
      }
    }
  } else {
X
xy0 已提交
2487 2488
    const char *pos = str.ptr();
    const char *end = pos + str.length();
O
oceanbase-admin 已提交
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
    if (is_negative(pos, end)) {
      ob_interval.mode_ |= DT_MODE_NEG;
    }
    ObTimeDigits digits[DATETIME_PART_CNT + 1];
    ObTimeDelims delims[DATETIME_PART_CNT + 1];
    int32_t expect_cnt = INTERVAL_INDEX[unit_type].count_;
    int32_t i = 0;
    for (; OB_SUCC(ret) && i < expect_cnt + 1 && pos < end; ++i) {
      if (OB_FAIL(get_datetime_digits_delims(pos, end, INT32_MAX, digits[i], delims[i]))) {
        LOG_WARN("failed to get part value of interval", K(ret), K(str));
      }
    }
    if (OB_SUCC(ret)) {
      // date_add('2012-1-1', interval '1-2' hour) => 2012-1-1 01:00:00 .
      // date_add('2012-1-1', interval '1-2-3' hour) => 2012-1-1 01:00:00 .
      // date_add('2012-1-1', interval '1-2-3' day_hour) => NULL.
      // date_add('2012-1-1', interval '1:2.3' minute_second) => NULL.
      if (i == expect_cnt + 1 && digits[expect_cnt].len_ > 0 && expect_cnt > 1) {
        ret = OB_TOO_MANY_DATETIME_PARTS;
        LOG_WARN("interval has too many datetime parts", K(ret));
      } else {
        if (DATE_UNIT_WEEK == unit_type) {
          digits[0].value_ *= DAYS_PER_WEEK;
        } else if (DATE_UNIT_QUARTER == unit_type) {
          digits[0].value_ *= MONS_PER_QUAR;
        }
        // date_add('2012-1-1', interval '1' second_microsecond) => 2012-01-01 00:00:00.100000 .
        // date_add('2012-1-1', interval '1-2' hour) => 2012-1-1 01:00:00 .
        int32_t actual_cnt = i;
        int32_t idx_diff = actual_cnt < expect_cnt ? INTERVAL_INDEX[unit_type].begin_ + expect_cnt - actual_cnt
                                                   : INTERVAL_INDEX[unit_type].begin_;
        int32_t assign_cnt = actual_cnt < expect_cnt ? actual_cnt : expect_cnt;
        for (i = 0; OB_SUCC(ret) && i < assign_cnt; ++i) {
          // date_add('2012-1-1', interval '1' microsecond) => 2012-01-01 00:00:00.000001 .
          // date_add('2012-1-1', interval '1.234' second_microsecond) => 2012-01-01 00:00:01.234000 .
          // date_add('2012-1-1', interval '1.2345678' second_microsecond) => 2012-01-01 00:00:03.345678 .
          // do not trunc or round.
          if (DT_USEC == i + idx_diff && expect_cnt > 1) {
            ret = normalize_usecond_trunc(digits[i], false);
          }
          ob_interval.parts_[i + idx_diff] = digits[i].value_;
        }
        // date_add('2012-1-1', interval '1.2.3' second) => 2012-01-01 00:00:01.200000 .
        // date_add('2012-1-1', interval '1:2.3' second) => 2012-01-01 00:00:01 .
        // date_add('2012-1-1', interval '1 .2' second) => 2012-01-01 00:00:01 .
        // date_add('2012-1-1', interval '1. 2' second) => 2012-01-01 00:00:01 .
        // date_add('2012-1-1', interval '1..2' second) => 2012-01-01 00:00:01 .
        // date_add('2012-1-1', interval '1.2345678' second) => 2012-01-01 00:00:01.234567 .
        // trunc, don't round.
        if (DATE_UNIT_SECOND == unit_type && actual_cnt > 1 && is_single_dot(delims[0])) {
          ret = normalize_usecond_trunc(digits[1], true);
          ob_interval.parts_[DT_USEC] = digits[1].value_;
        }
      }
    }
  }
  return ret;
}

X
xy0 已提交
2548
int ObTimeConverter::usec_to_ob_time(int64_t usec, ObTime &ob_time)
O
oceanbase-admin 已提交
2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
{
  int ret = OB_SUCCESS;
  int32_t days = static_cast<int32_t>(usec / USECS_PER_DAY);
  usec %= USECS_PER_DAY;
  if (OB_UNLIKELY(usec < 0)) {
    --days;
    usec += USECS_PER_DAY;
  }
  if (OB_FAIL(date_to_ob_time(days, ob_time))) {
    LOG_WARN("failed to convert date part to obtime", K(ret), K(usec));
  } else if (OB_FAIL(time_to_ob_time(usec, ob_time))) {
    LOG_WARN("failed to convert time part to obtime", K(ret), K(usec));
  }
  return ret;
}

X
xy0 已提交
2565
int ObTimeConverter::datetime_to_ob_time(int64_t value, const ObTimeZoneInfo *tz_info, ObTime &ob_time)
O
oceanbase-admin 已提交
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
{
  int ret = OB_SUCCESS;
  int64_t usec = value;
  if (OB_UNLIKELY(ZERO_DATETIME == usec)) {
    MEMSET(ob_time.parts_, 0, sizeof(*ob_time.parts_) * TOTAL_PART_CNT);
    ob_time.parts_[DT_DATE] = ZERO_DATE;
  } else if (OB_FAIL(add_timezone_offset(tz_info, usec))) {
    LOG_WARN("failed to adjust value with time zone offset", K(ret));
  } else if (OB_FAIL(usec_to_ob_time(usec, ob_time))) {
    LOG_WARN("failed to usec_to_ob_time", K(ret));
  } else {
    LOG_DEBUG("succ datetime_to_ob_time", K(value), K(ob_time));
  }
  return ret;
}

X
xy0 已提交
2582 2583
int ObTimeConverter::otimestamp_to_ob_time(const ObObjType type, const ObOTimestampData &ot_data,
    const ObTimeZoneInfo *tz_info, ObTime &ob_time, const bool store_utc_time /*true*/)
O
oceanbase-admin 已提交
2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(!ob_is_otimestamp_type(type))) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("it is not otimestamp type", K(type), K(ret));
  } else if (ot_data.is_null_value()) {
    // NOTE: Any arithmetic expression containing a null always evaluates to null.
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("it is null otimestamp, should not arrive here", K(type), K(ot_data), K(ret));
  } else if (ObTimestampTZType == type) {
    int32_t offset_min = 0;
    int64_t usec = ot_data.time_us_;
    if (OB_FAIL(extract_offset_from_otimestamp(ot_data, tz_info, offset_min, ob_time))) {
      LOG_WARN("failed to extract_offset_from_otimestamp", K(ret));
    } else {
      usec += (store_utc_time ? 0 : offset_min * SECS_PER_MIN * USECS_PER_SEC);
      int64_t nsec = 0;
      if (OB_FAIL(usec_to_ob_time(usec, ob_time))) {
        LOG_WARN("failed to convert usec part to obtime", K(ret), K(usec));
      } else if (OB_UNLIKELY(
                     (nsec = ob_time.parts_[DT_USEC] * NSECS_PER_USEC + ot_data.time_ctx_.tail_nsec_) > INT32_MAX)) {
        ret = OB_SIZE_OVERFLOW;
        LOG_WARN("nsec is overflow", K(nsec), K(ret));
      } else {
        ob_time.parts_[DT_USEC] = static_cast<int32_t>(nsec);
        ob_time.mode_ |= DT_TYPE_ORACLE;
        ob_time.mode_ |= DT_TYPE_TIMEZONE;
        if (store_utc_time) {
          ob_time.mode_ |= DT_TYPE_STORE_UTC;
        } else {
          ob_time.mode_ &= ~(DT_TYPE_STORE_UTC);
        }
      }
    }
  } else {
    int64_t usec = ot_data.time_us_;
    int64_t nsec = 0;
    if (ObTimestampLTZType == type && !store_utc_time) {
      if (OB_FAIL(add_timezone_offset(tz_info, usec))) {
        LOG_WARN("failed to adjust value with time zone offset", K(ret));
      }
    }

    if (OB_FAIL(ret)) {
    } else if (OB_FAIL(usec_to_ob_time(usec, ob_time))) {
      LOG_WARN("failed to convert usec part to obtime", K(ret), K(usec));
    } else if (OB_UNLIKELY(
                   (nsec = ob_time.parts_[DT_USEC] * NSECS_PER_USEC + ot_data.time_ctx_.tail_nsec_) > INT32_MAX)) {
      ret = OB_SIZE_OVERFLOW;
      LOG_WARN("nsec is overflow", K(nsec), K(ret));
    } else {
      ob_time.parts_[DT_USEC] = static_cast<int32_t>(nsec);
      ob_time.mode_ |= DT_TYPE_ORACLE;
      if (ObTimestampNanoType == type || !store_utc_time) {
        ob_time.mode_ &= ~(DT_TYPE_STORE_UTC);
      } else {
        ob_time.mode_ |= DT_TYPE_STORE_UTC;
      }
    }
  }

  if (OB_SUCC(ret)) {
    LOG_DEBUG("succ to otimestamp_to_ob_time", K(ret), K(ot_data), K(type), K(ob_time), K(lbt()));
  }

  return ret;
}

X
xy0 已提交
2652
int ObTimeConverter::date_to_ob_time(int32_t value, ObTime &ob_time)
O
oceanbase-admin 已提交
2653 2654
{
  int ret = OB_SUCCESS;
X
xy0 已提交
2655
  int32_t *parts = ob_time.parts_;
O
oceanbase-admin 已提交
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
  if (!HAS_TYPE_ORACLE(ob_time.mode_) && OB_UNLIKELY(ZERO_DATE == value)) {
    memset(parts, 0, sizeof(*parts) * DATETIME_PART_CNT);
    parts[DT_DATE] = ZERO_DATE;
  } else {
    int32_t days = value;
    int32_t leap_year = 0;
    int32_t year = EPOCH_YEAR4;
    parts[DT_DATE] = value;
    // year.
    while (days < 0 || days >= DAYS_PER_YEAR[leap_year = IS_LEAP_YEAR(year)]) {
      int32_t new_year = year + days / DAYS_PER_NYEAR;
      new_year -= (days < 0);
      days -= (new_year - year) * DAYS_PER_NYEAR + LEAP_YEAR_COUNT(new_year - 1) - LEAP_YEAR_COUNT(year - 1);
      year = new_year;
    }
    parts[DT_YEAR] = year;
    parts[DT_YDAY] = days + 1;
    parts[DT_WDAY] = WDAY_OFFSET[value % DAYS_PER_WEEK][EPOCH_WDAY];
    // month.
X
xy0 已提交
2675
    const int32_t *cur_days_until_mon = DAYS_UNTIL_MON[leap_year];
O
oceanbase-admin 已提交
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685
    int32_t month = 1;
    for (; month < MONS_PER_YEAR && days >= cur_days_until_mon[month]; ++month) {}
    parts[DT_MON] = month;
    days -= cur_days_until_mon[month - 1];
    // day.
    parts[DT_MDAY] = days + 1;
  }
  return ret;
}

X
xy0 已提交
2686
int ObTimeConverter::time_to_ob_time(int64_t value, ObTime &ob_time)
O
oceanbase-admin 已提交
2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(value < 0)) {
    ob_time.mode_ |= DT_MODE_NEG;
    value = -value;
  }
  int64_t secs = USEC_TO_SEC(value);
  ob_time.parts_[DT_HOUR] = static_cast<int32_t>(secs / SECS_PER_HOUR);
  secs %= SECS_PER_HOUR;
  ob_time.parts_[DT_MIN] = static_cast<int32_t>(secs / SECS_PER_MIN);
  ob_time.parts_[DT_SEC] = static_cast<int32_t>(secs % SECS_PER_MIN);
  ob_time.parts_[DT_USEC] = static_cast<int32_t>(value % USECS_PER_SEC);
  return ret;
}

////////////////////////////////
// int / uint / string <- ObTime -> datetime / date / time.

#define DTAE_DIGIT_LEN 8
#define TIME_DIGIT_LEN 6

X
xj0 已提交
2708
int64_t ObTimeConverter::ob_time_to_int(const ObTime &ob_time, ObDTMode mode)
O
oceanbase-admin 已提交
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
{
  int64_t value = 0;
  if ((DT_TYPE_DATE & mode) != 0) {
    value =
        ob_time.parts_[DT_YEAR] * power_of_10[4] + ob_time.parts_[DT_MON] * power_of_10[2] + ob_time.parts_[DT_MDAY];
  }
  if ((DT_TYPE_TIME & mode) != 0) {
    value *= power_of_10[6];
    value +=
        (ob_time.parts_[DT_HOUR] * power_of_10[4] + ob_time.parts_[DT_MIN] * power_of_10[2] + ob_time.parts_[DT_SEC]);
  }
  return value;
}

static const int32_t DT_PART_MUL[DATETIME_PART_CNT] = {100, 100, 100, 100, 100, 1000000, 1};
static const int64_t ZERO_DATE_WEEK = 613566757;  // for 0000-00-00 00:00:00 .
X
xy0 已提交
2725
int64_t ObTimeConverter::ob_time_to_int_extract(const ObTime &ob_time, ObDateUnitType unit_type)
O
oceanbase-admin 已提交
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785
{
  int64_t value = 0;
  switch (unit_type) {
    case DATE_UNIT_WEEK: {
      value = ZERO_DATE == ob_time.parts_[DT_DATE]
                  ? ZERO_DATE_WEEK
                  : ObTimeConverter::ob_time_to_week(ob_time, DT_WEEK_SUN_BEGIN | DT_WEEK_ZERO_BEGIN);
      break;
    }
    case DATE_UNIT_QUARTER: {
      value = (ob_time.parts_[DT_MON] + 2) / 3;
      break;
    }
    default: {
      int32_t idx_begin = INTERVAL_INDEX[unit_type].begin_;
      int32_t idx_end = INTERVAL_INDEX[unit_type].end_;
      for (int32_t i = idx_begin; i < idx_end; ++i) {
        value += ob_time.parts_[i];
        value *= DT_PART_MUL[i];
      }
      value += ob_time.parts_[idx_end];
      if ((DT_MODE_NEG & ob_time.mode_) != 0) {
        value = -value;
      }
      break;
    }
  }
  return value;
}

#define DATE_FMT_WITH_DELIM "%04d-%02d-%02d"
#define DATE_FMT_NO_DELIM "%04d%02d%02d"
#define TIME_FMT_WITH_DELIM "%02d:%02d:%02d"
#define TIME_FMT_NO_DELIM "%02d%02d%02d"

// should guarantee buff have two digit
// snprintf(buff, "%02d", num) num is less than 100
#define PRINTF_2D_WITH_TWO_DIGIT(buff, num) \
  {                                         \
    int32_t tmp2 = (num) / 10;              \
    int32_t tmp = (num)-tmp2 * 10;          \
    *buff++ = (char)('0' + tmp2);           \
    *buff++ = (char)('0' + tmp);            \
  }

// snprintf(buff, "%02d", num), num is less than 1000
#define PRINTF_2D_WITH_THREE_DIGIT(buff, num) \
  {                                           \
    int32_t m = (num) / 10;                   \
    int32_t l = (num)-m * 10;                 \
    int32_t h = m / 10;                       \
    m = m - h * 10;                           \
    if (h > 0) {                              \
      *buff++ = (char)('0' + h);              \
    }                                         \
    *buff++ = (char)('0' + m);                \
    *buff++ = (char)('0' + l);                \
  }

int ObTimeConverter::ob_time_to_str(
X
xy0 已提交
2786
    const ObTime &ob_time, ObDTMode mode, int16_t scale, char *buf, int64_t buf_len, int64_t &pos, bool with_delim)
O
oceanbase-admin 已提交
2787 2788 2789 2790 2791 2792 2793
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(mode <= 0) || OB_UNLIKELY(scale > (HAS_TYPE_ORACLE(ob_time.mode_) ? 9 : 6)) || OB_ISNULL(buf) ||
      OB_UNLIKELY(buf_len <= 0) || OB_UNLIKELY(pos < 0)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("Invalid argument", KP(buf), K(buf_len), K(pos), K(scale), K(mode), K(ret));
  } else {
X
xy0 已提交
2794
    const int32_t *parts = ob_time.parts_;
O
oceanbase-admin 已提交
2795 2796 2797
    if (HAS_TYPE_DATE(mode)) {
      if (OB_UNLIKELY(parts[DT_YEAR] > 9999) || OB_UNLIKELY(parts[DT_YEAR] < 0) || OB_UNLIKELY(parts[DT_MON] > 12) ||
          OB_UNLIKELY(parts[DT_MON] < 0) || OB_UNLIKELY(parts[DT_MDAY] > 31) || OB_UNLIKELY(parts[DT_MDAY] < 0)) {
2798 2799 2800 2801 2802
        if (parts[DT_YEAR] > 9999 || parts[DT_YEAR] < 0) {
          ret = OB_ERR_DATETIME_INTERVAL_INTERNAL_ERROR;
        } else {
          ret = OB_ERR_UNEXPECTED;
        }
O
oceanbase-admin 已提交
2803 2804 2805
        LOG_WARN("Unexpected time", K(ret), K(parts[DT_YEAR]), K(parts[DT_MON]), K(parts[DT_MDAY]));
      } else if (OB_LIKELY(with_delim && (buf_len - pos) > 10)        // format 0000-00-00
                 || OB_LIKELY(!with_delim && (buf_len - pos) > 8)) {  // format yyyymmdd
X
xy0 已提交
2806
        char *buf_t = buf + pos;
O
oceanbase-admin 已提交
2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
        // deal year.year[0000-9999]
        int32_t high = parts[DT_YEAR] / 100;
        int32_t low = parts[DT_YEAR] - high * 100;
        PRINTF_2D_WITH_TWO_DIGIT(buf_t, high);
        PRINTF_2D_WITH_TWO_DIGIT(buf_t, low);
        if (with_delim) {
          *buf_t++ = '-';
        }
        // deal month
        PRINTF_2D_WITH_TWO_DIGIT(buf_t, parts[DT_MON]);
        if (with_delim) {
          *buf_t++ = '-';
        }
        // deal day
        PRINTF_2D_WITH_TWO_DIGIT(buf_t, parts[DT_MDAY]);
        pos += (buf_t - buf - pos);
      } else {
        ret = OB_SIZE_OVERFLOW;
      }
    }
    if (OB_SUCC(ret)) {
      if (IS_TYPE_DATETIME(mode) && with_delim) {
        if (OB_LIKELY(buf_len - pos > 1)) {
          *(buf + pos++) = ' ';
        } else {
          ret = OB_SIZE_OVERFLOW;
        }
      } else if (IS_TYPE_TIME(mode) && IS_NEG_TIME(ob_time.mode_)) {
        if (OB_LIKELY(buf_len - pos > 1)) {
          *(buf + pos++) = '-';
        } else {
          ret = OB_SIZE_OVERFLOW;
        }
      }
    }
    if (OB_SUCC(ret) && HAS_TYPE_TIME(mode)) {
      if (OB_UNLIKELY(parts[DT_HOUR] > 999) || OB_UNLIKELY(parts[DT_HOUR] < 0) || OB_UNLIKELY(parts[DT_MIN] > 60) ||
          OB_UNLIKELY(parts[DT_MIN] < 0) || OB_UNLIKELY(parts[DT_SEC] > 60) || OB_UNLIKELY(parts[DT_SEC] < 0)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("Unexpected hour", K(parts[DT_HOUR]), K(parts[DT_MIN]), K(parts[DT_SEC]), K(ret));
      } else if (OB_LIKELY(with_delim && (buf_len - pos) > 9)         // format 00:00:00 and hour may 3 digit
                 || OB_LIKELY(!with_delim && (buf_len - pos) > 7)) {  // format hhmmss and hour may 3 digit
X
xy0 已提交
2849
        char *buf_t = buf + pos;
O
oceanbase-admin 已提交
2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944
        PRINTF_2D_WITH_THREE_DIGIT(buf_t, parts[DT_HOUR]);
        if (with_delim) {
          *buf_t++ = ':';
        }
        PRINTF_2D_WITH_TWO_DIGIT(buf_t, parts[DT_MIN]);
        if (with_delim) {
          *buf_t++ = ':';
        }
        PRINTF_2D_WITH_TWO_DIGIT(buf_t, parts[DT_SEC]);
        pos += (buf_t - buf - pos);
      } else {
        ret = OB_SIZE_OVERFLOW;
      }

      const bool is_oracle_timestamp = HAS_TYPE_ORACLE(ob_time.mode_);
      if (scale < 0) {
        if (lib::is_oracle_mode()) {
          scale = is_oracle_timestamp ? 9 : 0;
        } else {
          scale = (parts[DT_USEC] > 0 ? 6 : 0);
        }
      }

      if (OB_SUCC(ret) && scale >= 0) {
        const int32_t max_value = is_oracle_timestamp ? 1000000000L : 1000000L;
        const int32_t max_sacle = is_oracle_timestamp ? 9 : 6;
        int32_t usec = parts[DT_USEC];
        if (0 == scale) {
          if (is_oracle_timestamp) {
            if (OB_LIKELY((buf_len - pos) > (scale + 1))) {
              *(buf + pos++) = '.';
            } else {
              ret = OB_SIZE_OVERFLOW;
            }
          } else {
            // do nothing
          }
        } else if (OB_UNLIKELY(usec >= max_value) || OB_UNLIKELY(usec < 0)) {
          ret = OB_ERR_UNEXPECTED;
          LOG_ERROR("unexpect value", K(max_value), K(ret));
        } else if (OB_LIKELY((buf_len - pos) > (scale + 1))) {
          *(buf + pos++) = '.';
          usec = static_cast<int32_t>(usec / power_of_10[max_sacle - scale]);
          ObFastFormatInt ffi(usec);
          if (OB_UNLIKELY(scale < ffi.length())) {
            ret = OB_ERR_UNEXPECTED;
          } else {
            MEMSET(buf + pos, '0', scale - ffi.length());
            MEMCPY(buf + pos + scale - ffi.length(), ffi.ptr(), ffi.length());
            pos += scale;
          }
        } else {
          ret = OB_SIZE_OVERFLOW;
        }
      }

      if (OB_SUCC(ret) && HAS_TYPE_TIMEZONE(ob_time.mode_)) {
        if (ob_time.is_tz_name_valid_) {
          int32_t tmp_len = static_cast<int32_t>(strlen(ob_time.tz_name_));
          if (buf_len - pos < (tmp_len + 1)) {
            ret = OB_SIZE_OVERFLOW;
          } else {
            *(buf + pos++) = ' ';
            MEMCPY(buf + pos, ob_time.tz_name_, tmp_len);
            pos += tmp_len;
          }

          if (OB_SUCC(ret)) {
            tmp_len = static_cast<int32_t>(strlen(ob_time.tzd_abbr_));
            if (buf_len - pos < (tmp_len + 1)) {
              ret = OB_SIZE_OVERFLOW;
            } else {
              *(buf + pos++) = ' ';
              MEMCPY(buf + pos, ob_time.tzd_abbr_, tmp_len);
              pos += tmp_len;
            }
          }
        } else {
          int32_t dt_offset_min = ob_time.parts_[DT_OFFSET_MIN];
          if (OB_UNLIKELY(buf_len - pos < 2)) {
            ret = OB_SIZE_OVERFLOW;
          } else {
            *(buf + pos++) = ' ';
            if (dt_offset_min < 0) {
              *(buf + pos++) = '-';
              dt_offset_min = -dt_offset_min;
            } else {
              *(buf + pos++) = '+';
            }
          }

          if (OB_FAIL(ret)) {
          } else if (OB_LIKELY(with_delim && (buf_len - pos) > 6)) {  // format 00:00 and hour may 3 digit
            int32_t offset_hour = static_cast<int32_t>(dt_offset_min / MINS_PER_HOUR);
            int32_t offset_min = static_cast<int32_t>(dt_offset_min % MINS_PER_HOUR);
X
xy0 已提交
2945
            char *buf_t = buf + pos;
O
oceanbase-admin 已提交
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970
            PRINTF_2D_WITH_THREE_DIGIT(buf_t, offset_hour);
            if (with_delim) {
              *buf_t++ = ':';
            }
            PRINTF_2D_WITH_TWO_DIGIT(buf_t, offset_min);
            pos += (buf_t - buf - pos);
          } else {
            ret = OB_SIZE_OVERFLOW;
          }

          if (OB_SUCC(ret)) {
            if (OB_UNLIKELY(pos + 1 > buf_len)) {
              ret = OB_SIZE_OVERFLOW;
            } else {
              buf[pos++] = ' ';  // for space between TZR and TZD
            }
          }
        }
      }  // end of is_oracle_timestamp
    }
  }
  return ret;
}

int ObTimeConverter::get_day_and_month_from_year_day(
X
xy0 已提交
2971
    const int32_t yday, const int32_t year, int32_t &month, int32_t &day)
O
oceanbase-admin 已提交
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
{
  int ret = OB_SUCCESS;
  int32_t leap_year = IS_LEAP_YEAR(year);
  if (OB_UNLIKELY(yday > DAYS_UNTIL_MON[leap_year][12])) {
    ret = OB_ERR_INVALID_DAY_OF_YEAR_VALUE;
  } else {
    bool stop_flag = false;
    for (int32_t i = ObDFMLimit::MONTH.MIN; !stop_flag && i <= ObDFMLimit::MONTH.MAX; ++i) {
      if (yday <= DAYS_UNTIL_MON[leap_year][i]) {
        month = i;
        day = yday - DAYS_UNTIL_MON[leap_year][i - 1];
        stop_flag = true;
      }
    }
  }
  return ret;
}

int32_t ObTimeConverter::calc_max_name_length(const ObTimeConstStr names[], const int64_t size)
{
  int32_t res = 0;
  for (int64_t i = 1; i <= size; ++i) {
    if (res < names[i].len_) {
      res = names[i].len_;
    }
  }
  return res;
}

X
xy0 已提交
3001
int ObTimeConverter::data_fmt_nd(char *buffer, int64_t buf_len, int64_t &pos, const int64_t n, int64_t target)
O
oceanbase-admin 已提交
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(n <= 0 || target < 0 || target > 999999999)) {
    ret = OB_ERR_UNEXPECTED;
    LIB_TIME_LOG(ERROR, "invalid argument", K(ret), K(n), K(target));
  } else if (OB_UNLIKELY(n > buf_len - pos)) {
    ret = OB_SIZE_OVERFLOW;
    LIB_TIME_LOG(WARN, "no enough space for buffer", K(ret), K(n), K(buf_len), K(pos));
  } else {
    ObFastFormatInt ffi(target);
    if (ffi.length() > n) {
      ret = OB_SIZE_OVERFLOW;
      LIB_TIME_LOG(WARN, "expected length is little", K(ret), K(n), K(ffi.length()), K(ffi.str()));
    } else {
      MEMSET(buffer + pos, '0', n - ffi.length());
      MEMCPY(buffer + pos + n - ffi.length(), ffi.ptr(), ffi.length());
      pos += n;
    }
  }
  return ret;
}

X
xy0 已提交
3024
int ObTimeConverter::data_fmt_d(char *buffer, int64_t buf_len, int64_t &pos, int64_t target)
O
oceanbase-admin 已提交
3025 3026
{
  int ret = OB_SUCCESS;
J
jg0 已提交
3027
  if (OB_UNLIKELY(target < 0)) {
O
oceanbase-admin 已提交
3028 3029 3030
    ret = OB_ERR_UNEXPECTED;
    LIB_TIME_LOG(ERROR, "invalid argument", K(ret), K(target));
  } else {
X
xy0 已提交
3031
    // buffer_size_need will be 1 or 2 or 3
J
jg0 已提交
3032
    int64_t buffer_size_need = 1 + (target >= 10) + (target >= 100);
O
oceanbase-admin 已提交
3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050
    if (OB_UNLIKELY(buffer_size_need > buf_len - pos)) {
      ret = OB_SIZE_OVERFLOW;
      LIB_TIME_LOG(WARN, "no enough space for buffer", K(ret), K(buffer_size_need), K(buf_len), K(pos));
    } else {
      int64_t idx = pos + buffer_size_need - 1;
      int64_t i = 0;
      // BTW, when loop size is small, maybe, we can use loop unrolling to get better performance.
      while (i < buffer_size_need) {
        buffer[idx--] = static_cast<char>(target % 10 + '0');
        target /= 10;
        ++i;
      }
      pos += i;
    }
  }
  return ret;
}

X
xy0 已提交
3051
int ObTimeConverter::data_fmt_s(char *buffer, int64_t buf_len, int64_t &pos, const char *ptr)
O
oceanbase-admin 已提交
3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(ptr)) {
    ret = OB_ERR_UNEXPECTED;
    LIB_TIME_LOG(ERROR, "invalid argument", K(ret), K(ptr));
  } else {
    int64_t buffer_size_need = strlen(ptr);
    if (OB_UNLIKELY(buffer_size_need > buf_len - pos)) {
      ret = OB_SIZE_OVERFLOW;
      LIB_TIME_LOG(WARN, "no enough space for buffer", K(ret), K(buffer_size_need), K(buf_len), K(pos));
    } else {
      // will not copy the '\0' in ptr
      MEMCPY(buffer + pos, ptr, buffer_size_need);
      pos += buffer_size_need;
    }
  }
  return ret;
}

/**
 * @brief ObTimeConverter::str_to_ob_time_oracle_strict
 *        convert str to ob_time according to oracle literal format of DATE or TIMESTAMP
 *        doc:
 * https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/Literals.html#GUID-8F4B3F82-8821-4071-84D6-FBBA21C05AC1
 * @param in:   str                   intput string
 * @param in:   cvrt_ctx
 * @param in:   is_timestamp_literal is timestamp literal or date literal
 * @param in:   ob_time              memory struct of datetime
 * @param out:  scale                scale of fractional seconds
 * @return OER
 */
X
xy0 已提交
3083 3084
int ObTimeConverter::str_to_ob_time_oracle_strict(const ObString &str, const ObTimeConvertCtx &cvrt_ctx,
    const bool is_timestamp_literal, ObTime &ob_time, ObScale &scale)
O
oceanbase-admin 已提交
3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283
{
  int ret = OB_SUCCESS;
  ObDFMParseCtx ctx(str.ptr(), str.length());
  const int64_t value_len_max = 20;
  int32_t value = 0;
  int64_t value_len = 0;
  scale = 0;

  int64_t part_id_max = DATE_PART_CNT;
  static constexpr char part_seps[ORACLE_DATE_PART_CNT] = {'\0', '-', '-', '\0', ':', ':'};
  ob_time.mode_ |= DT_TYPE_DATETIME;
  if (is_timestamp_literal) {
    ob_time.mode_ |= DT_TYPE_ORACLE;
    part_id_max = ORACLE_DATE_PART_CNT;
  }

  ObDFMUtil::skip_blank_chars(ctx);

  // 1. hard code to parse the input string according to format 'YYYY-MM-DD HH24:MI:SS'
  for (int64_t part_idx = 0; OB_SUCC(ret) && part_idx < part_id_max; ++part_idx) {
    if (OB_UNLIKELY(ctx.is_parse_finish())) {
      ret = OB_INVALID_DATE_VALUE;
      LOG_WARN("invalid date value", K(ret));
    } else {
      if (DT_HOUR != part_idx &&
          DT_YEAR != part_idx) {  // positive year only for now, we do not care '-' sign for BC years
        if (part_seps[part_idx] != ctx.cur_ch_[0]) {
          ret = OB_INVALID_DATE_VALUE;
          LOG_WARN("invalid date value", K(ret), K(part_idx));
        } else {
          ctx.update(1);
          ObDFMUtil::skip_blank_chars(ctx);
        }
      }
      if (OB_FAIL(ret)) {
        // do nothing
      } else if (OB_UNLIKELY(ctx.is_parse_finish())) {
        ret = get_oracle_err_when_datetime_out_of_range(part_idx);
        LOG_WARN("input finished unexpected", K(ret));
      } else if (OB_FAIL(ObDFMUtil::match_int_value(ctx, value_len_max, value_len, value))) {
        LOG_WARN("failed to match int value", K(ret));
      } else if (OB_UNLIKELY(DT_YEAR == part_idx ? (value_len > 5) : (value_len > 2))) {
        ret = get_oracle_err_when_datetime_out_of_range(part_idx);
        LOG_WARN("input finished unexpected", K(ret), K(part_idx), K(value_len));
      } else {
        ob_time.parts_[part_idx] = value;
        ctx.update(value_len);
        ObDFMUtil::skip_blank_chars(ctx);
      }  // end if
    }    // end if
  }      // end for

  // 2. hard code to parse the input string according to format 'FF'
  if (OB_SUCC(ret) && is_timestamp_literal && !ctx.is_parse_finish() && '.' == ctx.cur_ch_[0]) {
    ctx.update(1);
    ObDFMUtil::skip_blank_chars(ctx);
    if (OB_UNLIKELY(ctx.is_parse_finish())) {
      ret = OB_INVALID_DATE_VALUE;
      LOG_WARN("expecting FF failed", K(ret));
    } else if (OB_FAIL(ObDFMUtil::match_int_value(ctx, value_len_max, value_len, value))) {
      LOG_WARN("failed to match int value", K(ret));
    } else if (value_len > OB_MAX_TIMESTAMP_TZ_PRECISION) {
      ret = OB_ERR_THE_LEADING_PRECISION_OF_THE_INTERVAL_IS_TOO_SMALL;
      LOG_WARN("precision not enough", K(ret), K(value_len), K(value));
    } else {
      scale = static_cast<ObScale>(value_len);
      ob_time.parts_[DT_USEC] = static_cast<int32_t>(value * power_of_10[OB_MAX_TIMESTAMP_TZ_PRECISION - scale]);
      ctx.update(value_len);
      ObDFMUtil::skip_blank_chars(ctx);
    }  // end if
  }

  // 3. hard code to parse the input string according to format 'TZR TZD' or 'TZH:TZM'
  if (OB_SUCC(ret) && is_timestamp_literal && !ctx.is_parse_finish()) {
    ob_time.mode_ |= DT_TYPE_TIMEZONE;
    if ('-' == ctx.cur_ch_[0] || '+' == ctx.cur_ch_[0]) {  //'TZH:TZM'
      int32_t factor = ('-' == ctx.cur_ch_[0]) ? -1 : 1;
      int32_t time_zone_offset_min = 0;
      ctx.update(1);
      ObDFMUtil::skip_blank_chars(ctx);
      if (OB_UNLIKELY(ctx.is_parse_finish())) {
        ret = OB_ERR_INVALID_TIME_ZONE_HOUR;
        LOG_WARN("parsing TZR hour failed", K(ret), K(str));
      } else if (OB_FAIL(ObDFMUtil::match_int_value(ctx, value_len_max, value_len, value))) {
        LOG_WARN("parsing to match int value");
      } else if (OB_UNLIKELY(value_len > 2)) {
        ret = OB_ERR_INVALID_TIME_ZONE_HOUR;
        LOG_WARN("parsing TZR hour failed", K(ret), K(str));
      } else {
        time_zone_offset_min = value * 60;
        ctx.update(value_len);
        ObDFMUtil::skip_blank_chars(ctx);
      }  // end if

      if (OB_FAIL(ret)) {
      } else if (OB_UNLIKELY(ctx.is_parse_finish())) {
        ret = OB_INVALID_DATE_VALUE;
        LOG_WARN("expecting TZR min failed", K(ret));
      } else if (OB_UNLIKELY(':' != ctx.cur_ch_[0])) {
        ret = OB_INVALID_DATE_VALUE;
        LOG_WARN("invalid timezone value", K(ret));
      } else {
        ctx.update(1);
        ObDFMUtil::skip_blank_chars(ctx);
      }

      if (OB_FAIL(ret)) {
      } else if (ctx.is_parse_finish()) {
        ret = OB_ERR_INVALID_TIME_ZONE_MINUTE;
        LOG_WARN("invalid timezone value", K(ret));
      } else if (OB_FAIL(ObDFMUtil::match_int_value(ctx, value_len_max, value_len, value))) {
        LOG_WARN("failed to match int value", K(ret));
      } else if (value_len > 2) {
        ret = OB_ERR_INVALID_TIME_ZONE_MINUTE;
        LOG_WARN("parsing TZR hour failed", K(ret), K(str));
      } else {
        time_zone_offset_min += value;
        ctx.update(value_len);
        ObDFMUtil::skip_blank_chars(ctx);
      }

      if (OB_SUCC(ret)) {
        time_zone_offset_min *= factor;
        ob_time.parts_[DT_OFFSET_MIN] = time_zone_offset_min;
      }
    } else {  // TZR TZD
      ob_time.is_tz_name_valid_ = true;
      ObString tzr_str;
      ObString tzd_str;
      int64_t value_len = OB_MAX_TZ_NAME_LEN;
      if (OB_FAIL(ObDFMUtil::match_chars_until_space(ctx, tzr_str, value_len))) {
        LOG_WARN("failed to match tzr", K(ret));
      } else {
        MEMCPY(ob_time.tz_name_, tzr_str.ptr(), tzr_str.length());
        ob_time.tz_name_[tzr_str.length()] = '\0';
        ctx.update(value_len);
        ObDFMUtil::skip_blank_chars(ctx);
      }

      value_len = OB_MAX_TZ_ABBR_LEN;
      if (OB_FAIL(ret) || ctx.is_parse_finish()) {
        // do nothing
      } else if (OB_FAIL(ObDFMUtil::match_chars_until_space(ctx, tzd_str, value_len))) {
        LOG_WARN("failed to match tzd", K(ret));
      } else {
        MEMCPY(ob_time.tzd_abbr_, tzd_str.ptr(), tzd_str.length());
        ob_time.tzd_abbr_[tzd_str.length()] = '\0';
        ctx.update(value_len);
        ObDFMUtil::skip_blank_chars(ctx);
      }
    }
  }

  // 4. check parse end
  if (OB_SUCC(ret) && OB_UNLIKELY(!ctx.is_parse_finish())) {
    ret = OB_INVALID_DATE_VALUE;
    LOG_WARN("literal is more than that expected", K(ret), K(ctx));
  }

  // 5. validate raw value in ob_time
  if (OB_SUCC(ret)) {
    if (OB_FAIL(validate_basic_part_of_ob_time_oracle(ob_time))) {
      LOG_WARN("failed to validate basic part of obtime", K(ret));
    } else {
      ob_time.parts_[DT_DATE] = ob_time_to_date(ob_time);
    }
  }

  // 6. validate timezone name or timezone offset
  if (OB_SUCC(ret) && HAS_TYPE_TIMEZONE(ob_time.mode_)) {
    if (ob_time.is_tz_name_valid_) {
      if (OB_FAIL(calc_tz_offset_by_tz_name(cvrt_ctx, ob_time))) {
        LOG_WARN("calc timezone offset failed", K(ret));
      }
    } else {
      if (OB_UNLIKELY(!ObOTimestampData::is_valid_offset_min_strict(ob_time.parts_[DT_OFFSET_MIN]))) {
        ret = OB_INVALID_DATE_VALUE;
        LOG_WARN("validate timezone offset failed", K(ret));
      }
    }
  }
  if (OB_SUCC(ret)) {
    LOG_DEBUG("convert to oracle timestamp succ", K(ret), K(ob_time));
  } else {
    LOG_WARN("convert to oracle timestamp failed", K(ret), K(ob_time));
  }
  return ret;
}

/**
 * @brief convert string to ob_time struct according to oracle datetime format model
 * @param in:   str         input string
 * @param in:   format      format string
 * @param in:   cvrt_ctx
 * @param in:   target_type includes ObDateTimeType, ObOTimestampTZType, ObOTimestampLTZType, ObOTimestampType
 * @param out:  ob_time     memory struct of datetime
 * @param out:  scale       scale of fractional seconds
 */
int ObTimeConverter::str_to_ob_time_oracle_dfm(
X
xy0 已提交
3284
    const ObString &str, const ObTimeConvertCtx &cvrt_ctx, const ObObjType target_type, ObTime &ob_time, ObScale &scale)
O
oceanbase-admin 已提交
3285 3286
{
  int ret = OB_SUCCESS;
X
xy0 已提交
3287
  const ObString &format = cvrt_ctx.oracle_nls_format_;
O
oceanbase-admin 已提交
3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325
  if (OB_UNLIKELY(
          str.empty() || format.empty() || (!ob_is_otimestamp_type(target_type) && !ob_is_datetime_tc(target_type)))) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(str), K(format), K(ob_time.mode_));
  } else {
    ob_time.mode_ |= DT_TYPE_DATETIME;
    if (ob_is_otimestamp_type(target_type)) {
      ob_time.mode_ |= DT_TYPE_ORACLE;
    }
    if (ob_is_timestamp_tz(target_type)) {
      ob_time.mode_ |= DT_TYPE_TIMEZONE;
    }
  }

  if (OB_SUCC(ret)) {
    ObSEArray<ObDFMElem, ObDFMUtil::COMMON_ELEMENT_NUMBER> dfm_elems;
    ObBitSet<ObDFMFlag::MAX_FLAG_NUMBER> elem_flags;

    // 1. parse and check semantic of format string
    if (OB_FAIL(ObDFMUtil::parse_datetime_format_string(format, dfm_elems))) {
      LOG_WARN("fail to parse oracle datetime format string", K(ret), K(format));
    } else if (OB_FAIL(ObDFMUtil::check_semantic(dfm_elems, elem_flags, ob_time.mode_))) {
      LOG_WARN("check semantic of format string failed", K(ret), K(format));
    }

    int32_t temp_tzh_value = -1;  // positive value is legal
    int32_t temp_tzm_value = -1;  // positive value is legal
    int32_t temp_tz_factor = 0;

    int32_t tz_hour = 0;  // will be negetive when time zone offset < 0
    int32_t tz_min = 0;   // will be negetive when time zone offset < 0
    int32_t session_tz_id = 0;
    int32_t session_tran_type_id = 0;

    int32_t session_tz_offset = 0;
    // 2. set default value for ob_time
    if (OB_SUCC(ret)) {
      int64_t utc_curr_time = ObTimeUtility::current_time();
B
bf0 已提交
3326
      int64_t utc_curr_time_copy = utc_curr_time;
O
oceanbase-admin 已提交
3327 3328 3329 3330
      int32_t cur_date = 0;
      if (OB_ISNULL(cvrt_ctx.tz_info_)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("session timezone info is null", K(ret));
X
xy0 已提交
3331 3332 3333 3334 3335 3336
      } else if (sub_timezone_offset(*(cvrt_ctx.tz_info_),
                     ObString(),
                     utc_curr_time_copy,
                     session_tz_offset,
                     session_tz_id,
                     session_tran_type_id)) {
O
oceanbase-admin 已提交
3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385
        LOG_WARN("get session timezone offset failed", K(ret));
      } else if (OB_FAIL(datetime_to_date(utc_curr_time, cvrt_ctx.tz_info_, cur_date))) {
        LOG_WARN("timestamp to date failed", K(ret));
      } else if (OB_FAIL(time_to_ob_time(0, ob_time))) {
        LOG_WARN("time to ob_time failed", K(ret));
      } else if (OB_FAIL(date_to_ob_time(cur_date, ob_time))) {
        LOG_WARN("date to ob_time failed", K(ret), K(cur_date));
      } else {
        if (OB_INVALID_TZ_ID != session_tz_id && ob_is_timestamp_tz(target_type)) {
          ob_time.is_tz_name_valid_ = true;
        }
        ob_time.parts_[DT_MDAY] = 1;  // oracle default value is the first day of current month
        ob_time.parts_[DT_DATE] = 0;  // will be recalculated
        ob_time.parts_[DT_YDAY] = 0;  // doesn't matter
        ob_time.parts_[DT_WDAY] = 0;  // doesn't matter
        tz_hour = static_cast<int32_t>(session_tz_offset / MINS_PER_HOUR);
        tz_min = static_cast<int32_t>(session_tz_offset - MINS_PER_HOUR * tz_hour);
        if (OB_FAIL(ObDFMLimit::TIMEZONE_HOUR_ABS.validate(abs(tz_hour)))) {
          LOG_WARN("invalid session timezone hour", K(ret), K(tz_hour));
        } else if (OB_FAIL(ObDFMLimit::TIMEZONE_MIN_ABS.validate(abs(tz_min)))) {
          LOG_WARN("invali d session timezone minutes", K(ret), K(tz_min));
        }
      }
    }

    // 3. go through each element, set and check conflict for ob_time parts: Year, Month, Day, Hour, Minute, Second
    if (OB_SUCC(ret)) {
      ObDFMParseCtx ctx(str.ptr(), str.length());
      int64_t last_elem_end_pos = 0;
      int64_t conflict_part_bitset = 0;
      int64_t elem_idx = 0;
      int64_t input_sep_len = 0;
      int64_t part_blank1_len = 0;
      int64_t part_sep_len = 0;
      int64_t part_blank2_len = 0;
      int64_t format_sep_len = 0;
      static_assert((1 << TOTAL_PART_CNT) < INT64_MAX, "for time_part_conflict_bitset");
      int32_t yday_temp_value = ZERO_DATE;  // as invalid value
      int32_t wday_temp_value = ZERO_DATE;  // as invalid value
      int32_t date_temp_value = ZERO_DATE;  // as invalid value
      int32_t hh12_temp_value = ZERO_TIME;
      int32_t julian_year_value = ZERO_DATE;  // as invalid value
      bool is_after_noon = false;
      bool is_before_christ = false;
      bool has_digit_tz_in_TZR = false;
      int64_t first_non_space_sep_char = INT64_MAX;
      int64_t ignore_fs_flag = false;

      for (elem_idx = 0; OB_SUCC(ret) && elem_idx < dfm_elems.count(); ++elem_idx) {
X
xy0 已提交
3386
        ObDFMElem &elem = dfm_elems.at(elem_idx);
O
oceanbase-admin 已提交
3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462
        if (OB_UNLIKELY(!elem.is_valid())) {
          ret = OB_ERR_UNEXPECTED;
          LOG_WARN("element is invalid", K(ret), K(elem));
        } else {
          LOG_DEBUG("DFM DEBUG: start element", K(elem), K(ctx));
        }

        // 1. check separate chars and skip blank chars first
        if (OB_SUCC(ret)) {
          // get format sep chars len
          format_sep_len = elem.offset_ - last_elem_end_pos;
          last_elem_end_pos = elem.offset_ + ObDFMFlag::PATTERN[elem.elem_flag_].len_;
          // parse input string and skip them
          part_blank1_len = ObDFMUtil::skip_blank_chars(ctx);
          // The # of skipped non-blank chars is according to format_str
          first_non_space_sep_char = ctx.is_parse_finish() ? INT64_MAX : ctx.cur_ch_[0];
          if (ObDFMFlag::X == elem.elem_flag_) {
            part_sep_len = 0;
          } else {
            part_sep_len = ObDFMUtil::skip_separate_chars(ctx, format_sep_len);
          }
          part_blank2_len = ObDFMUtil::skip_blank_chars(ctx);
          input_sep_len = part_blank1_len + part_sep_len + part_blank2_len;
          LOG_DEBUG("DFM DEBUG: skip blank and serarate chars",
              K(part_blank1_len),
              K(part_sep_len),
              K(part_blank2_len),
              K(format_sep_len),
              K(ctx));
        }

        if (OB_SUCC(ret) && ctx.is_parse_finish()) {
          break;  // if all the input chars has beeen processed, break this loop
        }

        // 2. next, parse the current element
        if (OB_SUCC(ret)) {
          int64_t parsed_elem_len = 0;
          const int64_t expected_elem_len = ObDFMFlag::EXPECTED_MATCHING_LENGTH[elem.elem_flag_];
          ctx.set_next_expected_elem(elem.elem_flag_, format_sep_len > 0 && input_sep_len == 0);
          switch (elem.elem_flag_) {
            case ObDFMFlag::AD:
            case ObDFMFlag::BC:
            case ObDFMFlag::AD2:
            case ObDFMFlag::BC2: {  // TODO : NLS_LANGUAGE
              bool is_with_dot = (ObDFMFlag::AD2 == elem.elem_flag_ || ObDFMFlag::BC2 == elem.elem_flag_);
              parsed_elem_len =
                  is_with_dot ? ObDFMFlag::PATTERN[ObDFMFlag::AD2].len_ : ObDFMFlag::PATTERN[ObDFMFlag::AD].len_;
              bool is_ad = ObDFMUtil::match_pattern_ignore_case(
                  ctx, is_with_dot ? ObDFMFlag::PATTERN[ObDFMFlag::AD2] : ObDFMFlag::PATTERN[ObDFMFlag::AD]);
              bool is_bc = ObDFMUtil::match_pattern_ignore_case(
                  ctx, is_with_dot ? ObDFMFlag::PATTERN[ObDFMFlag::BC2] : ObDFMFlag::PATTERN[ObDFMFlag::BC]);
              if (!is_ad && !is_bc) {
                ret = OB_ERR_BC_OR_AD_REQUIRED;
              } else {
                is_before_christ = is_bc;
              }
              break;
            }
            case ObDFMFlag::D: {
              int32_t wday = 0;
              if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, wday))) {
                LOG_WARN("failed to match int value", K(ret));
              } else if (OB_FAIL(ObDFMLimit::WEEK_DAY.validate(wday))) {
                LOG_WARN("not a valid day of the week", K(ret), K(wday));
              } else {
                // oracle numbered sunday as 1 in territory of CHINA
                // TODO : hard code for now, need look up NLS_TERRITORIES
                wday_temp_value = (wday + 5) % 7 + 1;
              }
              break;
            }
            case ObDFMFlag::DY:
            case ObDFMFlag::DAY: {  // TODO : NLS_LANGUAGE NLS_TERRITORIES
              int32_t wday = 0;
              for (wday = 1; wday <= DAYS_PER_WEEK; ++wday) {
X
xy0 已提交
3463
                const ObTimeConstStr &day_str =
O
oceanbase-admin 已提交
3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613
                    (elem.elem_flag_ == ObDFMFlag::DAY) ? WDAY_NAMES[wday] : WDAY_ABBR_NAMES[wday];
                if (ObDFMUtil::match_pattern_ignore_case(ctx, day_str)) {
                  parsed_elem_len = day_str.len_;
                  break;
                }
              }
              if (OB_FAIL(ObDFMLimit::WEEK_DAY.validate(wday))) {
                LOG_WARN("validate week day failed", K(ret), K(wday));
              } else {
                wday_temp_value = wday;
              }
              break;
            }
            case ObDFMFlag::DD: {
              int32_t mday = 0;
              if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, mday))) {
                LOG_WARN("failed to match int value", K(ret));
              } else if (OB_FAIL(ObDFMLimit::MONTH_DAY.validate(mday))) {
                LOG_WARN("day of month must be between 1 and last day of month", K(ret), K(mday));
              } else {
                // may conflict with DDD
                ret = set_ob_time_part_directly(ob_time, conflict_part_bitset, DT_MDAY, mday);
              }
              break;
            }
            case ObDFMFlag::DDD: {
              int32_t yday = 0;
              if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, yday))) {
                LOG_WARN("failed to match int value", K(ret));
              } else if (OB_FAIL(ObDFMLimit::YEAR_DAY.validate(yday))) {
                LOG_WARN("day of year must be between 1 and 365 (366 for leap year)", K(ret), K(yday));
              } else {
                yday_temp_value = yday;
              }
              break;
            }
            case ObDFMFlag::DS: {  // TODO : impl it NEED NLS_DATE_FORMAT NLS_TERRITORY NLS_LANGUAGE
              ret = OB_NOT_SUPPORTED;
              LOG_WARN("DS is not supported now", K(ret));
              break;
            }
            case ObDFMFlag::DL: {  // TODO : impl it NEED NLS_DATE_FORMAT NLS_TERRITORY NLS_LANGUAGE
              ret = OB_NOT_SUPPORTED;
              LOG_WARN("DL is not supported now", K(ret));
              break;
            }
            case ObDFMFlag::FF:
            case ObDFMFlag::FF1:
            case ObDFMFlag::FF2:
            case ObDFMFlag::FF3:
            case ObDFMFlag::FF4:
            case ObDFMFlag::FF5:
            case ObDFMFlag::FF6:
            case ObDFMFlag::FF7:
            case ObDFMFlag::FF8:
            case ObDFMFlag::FF9: {
              int32_t usec = 0;
              // format string has '.' or 'X', but input string does not contain '.'
              // do nothing, skip element FF and revert ctx by the length of parsed chars
              if (ignore_fs_flag) {
                ctx.revert(part_blank1_len + part_sep_len + part_blank2_len);
              } else if (elem.is_single_dot_before_ && '.' != first_non_space_sep_char) {
                ctx.revert(part_sep_len + part_blank2_len);
              } else if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, usec))) {
                LOG_WARN("failed to match usecs", K(ret), K(ctx));
              } else {
                scale = static_cast<ObScale>(parsed_elem_len);
                usec = static_cast<int32_t>(usec * power_of_10[MAX_SCALE_FOR_ORACLE_TEMPORAL - parsed_elem_len]);
                ob_time.parts_[DT_USEC] = usec;
              }
              break;
            }

            case ObDFMFlag::TZH:
            case ObDFMFlag::TZM: {
              if (OB_UNLIKELY(elem_flags.has_member(ObDFMFlag::TZR) || elem_flags.has_member(ObDFMFlag::TZD))) {
                ret = OB_ERR_NOT_A_VALID_TIME_ZONE;
                LOG_WARN("tzh tzm and tzr tzd can not appear at the same time", K(ret));
              } else {
                // SQL> alter session set NLS_TIMESTAMP_TZ_FORMAT='DD-MON-RR HH.MI.SS AM TZH:TZM';
                // SQL> alter session set time_zone='Asia/Shanghai';
                // SQL> select cast('01-SEP-20 11.11.11' as timestamp with time zone) from dual;
                // 01-SEP-20 11.11.11 AM +08:00
                // If format contains TZH and time_zone is a position, need to convert first.
                ob_time.is_tz_name_valid_ = false;
                int32_t value = 0;
                int32_t local_tz_factor = 1;
                if (ObDFMFlag::TZH == elem.elem_flag_) {
                  if (ObDFMUtil::is_sign_char(ctx.cur_ch_[0])) {
                    local_tz_factor = ('-' == ctx.cur_ch_[0] ? -1 : 1);
                    ctx.update(1);
                  } else {
                    if (ctx.get_parsed_len() > 0 && input_sep_len > format_sep_len) {
                      // if the input valid separate chars > format separate chars
                      // the superfluous '-' will be regarded as minus sign
                      local_tz_factor = (static_cast<int64_t>('-') == ctx.cur_ch_[-1] ? -1 : 1);
                    }
                  }
                  temp_tz_factor = local_tz_factor;  // 1 or -1, but never be 0
                }

                if (ctx.is_parse_finish()) {
                  // do nothing
                } else if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, value))) {
                  LOG_WARN("failed to match usecs", K(ret), K(ctx));
                } else if (OB_FAIL((elem.elem_flag_ == ObDFMFlag::TZH)
                                       ? ObDFMLimit::TIMEZONE_HOUR_ABS.validate(value)
                                       : ObDFMLimit::TIMEZONE_MIN_ABS.validate(value))) {
                  LOG_WARN("failed to validate timezone value", K(value), K(ret));
                } else {
                  if (elem.elem_flag_ == ObDFMFlag::TZH) {
                    temp_tzh_value = value;
                  } else {
                    temp_tzm_value = value;
                  }
                }
              }
              break;
            }

            case ObDFMFlag::TZR: {
              if (OB_UNLIKELY(elem_flags.has_member(ObDFMFlag::TZH) || elem_flags.has_member(ObDFMFlag::TZM))) {
                ret = OB_ERR_NOT_A_VALID_TIME_ZONE;
                LOG_WARN("tzh tzm and tzr tzd can not appear at the same time", K(ret));
              } else {
                int32_t local_tz_factor = 1;
                if (isdigit(ctx.cur_ch_[0]) || ObDFMUtil::is_sign_char(ctx.cur_ch_[0])) {  // case1: digits
                  int32_t tmp_tz_hour = 0;
                  int32_t tmp_tz_min = 0;
                  if (ObDFMUtil::is_sign_char(ctx.cur_ch_[0])) {
                    local_tz_factor = ('-' == ctx.cur_ch_[0] ? -1 : 1);
                    ctx.update(1);
                  } else if (part_blank1_len + part_sep_len > format_sep_len &&
                             ObDFMUtil::is_sign_char(ctx.cur_ch_[-1])) {
                    local_tz_factor = ('-' == ctx.cur_ch_[-1] ? -1 : 1);
                  }

                  ObString digits_timezone;
                  parsed_elem_len = ObDFMUtil::UNKNOWN_LENGTH_OF_ELEMENT;
                  if (OB_LIKELY(!ctx.is_parse_finish())) {
                    // If tz str is not just character '+' or '-', result of convert should be
                    // offset instead of tz_id/name
                    ob_time.is_tz_name_valid_ = false;
                  }
                  if (OB_UNLIKELY(ctx.is_parse_finish())) {
                    // do nothing
                  } else if (OB_FAIL(ObDFMUtil::match_chars_until_space(ctx, digits_timezone, parsed_elem_len))) {
                    // do nothing
                  } else {
                    ObDFMParseCtx local_ctx(digits_timezone.ptr(), digits_timezone.length());
X
xy0 已提交
3614
                    const char *local_sep = ObDFMUtil::find_first_separator(local_ctx);
O
oceanbase-admin 已提交
3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775

                    if (OB_ISNULL(local_sep)) {
                      // timezone offset string is allowd to contain only hour part.
                    } else {
                      int64_t hour_expected_len = local_sep - digits_timezone.ptr();
                      int64_t local_parsed_len = 0;
                      if (OB_FAIL(ObDFMUtil::match_int_value(
                              local_ctx, hour_expected_len, local_parsed_len, tmp_tz_hour, local_tz_factor))) {
                        LOG_WARN("matching timezone hour failed, for 'TZR'. the error is ignored.",
                            K(ret),
                            K(hour_expected_len));
                      } else if (OB_FAIL(ObDFMLimit::TIMEZONE_HOUR_ABS.validate(abs(tmp_tz_hour)))) {
                        LOG_WARN("not valid timezone hour", K(ret), K(tmp_tz_hour));
                      } else if (OB_UNLIKELY(hour_expected_len != local_parsed_len)) {
                        ret = OB_INVALID_DATE_VALUE;  // invalid time zone
                      } else if (FALSE_IT(local_ctx.update(hour_expected_len + 1))) {
                      } else if (OB_UNLIKELY(local_ctx.is_parse_finish())) {
                        // When format contains TZR, tz str is allowed to be hour, which equals to hour:00.
                        has_digit_tz_in_TZR = true;
                        tz_hour = tmp_tz_hour;
                        tz_min = tmp_tz_min;
                      } else if (OB_FAIL(ObDFMUtil::match_int_value(local_ctx,
                                     parsed_elem_len - hour_expected_len - 1,
                                     local_parsed_len,
                                     tmp_tz_min,
                                     local_tz_factor))) {
                        LOG_WARN("matching timezone hour failed, for 'TZR'. the error is ignored.",
                            K(ret),
                            K(hour_expected_len));
                      } else if (OB_FAIL(ObDFMLimit::TIMEZONE_MIN_ABS.validate(abs(tmp_tz_min)))) {
                        LOG_WARN("not valid timezone hour", K(ret), K(tmp_tz_min));
                      } else if (OB_UNLIKELY(parsed_elem_len != hour_expected_len + local_parsed_len + 1)) {
                        ret = OB_INVALID_DATE_VALUE;  // invalid time zone
                      } else {
                        has_digit_tz_in_TZR = true;
                        tz_hour = tmp_tz_hour;
                        tz_min = tmp_tz_min;
                      }
                    }
                  }
                } else {  // case2: strings
                  ObString tzr_str;
                  parsed_elem_len = OB_MAX_TZ_NAME_LEN - 1;
                  if (OB_FAIL(ObDFMUtil::match_chars_until_space(ctx, tzr_str, parsed_elem_len))) {
                    LOG_WARN("failed to match tzr", K(ret));
                  } else {
                    MEMCPY(ob_time.tz_name_, tzr_str.ptr(), tzr_str.length());
                    ob_time.tz_name_[tzr_str.length()] = '\0';
                    ob_time.is_tz_name_valid_ = true;
                  }
                }
              }
              break;
            }

            case ObDFMFlag::TZD: {
              if (OB_UNLIKELY(elem_flags.has_member(ObDFMFlag::TZH) || elem_flags.has_member(ObDFMFlag::TZM))) {
                ret = OB_ERR_NOT_A_VALID_TIME_ZONE;
                LOG_WARN("tzh tzm and tzr tzd can not appear at the same time", K(ret));
              } else if (OB_UNLIKELY(has_digit_tz_in_TZR)) {
                ret = OB_INVALID_DATE_FORMAT;
                LOG_WARN("digit TZR with TZD is invalid", K(ret));
              } else {
                ObString tzd_str;
                parsed_elem_len = OB_MAX_TZ_ABBR_LEN - 1;
                if (OB_FAIL(ObDFMUtil::match_chars_until_space(ctx, tzd_str, parsed_elem_len))) {
                  LOG_WARN("failed to match tzd", K(ret));
                } else {
                  MEMCPY(ob_time.tzd_abbr_, tzd_str.ptr(), tzd_str.length());
                  ob_time.tzd_abbr_[tzd_str.length()] = '\0';
                  // ob_time.is_tz_name_valid_ = true;
                }
              }
              break;
            }

            case ObDFMFlag::J: {
              const int32_t base_julian_day = 2378497;  // julian day of 1800-01-01
              const int32_t base_date = -62091;         // ob_time.parts_[DT_DATE] of 1800-01-01
              int32_t julian_day = 0;
              int32_t ob_time_date = 0;
              ObTime tmp_ob_time;
              if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, julian_day))) {
                LOG_WARN("failed to match int value", K(ret));
              } else if (OB_FAIL(ObDFMLimit::JULIAN_DATE.validate(julian_day))) {
                LOG_WARN("julian_day must between 1 and 5373484", K(ret), K(julian_day));
              } else if (julian_day < base_julian_day) {
                ret = OB_NOT_SUPPORTED;
                LOG_WARN("julian day of date before 1800-01-01 not supported", K(ret));
              } else if (FALSE_IT(ob_time_date = julian_day - base_julian_day + base_date)) {
              } else if (OB_FAIL(date_to_ob_time(ob_time_date, tmp_ob_time))) {
                LOG_WARN("date to ob time failed", K(ret));
              } else if (OB_FAIL(set_ob_time_year_may_conflict(ob_time,
                             julian_year_value,
                             tmp_ob_time.parts_[DT_YEAR],
                             tmp_ob_time.parts_[DT_YEAR],
                             true /* overwrite */))) {
                LOG_WARN("set ob_time_year conflict", K(ret));
              } else {
                yday_temp_value = tmp_ob_time.parts_[DT_YDAY];
              }
              break;
            }

            case ObDFMFlag::HH:
            case ObDFMFlag::HH12: {
              int32_t hour = 0;
              if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, hour))) {
                LOG_WARN("failed to match int value", K(ret));
              } else if (OB_FAIL(ObDFMLimit::HOUR12.validate(hour))) {
                LOG_WARN("hour must be between 1 and 12", K(ret), K(hour));
              } else if (!ObDFMUtil::elem_has_meridian_indicator(elem_flags)) {
                ret = set_ob_time_part_may_conflict(ob_time, conflict_part_bitset, DT_HOUR, hour);
              } else {
                hh12_temp_value = hour;
              }
              break;
            }
            case ObDFMFlag::HH24: {
              int32_t hour = 0;
              if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, hour))) {
                LOG_WARN("failed to match int value", K(ret));
              } else if (OB_FAIL(ObDFMLimit::HOUR24.validate(hour))) {
                LOG_WARN("hour must be between 0 and 23", K(ret), K(hour));
              } else if (OB_UNLIKELY(ObDFMUtil::elem_has_meridian_indicator(elem_flags))) {
                ret = OB_INVALID_MERIDIAN_INDICATOR_USE;
                LOG_WARN("HH24 appears with meridian indicator", K(ret), K(format));
              } else {
                // may conflict with SSSSS
                ret = set_ob_time_part_may_conflict(ob_time, conflict_part_bitset, DT_HOUR, hour);
              }
              break;
            }
            case ObDFMFlag::MI: {
              int32_t min = 0;
              if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, min))) {
                LOG_WARN("failed to match int value", K(ret));
              } else if (OB_FAIL(ObDFMLimit::MINUTE.validate(min))) {
                LOG_WARN("minutes must be between 0 and 59", K(ret), K(min));
              } else {
                // may conflict with SSSSS
                ret = set_ob_time_part_may_conflict(ob_time, conflict_part_bitset, DT_MIN, min);
              }
              break;
            }
            case ObDFMFlag::MM: {
              int32_t mon = 0;
              if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, mon))) {
                LOG_WARN("failed to match int value", K(ret));
              } else if (OB_FAIL(ObDFMLimit::MONTH_DAY.validate(mon))) {
                LOG_WARN("not a valid month", K(ret), K(mon));
              } else {
                // may conflict with DDD
                ret = set_ob_time_part_directly(ob_time, conflict_part_bitset, DT_MON, mon);
              }
              break;
            }
            case ObDFMFlag::MON:
            case ObDFMFlag::MONTH: {
              int32_t mon = 0;
              for (mon = ObDFMLimit::MONTH.MIN; mon <= ObDFMLimit::MONTH.MAX; ++mon) {
X
xy0 已提交
3776
                const ObTimeConstStr &mon_str =
O
oceanbase-admin 已提交
3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143
                    (elem.elem_flag_ == ObDFMFlag::MONTH) ? MON_NAMES[mon] : MON_ABBR_NAMES[mon];
                if (ObDFMUtil::match_pattern_ignore_case(ctx, mon_str)) {
                  parsed_elem_len = mon_str.len_;
                  break;
                }
              }
              if (OB_FAIL(ObDFMLimit::MONTH.validate(mon))) {
                LOG_WARN("not a valid month", K(ret), K(mon));
              } else {
                // may conflict with DDD
                ret = set_ob_time_part_directly(ob_time, conflict_part_bitset, DT_MON, mon);
              }
              break;
            }
            case ObDFMFlag::AM:
            case ObDFMFlag::PM:
            case ObDFMFlag::AM2:
            case ObDFMFlag::PM2: {
              bool is_with_dot = (ObDFMFlag::AM2 == elem.elem_flag_ || ObDFMFlag::PM2 == elem.elem_flag_);
              parsed_elem_len =
                  is_with_dot ? ObDFMFlag::PATTERN[ObDFMFlag::AM2].len_ : ObDFMFlag::PATTERN[ObDFMFlag::AM].len_;
              bool is_am = ObDFMUtil::match_pattern_ignore_case(
                  ctx, is_with_dot ? ObDFMFlag::PATTERN[ObDFMFlag::AM2] : ObDFMFlag::PATTERN[ObDFMFlag::AM]);
              bool is_pm = ObDFMUtil::match_pattern_ignore_case(
                  ctx, is_with_dot ? ObDFMFlag::PATTERN[ObDFMFlag::PM2] : ObDFMFlag::PATTERN[ObDFMFlag::PM]);
              if (OB_UNLIKELY(!is_am && !is_pm)) {
                ret = OB_ERR_AM_OR_PM_REQUIRED;
                LOG_WARN("AM/A.M. or PM/P.M. required", K(ret));
              } else {
                is_after_noon = is_pm;
              }
              break;
            }
            case ObDFMFlag::RR:
            case ObDFMFlag::RRRR: {
              int32_t round_year = 0;
              int32_t conflict_check_year = 0;
              if (OB_FAIL(ObDFMUtil::match_int_value(ctx, 4, parsed_elem_len, round_year))) {
                LOG_WARN("failed to match int value", K(ret));
              } else if (parsed_elem_len > 2) {
                conflict_check_year = round_year;
                // do nothing
              } else {
                conflict_check_year = round_year;
                int32_t first_two_digits_of_current_year = (ob_time.parts_[DT_YEAR] / 100) % 100;
                int32_t last_two_digits_of_current_year = ob_time.parts_[DT_YEAR] % 100;
                if (round_year < 50) {  // 0~49
                  if (last_two_digits_of_current_year < 50) {
                    round_year += first_two_digits_of_current_year * 100;
                  } else {
                    round_year += (first_two_digits_of_current_year + 1) * 100;
                  }
                } else if (round_year < 100) {  // 50~99
                  if (last_two_digits_of_current_year < 50) {
                    round_year += (first_two_digits_of_current_year - 1) * 100;
                  } else {
                    round_year += first_two_digits_of_current_year * 100;
                  }
                }
              }
              if (OB_SUCC(ret)) {
                if (OB_FAIL(ObDFMLimit::YEAR.validate(round_year))) {  // TODO : negetive year number
                  LOG_WARN("(full) year must be between -4713 and +9999, and not be 0", K(ret), K(round_year));
                } else if (OB_FAIL(set_ob_time_year_may_conflict(
                               ob_time, julian_year_value, conflict_check_year, round_year, false /* overwrite */))) {
                  LOG_WARN("set ob_time_year conflict", K(ret));
                }
              }
              break;
            }
            case ObDFMFlag::SS: {
              int32_t sec = 0;
              if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, sec))) {
                LOG_WARN("failed to match int value", K(ret));
              } else if (OB_FAIL(ObDFMLimit::SECOND.validate(sec))) {
                LOG_WARN("seconds must be between 0 and 59", K(ret), K(sec));
              } else {
                ret = set_ob_time_part_may_conflict(ob_time, conflict_part_bitset, DT_SEC, sec);
              }
              break;
            }
            case ObDFMFlag::SSSSS: {
              int32_t sec_past_midnight = 0;
              if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, sec_past_midnight))) {
                LOG_WARN("failed to match int value", K(ret));
              } else if (OB_FAIL(ObDFMLimit::SECS_PAST_MIDNIGHT.validate(sec_past_midnight))) {
                LOG_WARN("seconds in day must be between 0 and 86399", K(ret), K(sec_past_midnight));
              } else {
                int32_t secs = sec_past_midnight % static_cast<int32_t>(SECS_PER_MIN);
                int32_t mins =
                    (sec_past_midnight / static_cast<int32_t>(SECS_PER_MIN)) % static_cast<int32_t>(MINS_PER_HOUR);
                int32_t hours =
                    sec_past_midnight / static_cast<int32_t>(SECS_PER_MIN) / static_cast<int32_t>(MINS_PER_HOUR);
                if (OB_FAIL(set_ob_time_part_may_conflict(ob_time, conflict_part_bitset, DT_SEC, secs))) {
                  LOG_WARN("set ob time conflict", K(ret), K(secs), K(ob_time));
                } else if (OB_FAIL(set_ob_time_part_may_conflict(ob_time, conflict_part_bitset, DT_MIN, mins))) {
                  LOG_WARN("set ob time conflict", K(ret), K(mins), K(ob_time));
                } else if (OB_FAIL(set_ob_time_part_may_conflict(ob_time, conflict_part_bitset, DT_HOUR, hours))) {
                  LOG_WARN("set ob time conflict", K(ret), K(hours), K(ob_time));
                }
              }
              break;
            }
            case ObDFMFlag::YGYYY: {
              int32_t years = 0;
              if (OB_FAIL(ObDFMUtil::match_int_value_with_comma(ctx, expected_elem_len, parsed_elem_len, years))) {
                LOG_WARN("failed to match int value", K(ret));
              } else if (OB_FAIL(ObDFMLimit::YEAR.validate(years))) {
                LOG_WARN("(full) year must be between -4713 and +9999, and not be 0", K(ret), K(years));
              } else if (OB_FAIL(set_ob_time_year_may_conflict(
                             ob_time, julian_year_value, years, years, false /* overwrite */))) {
                LOG_WARN("set ob_time_year conflict", K(ret));
              }
              break;
            }
            case ObDFMFlag::SYYYY:
            case ObDFMFlag::YYYY:
            case ObDFMFlag::YYY:
            case ObDFMFlag::YY:
            case ObDFMFlag::Y: {
              int32_t years = 0;
              int32_t conflict_check_year = 0;
              int32_t sign = 1;
              if (ObDFMFlag::SYYYY == elem.elem_flag_) {
                if (ObDFMUtil::is_sign_char(ctx.cur_ch_[0])) {
                  sign = ('-' == ctx.cur_ch_[0] ? -1 : 1);
                  ctx.update(1);
                } else if (part_blank1_len + part_sep_len > format_sep_len &&
                           ObDFMUtil::is_sign_char(ctx.cur_ch_[-1])) {
                  sign = ('-' == ctx.cur_ch_[-1] ? -1 : 1);
                }
              }
              if (OB_UNLIKELY(!ctx.is_valid())) {
              } else if (OB_FAIL(ObDFMUtil::match_int_value(ctx, expected_elem_len, parsed_elem_len, years, sign))) {
                LOG_WARN("failed to match int value", K(ret));
              }
              if (OB_SUCC(ret)) {
                conflict_check_year = years;
                if (expected_elem_len < 4) {
                  years += (ob_time.parts_[DT_YEAR] / static_cast<int32_t>(power_of_10[parsed_elem_len])) *
                           static_cast<int32_t>(power_of_10[parsed_elem_len]);
                }
                if (OB_FAIL(ObDFMLimit::YEAR.validate(years))) {
                  LOG_WARN("(full) year must be between -4713 and +9999, and not be 0", K(ret), K(years));
                } else if (OB_FAIL(set_ob_time_year_may_conflict(
                               ob_time, julian_year_value, conflict_check_year, years, false /* overwrite */))) {
                  LOG_WARN("set ob_time_year conflict", K(ret));
                }
              }
              break;
            }
            case ObDFMFlag::X: {
              if (OB_UNLIKELY('.' != ctx.cur_ch_[0])) {
                ignore_fs_flag = true;
                // revert equal to 'X' not exist
                ctx.revert(part_blank1_len + part_sep_len + part_blank2_len);
              } else {
                parsed_elem_len = 1;
              }
              break;
            }

            case ObDFMFlag::CC:
            case ObDFMFlag::SCC:
            case ObDFMFlag::IW:
            case ObDFMFlag::W:
            case ObDFMFlag::WW:
            case ObDFMFlag::YEAR:
            case ObDFMFlag::SYEAR:
            case ObDFMFlag::Q:
            case ObDFMFlag::I:
            case ObDFMFlag::IY:
            case ObDFMFlag::IYY:
            case ObDFMFlag::IYYY: {
              ret = OB_ERR_FORMAT_CODE_CANNOT_APPEAR;
              LOG_WARN("element can not appear", K(ret), K(elem));
              break;
            }
            default: {
              ret = OB_INVALID_DATE_FORMAT;
              LOG_WARN("unsupport element", K(ret), K(elem));
              break;
            }
          }  // end switch

          if (OB_SUCC(ret)) {
            ctx.update(parsed_elem_len);
          }
        }  // end if

        if (OB_FAIL(ret)) {
          LOG_WARN("failed to convert string to ob time by oracle dfm", K(ret), K(elem), K(ctx));
        } else {
          LOG_DEBUG("DFM DEBUG: finish element", K(elem), K(ctx));
        }
      }  // end for

      // check if the unprocessed elems has permission to be omitted
      if (OB_SUCC(ret)) {
        for (; elem_idx < dfm_elems.count(); ++elem_idx) {
          if (OB_UNLIKELY(!ObDFMUtil::is_element_can_omit(dfm_elems[elem_idx]))) {
            ret = OB_ERR_INPUT_VALUE_NOT_LONG_ENOUGH;
            LOG_WARN("input value not long enough for date format", K(ret));
          }
        }
      }

      if (OB_SUCC(ret)) {
        // all elems has finished, is there anything else in str? the rest must be separators, do check.
        int64_t str_remain_sep_len = 0;
        while (str_remain_sep_len < ctx.remain_len_ && ObDFMUtil::is_split_char(ctx.cur_ch_[str_remain_sep_len])) {
          str_remain_sep_len++;
        }
        ctx.update(str_remain_sep_len);
        if (ctx.remain_len_ > 0) {
          ret = OB_INVALID_DATE_FORMAT_END;
          LOG_WARN("input value has not finished yet", K(ctx.remain_len_), K(format), K(str), K(ret));
        }
      }

      // after noon conflict: AM PM vs HH12 HH
      if (OB_SUCC(ret)) {
        if (hh12_temp_value != ZERO_TIME) {
          // when  hour value, varied by meridian indicators
          // if HH12 = 12, when meridian indicator 'AM' exists, the real time is hour = 0
          // if HH12 = 12, when meridian indicator 'PM' exists, the real time is hour = 12
          if (is_after_noon) {
            ret = set_ob_time_part_may_conflict(ob_time, conflict_part_bitset, DT_HOUR, hh12_temp_value % 12 + 12);
          } else {
            ret = set_ob_time_part_may_conflict(ob_time, conflict_part_bitset, DT_HOUR, hh12_temp_value % 12);
          }
        }
      }

      // before christ conflict: BC AD vs YEAR   //TODO : change this when ob_time support negetive years
      if (OB_SUCC(ret)) {
        if (is_before_christ) {
          ret = OB_ERR_INVALID_YEAR_VALUE;
          LOG_WARN("before christ is not supported now!", K(ret));
        }
      }

      // year cannot changed after this line
      // feed/validate yday + YEAR to MON and DAY
      if (OB_SUCC(ret)) {
        if (yday_temp_value != ZERO_DATE) {
          int32_t month = 0;
          int32_t day = 0;
          if (OB_FAIL(get_day_and_month_from_year_day(yday_temp_value, ob_time.parts_[DT_YEAR], month, day))) {
            LOG_WARN("failed to get day and month from year day", K(ret), K(yday_temp_value), K(ob_time));
          } else if (OB_FAIL(set_ob_time_part_may_conflict(ob_time, conflict_part_bitset, DT_MON, month))) {
            LOG_WARN("failed to set ob time part with conflict", K(ret), K(month), K(ob_time));
          } else if (OB_FAIL(set_ob_time_part_may_conflict(ob_time, conflict_part_bitset, DT_MDAY, day))) {
            LOG_WARN("failed to set ob time part with conflict", K(ret), K(day), K(ob_time));
          }
        }
      }

      // calc and validate: YDAY WDAY vs YEAR MON DAY
      if (OB_SUCC(ret)) {
        if (OB_FAIL(validate_oracle_date(ob_time))) {
          LOG_WARN("date is invalid or out of range", K(ret), K(str));
        } else {
          // ob_time_to_date func is to calc YDAY and WDAY and return DATE
          date_temp_value = ob_time_to_date(ob_time);  // TODO: shanting
          if (yday_temp_value != ZERO_DATE && OB_UNLIKELY(ob_time.parts_[DT_YDAY] != yday_temp_value)) {
            ret = OB_ERR_DAY_OF_YEAR_CONFLICTS_WITH_JULIAN_DATE;
          } else if (wday_temp_value != ZERO_DATE && OB_UNLIKELY(ob_time.parts_[DT_WDAY] != wday_temp_value)) {
            ret = OB_ERR_DAY_OF_WEEK_CONFLICTS_WITH_JULIAN_DATE;
          } else {
            ob_time.parts_[DT_DATE] = date_temp_value;
          }
        }
      }

      // for time zone info
      if (OB_SUCC(ret) && ob_is_timestamp_tz(target_type)) {
        if (ob_time.is_tz_name_valid_) {  // A. timezone defined by names
          if (ob_time.get_tz_name_str().empty()) {
            // string not contains TZR, use tz_name of sessiontimezone as default value.
            int64_t pos = 0;
            if (OB_FAIL(cvrt_ctx.tz_info_->timezone_to_str(ob_time.tz_name_, OB_MAX_TZ_NAME_LEN, pos))) {
              LOG_WARN("print tz name failed", K(ret));
            }
          }
          if (OB_SUCC(ret) && OB_FAIL(calc_tz_offset_by_tz_name(cvrt_ctx, ob_time))) {
            LOG_WARN("calc timezone offset failed", K(ret));
          }
        } else {  // B. timezone defined by time zone hour and minute
          int32_t tz_offset_value = 0;

          if (elem_flags.has_member(ObDFMFlag::TZH)) {
            bool has_tzh_value = (temp_tzh_value >= 0);
            bool has_tzm_value = (temp_tzm_value >= 0);
            if (OB_UNLIKELY(!has_tzh_value && has_tzm_value)) {
              ret = OB_INVALID_DATE_VALUE;
              LOG_WARN("only TZM match, TZH is not found", K(ret));
            } else if (!has_tzh_value && !has_tzm_value) {
              // do nothing
            } else if (has_tzh_value && !has_tzm_value) {
              tz_hour = temp_tz_factor * temp_tzh_value;
              tz_min = temp_tz_factor * abs(tz_min);
            } else {
              tz_hour = temp_tz_factor * temp_tzh_value;
              tz_min = temp_tz_factor * temp_tzm_value;
            }
          } else if (elem_flags.has_member(ObDFMFlag::TZR)) {
            // do nothing
          } else {
            // no time zone info in elem_flags
          }

          // calc offset
          if (OB_SUCC(ret)) {
            if (OB_UNLIKELY(tz_hour * tz_min < 0)) {
              ret = OB_ERR_UNEXPECTED;
              LOG_WARN("tz_hour and tz_min cantains counter arithmetic symbols", K(ret));
            } else {
              tz_offset_value = static_cast<int32_t>(tz_hour * MINS_PER_HOUR + tz_min);
            }
          }

          // final validate
          if (OB_SUCC(ret)) {
            if (OB_UNLIKELY(!ObOTimestampData::is_valid_offset_min(tz_offset_value))) {
              ret = OB_INVALID_DATE_VALUE;
              LOG_WARN("validate timezone offset failed", K(ret), K(tz_offset_value));
            } else {
              ob_time.parts_[DT_OFFSET_MIN] = tz_offset_value;
            }
          }
        }
      }

      if (OB_FAIL(ret)) {
        LOG_WARN("failed to convert string to ob_time",
            K(format),
            K(ob_time),
            K(conflict_part_bitset),
            K(yday_temp_value),
            K(wday_temp_value),
            K(date_temp_value),
            K(hh12_temp_value),
            K(tz_hour),
            K(tz_min),
            K(is_after_noon),
            K(is_before_christ));
      } else {
        LOG_DEBUG("convert from string to ob_time",
            K(format),
            K(ob_time),
            K(conflict_part_bitset),
            K(yday_temp_value),
            K(wday_temp_value),
            K(date_temp_value),
            K(hh12_temp_value),
            K(tz_hour),
            K(tz_min),
            K(is_after_noon),
            K(is_before_christ));
      }
    }  // end if
  }

  return ret;
}
// check parts of obtime before print timestamp in oracle mode
X
xy0 已提交
4144
bool ObTimeConverter::valid_oracle_year(const ObTime &ob_time)
O
oceanbase-admin 已提交
4145 4146 4147 4148 4149 4150 4151 4152 4153
{
  int ret = true;
  if (ob_time.parts_[DT_YEAR] < 0 || ob_time.parts_[DT_YEAR] > 9999) {
    ret = false;
  }
  return ret;
}

int ObTimeConverter::ob_time_to_str_oracle_dfm(
X
xy0 已提交
4154
    const ObTime &ob_time, ObScale scale, const ObString &format, char *buf, int64_t buf_len, int64_t &pos)
O
oceanbase-admin 已提交
4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(buf) || OB_UNLIKELY(buf_len <= 0) || OB_UNLIKELY(format.empty()) ||
      OB_UNLIKELY(scale > MAX_SCALE_FOR_ORACLE_TEMPORAL)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid arguments", K(ret), K(format), K(buf), K(buf_len), K(ob_time), K(scale));
  } else if (!valid_oracle_year(ob_time)) {
    ret = OB_ERR_DATETIME_INTERVAL_INTERNAL_ERROR;
    LOG_WARN("invalid oracle timestamp", K(ret), K(ob_time));
  } else {
    if (scale < 0) {
      scale = DEFAULT_SCALE_FOR_ORACLE_FRACTIONAL_SECONDS;
    }

    int32_t iso_week = 0;
    int32_t iso_year_delta = 0;
    bool is_iso_week_calced = false;
    // avoid repeat calc iso_week.
    auto calc_iso_week = [&](void) {
      if (!is_iso_week_calced) {
        iso_week = ob_time_to_week(ob_time, WEEK_MODE[3], iso_year_delta);
        is_iso_week_calced = true;
      }
    };

X
xy0 已提交
4180
    const char *const format_begin_ptr = format.ptr();
O
oceanbase-admin 已提交
4181 4182 4183 4184 4185 4186 4187 4188 4189 4190
    int64_t last_elem_end_pos = 0;
    ObSEArray<ObDFMElem, ObDFMUtil::COMMON_ELEMENT_NUMBER> dfm_elems;

    // 1. parse element from format string
    if (OB_FAIL(ObDFMUtil::parse_datetime_format_string(format, dfm_elems))) {
      LOG_WARN("fail to parse oracle datetime format string", K(ret), K(format));
    }

    // 2. print each element
    for (int64_t i = 0; OB_SUCC(ret) && i < dfm_elems.count(); ++i) {
X
xy0 已提交
4191
      ObDFMElem &elem = dfm_elems.at(i);
O
oceanbase-admin 已提交
4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217

      // element is valid
      if (OB_UNLIKELY(!elem.is_valid())) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("element is invalid", K(ret), K(elem));
      }

      // print separate chars between elements
      if (OB_SUCC(ret)) {
        int64_t separate_chars_len = elem.offset_ - last_elem_end_pos;
        if (separate_chars_len > 0) {
          ret = databuff_printf(buf,
              buf_len,
              pos,
              "%.*s",
              static_cast<int32_t>(separate_chars_len),
              format_begin_ptr + last_elem_end_pos);
        }
        last_elem_end_pos = elem.offset_ + ObDFMFlag::PATTERN[elem.elem_flag_].len_;
      }

      // print current elem
      if (OB_SUCC(ret)) {
        switch (elem.elem_flag_) {
          case ObDFMFlag::AD:
          case ObDFMFlag::BC: {  // TODO : NLS_LANGUAGE
X
xy0 已提交
4218
            const ObTimeConstStr &target_str =
O
oceanbase-admin 已提交
4219 4220 4221 4222 4223 4224
                ob_time.parts_[DT_YEAR] > 0 ? ObDFMFlag::PATTERN[ObDFMFlag::AD] : ObDFMFlag::PATTERN[ObDFMFlag::BC];
            ret = ObDFMUtil::special_mode_sprintf(buf, buf_len, pos, target_str, elem.upper_case_mode_);
            break;
          }
          case ObDFMFlag::AD2:
          case ObDFMFlag::BC2: {  // TODO : NLS_LANGUAGE
X
xy0 已提交
4225
            const ObTimeConstStr &str =
O
oceanbase-admin 已提交
4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243
                ob_time.parts_[DT_YEAR] > 0 ? ObDFMFlag::PATTERN[ObDFMFlag::AD2] : ObDFMFlag::PATTERN[ObDFMFlag::BC2];
            ret = ObDFMUtil::special_mode_sprintf(buf, buf_len, pos, str, elem.upper_case_mode_);
            break;
          }
          case ObDFMFlag::CC: {
            ret = databuff_printf(buf, buf_len, pos, "%02d", (abs(ob_time.parts_[DT_YEAR]) + 99) / 100);
            break;
          }
          case ObDFMFlag::SCC: {
            char symbol = ob_time.parts_[DT_YEAR] > 0 ? ' ' : '-';
            ret = databuff_printf(buf, buf_len, pos, "%c%02d", symbol, (abs(ob_time.parts_[DT_YEAR]) + 99) / 100);
            break;
          }
          case ObDFMFlag::D: {
            ret = databuff_printf(buf, buf_len, pos, "%d", ob_time.parts_[DT_WDAY] % 7 + 1);
            break;
          }
          case ObDFMFlag::DAY: {  // TODO : NLS_LANGUAGE
X
xy0 已提交
4244
            const ObTimeConstStr &day_str = WDAY_NAMES[ob_time.parts_[DT_WDAY]];
O
oceanbase-admin 已提交
4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257
            ret = ObDFMUtil::special_mode_sprintf(
                buf, buf_len, pos, day_str, elem.upper_case_mode_, MAX_WDAY_NAME_LENGTH);
            break;
          }
          case ObDFMFlag::DD: {
            ret = databuff_printf(buf, buf_len, pos, "%02d", ob_time.parts_[DT_MDAY]);
            break;
          }
          case ObDFMFlag::DDD: {
            ret = databuff_printf(buf, buf_len, pos, "%03d", ob_time.parts_[DT_YDAY]);
            break;
          }
          case ObDFMFlag::DY: {  // TODO: 1. NLS_LANGUAGE
X
xy0 已提交
4258
            const ObTimeConstStr &day_str = WDAY_ABBR_NAMES[ob_time.parts_[DT_WDAY]];
O
oceanbase-admin 已提交
4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272
            ret = ObDFMUtil::special_mode_sprintf(buf, buf_len, pos, day_str, elem.upper_case_mode_);
            break;
          }
          case ObDFMFlag::DS: {  // TODO: 1. NLS_TERRITORY 2. NLS_LANGUAGE
            ret = databuff_printf(buf,
                buf_len,
                pos,
                "%02d/%02d/%d",
                ob_time.parts_[DT_MON],
                ob_time.parts_[DT_MDAY],
                ob_time.parts_[DT_YEAR]);
            break;
          }
          case ObDFMFlag::DL: {  // TODO: 1. NLS_DATE_FORMAT 2. NLS_TERRITORY 3. NLS_LANGUAGE
X
xy0 已提交
4273 4274
            const ObTimeConstStr &wday_str = WDAY_NAMES[ob_time.parts_[DT_WDAY]];
            const ObTimeConstStr &mon_str = MON_NAMES[ob_time.parts_[DT_MON]];
O
oceanbase-admin 已提交
4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360
            ret = databuff_printf(buf,
                buf_len,
                pos,
                "%.*s, %.*s %02d, %d",
                wday_str.len_,
                wday_str.ptr_,
                mon_str.len_,
                mon_str.ptr_,
                ob_time.parts_[DT_MDAY],
                ob_time.parts_[DT_YEAR]);
            break;
          }
          case ObDFMFlag::FF: {
            if (OB_UNLIKELY(!HAS_TYPE_ORACLE(ob_time.mode_))) {
              ret = OB_INVALID_DATE_FORMAT;
            } else if (0 == scale) {
              // print nothing
            } else {
              ret = data_fmt_nd(buf,
                  buf_len,
                  pos,
                  scale,
                  ob_time.parts_[DT_USEC] / power_of_10[MAX_SCALE_FOR_ORACLE_TEMPORAL - scale]);
            }
            break;
          }
          case ObDFMFlag::FF1:
          case ObDFMFlag::FF2:
          case ObDFMFlag::FF3:
          case ObDFMFlag::FF4:
          case ObDFMFlag::FF5:
          case ObDFMFlag::FF6:
          case ObDFMFlag::FF7:
          case ObDFMFlag::FF8:
          case ObDFMFlag::FF9: {
            int64_t scale = elem.elem_flag_ - ObDFMFlag::FF1 + 1;
            if (OB_UNLIKELY(!HAS_TYPE_ORACLE(ob_time.mode_))) {
              ret = OB_INVALID_DATE_FORMAT;
            } else {
              ret = data_fmt_nd(buf,
                  buf_len,
                  pos,
                  scale,
                  ob_time.parts_[DT_USEC] / power_of_10[MAX_SCALE_FOR_ORACLE_TEMPORAL - scale]);
            }
            break;
          }
          case ObDFMFlag::HH:
          case ObDFMFlag::HH12: {
            int32_t h = ob_time.parts_[DT_HOUR] % 12;
            if (0 == h) {
              h = 12;
            }
            ret = databuff_printf(buf, buf_len, pos, "%02d", h);
            break;
          }
          case ObDFMFlag::HH24: {
            int32_t h = ob_time.parts_[DT_HOUR];
            ret = databuff_printf(buf, buf_len, pos, "%02d", h);
            break;
          }
          case ObDFMFlag::IW: {
            calc_iso_week();
            ret = databuff_printf(buf, buf_len, pos, "%02d", iso_week);
            break;
          }
          case ObDFMFlag::J: {
            const int32_t base_julian_day = 2378497;  // julian day of 1800-01-01
            const int32_t base_date = -62091;         // ob_time.parts_[DT_DATE] of 1800-01-01
            if (ob_time.parts_[DT_DATE] < base_date) {
              ret = OB_NOT_SUPPORTED;
            } else {
              int32_t julian_day = base_julian_day + ob_time.parts_[DT_DATE] - base_date;
              ret = databuff_printf(buf, buf_len, pos, "%07d", julian_day);
            }
            break;
          }
          case ObDFMFlag::MI: {
            ret = databuff_printf(buf, buf_len, pos, "%02d", ob_time.parts_[DT_MIN]);
            break;
          }
          case ObDFMFlag::MM: {
            ret = databuff_printf(buf, buf_len, pos, "%02d", ob_time.parts_[DT_MON]);
            break;
          }
          case ObDFMFlag::MONTH: {
X
xy0 已提交
4361
            const ObTimeConstStr &mon_str = MON_NAMES[ob_time.parts_[DT_MON]];
O
oceanbase-admin 已提交
4362 4363 4364 4365 4366
            ret =
                ObDFMUtil::special_mode_sprintf(buf, buf_len, pos, mon_str, elem.upper_case_mode_, MAX_MON_NAME_LENGTH);
            break;
          }
          case ObDFMFlag::MON: {
X
xy0 已提交
4367
            const ObTimeConstStr &mon_str = MON_ABBR_NAMES[ob_time.parts_[DT_MON]];
O
oceanbase-admin 已提交
4368 4369 4370 4371 4372
            ret = ObDFMUtil::special_mode_sprintf(buf, buf_len, pos, mon_str, elem.upper_case_mode_);
            break;
          }
          case ObDFMFlag::AM:
          case ObDFMFlag::PM: {
X
xy0 已提交
4373
            const ObTimeConstStr &str =
O
oceanbase-admin 已提交
4374 4375 4376 4377 4378 4379
                ob_time.parts_[DT_HOUR] >= 12 ? ObDFMFlag::PATTERN[ObDFMFlag::PM] : ObDFMFlag::PATTERN[ObDFMFlag::AM];
            ret = ObDFMUtil::special_mode_sprintf(buf, buf_len, pos, str, elem.upper_case_mode_);
            break;
          }
          case ObDFMFlag::AM2:
          case ObDFMFlag::PM2: {
X
xy0 已提交
4380
            const ObTimeConstStr &str =
O
oceanbase-admin 已提交
4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430
                ob_time.parts_[DT_HOUR] >= 12 ? ObDFMFlag::PATTERN[ObDFMFlag::PM2] : ObDFMFlag::PATTERN[ObDFMFlag::AM2];
            ret = ObDFMUtil::special_mode_sprintf(buf, buf_len, pos, str, elem.upper_case_mode_);
            break;
          }
          case ObDFMFlag::Q: {
            ret = databuff_printf(buf, buf_len, pos, "%d", (ob_time.parts_[DT_MON] + 2) / 3);
            break;
          }
          case ObDFMFlag::RR: {
            ret = databuff_printf(buf, buf_len, pos, "%02d", (ob_time.parts_[DT_YEAR]) % 100);
            break;
          }
          case ObDFMFlag::RRRR: {
            ret = databuff_printf(buf, buf_len, pos, "%04d", ob_time.parts_[DT_YEAR]);
            break;
          }
          case ObDFMFlag::SS: {
            ret = databuff_printf(buf, buf_len, pos, "%02d", ob_time.parts_[DT_SEC]);
            break;
          }
          case ObDFMFlag::SSSSS: {
            ret = databuff_printf(buf,
                buf_len,
                pos,
                "%05d",
                ob_time.parts_[DT_HOUR] * 3600 + ob_time.parts_[DT_MIN] * 60 + ob_time.parts_[DT_SEC]);
            break;
          }
          case ObDFMFlag::WW: {  // the first complete week of a year
            ret = databuff_printf(buf, buf_len, pos, "%02d", (ob_time.parts_[DT_YDAY] - 1) / 7 + 1);
            break;
          }
          case ObDFMFlag::W: {  // the first complete week of a month
            ret = databuff_printf(buf, buf_len, pos, "%d", (ob_time.parts_[DT_MDAY] - 1) / 7 + 1);
            break;
          }
          case ObDFMFlag::YGYYY: {
            ret = databuff_printf(
                buf, buf_len, pos, "%d,%03d", abs(ob_time.parts_[DT_YEAR]) / 1000, abs(ob_time.parts_[DT_YEAR]) % 1000);
            break;
          }
          case ObDFMFlag::YEAR: {
            ret = OB_NOT_SUPPORTED;
            break;
          }
          case ObDFMFlag::SYEAR: {
            ret = OB_NOT_SUPPORTED;
            break;
          }
          case ObDFMFlag::SYYYY: {
X
xy0 已提交
4431
            const char *fmt_str = ob_time.parts_[DT_YEAR] < 0 ? "-%04d" : " %04d";
O
oceanbase-admin 已提交
4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488
            ret = databuff_printf(buf, buf_len, pos, fmt_str, abs(ob_time.parts_[DT_YEAR]));
            break;
          }
          case ObDFMFlag::YYYY: {
            ret = databuff_printf(buf, buf_len, pos, "%04d", abs(ob_time.parts_[DT_YEAR]));
            break;
          }
          case ObDFMFlag::YYY: {
            ret = databuff_printf(buf, buf_len, pos, "%03d", abs(ob_time.parts_[DT_YEAR] % 1000));
            break;
          }
          case ObDFMFlag::YY: {
            ret = databuff_printf(buf, buf_len, pos, "%02d", abs(ob_time.parts_[DT_YEAR] % 100));
            break;
          }
          case ObDFMFlag::Y: {
            ret = databuff_printf(buf, buf_len, pos, "%01d", abs(ob_time.parts_[DT_YEAR] % 10));
            break;
          }
          case ObDFMFlag::IYYY: {
            calc_iso_week();
            ret = databuff_printf(buf, buf_len, pos, "%04d", abs(ob_time.parts_[DT_YEAR] + iso_year_delta));
            break;
          }
          case ObDFMFlag::IYY: {
            calc_iso_week();
            ret = databuff_printf(buf, buf_len, pos, "%03d", abs((ob_time.parts_[DT_YEAR] + iso_year_delta) % 1000));
            break;
          }
          case ObDFMFlag::IY: {
            calc_iso_week();
            ret = databuff_printf(buf, buf_len, pos, "%02d", abs((ob_time.parts_[DT_YEAR] + iso_year_delta) % 100));
            break;
          }
          case ObDFMFlag::I: {
            calc_iso_week();
            ret = databuff_printf(buf, buf_len, pos, "%01d", abs((ob_time.parts_[DT_YEAR] + iso_year_delta) % 10));
            break;
          }
          case ObDFMFlag::TZD: {
            if (OB_UNLIKELY(!HAS_TYPE_ORACLE(ob_time.mode_))) {
              ret = OB_INVALID_DATE_FORMAT;
            } else if (OB_LIKELY(ob_time.time_zone_id_ != OB_INVALID_INDEX)) {
              ret = databuff_printf(
                  buf, buf_len, pos, "%.*s", ob_time.get_tzd_abbr_str().length(), ob_time.get_tzd_abbr_str().ptr());
            } else {
              // do nothing
            }
            break;
          }
          case ObDFMFlag::TZR: {
            if (OB_UNLIKELY(!HAS_TYPE_ORACLE(ob_time.mode_))) {
              ret = OB_INVALID_DATE_FORMAT;
            } else if (OB_LIKELY(ob_time.time_zone_id_ != OB_INVALID_INDEX)) {
              ret = databuff_printf(
                  buf, buf_len, pos, "%.*s", ob_time.get_tz_name_str().length(), ob_time.get_tz_name_str().ptr());
            } else {
X
xy0 已提交
4489
              const char *fmt_str = ob_time.parts_[DT_OFFSET_MIN] < 0 ? "-%02d:%02d" : "+%02d:%02d";
O
oceanbase-admin 已提交
4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502
              ret = databuff_printf(buf,
                  buf_len,
                  pos,
                  fmt_str,
                  abs(ob_time.parts_[DT_OFFSET_MIN]) / 60,
                  abs(ob_time.parts_[DT_OFFSET_MIN]) % 60);
            }
            break;
          }
          case ObDFMFlag::TZH: {
            if (OB_UNLIKELY(!HAS_TYPE_TIMEZONE(ob_time.mode_))) {
              ret = OB_INVALID_DATE_FORMAT;
            } else {
X
xy0 已提交
4503
              const char *fmt_str = ob_time.parts_[DT_OFFSET_MIN] < 0 ? "-%02d" : "+%02d";
O
oceanbase-admin 已提交
4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557
              ret = databuff_printf(buf, buf_len, pos, fmt_str, abs(ob_time.parts_[DT_OFFSET_MIN]) / 60);
            }
            break;
          }
          case ObDFMFlag::TZM: {
            if (OB_UNLIKELY(!HAS_TYPE_TIMEZONE(ob_time.mode_))) {
              ret = OB_INVALID_DATE_FORMAT;
            } else {
              ret = databuff_printf(buf, buf_len, pos, "%02d", abs(ob_time.parts_[DT_OFFSET_MIN]) % 60);
            }
            break;
          }
          case ObDFMFlag::X: {
            ret = databuff_printf(buf, buf_len, pos, ".");
            break;
          }

          default: {
            ret = OB_INVALID_DATE_FORMAT;
            LOG_WARN("unknown elem", K(ret), K(elem));
            break;
          }
        }  // end switch
        if (OB_FAIL(ret)) {
          LOG_WARN("failed to print buf", K(elem), K(ret));
        }
      }  // end if
    }    // end for

    if (OB_SUCC(ret)) {
      // print the rest separate chars
      int64_t separate_chars_len = format.length() - last_elem_end_pos;
      if (separate_chars_len > 0) {
        if (OB_FAIL(databuff_printf(buf,
                buf_len,
                pos,
                "%.*s",
                static_cast<int32_t>(separate_chars_len),
                format_begin_ptr + last_elem_end_pos))) {
          LOG_WARN("failed to print otimestamp", "buf", ObString(pos, buf), K(ret));
        }
      }
      LOG_DEBUG("succ to print otimestamp", "buf", ObString(pos, buf), K(ret));
    }

    if (OB_UNLIKELY(OB_SIZE_OVERFLOW == ret)) {
      int ori_ret = ret;
      ret = OB_ERR_DATE_FORMAT_IS_TOO_LONG_FOR_INTERNAL_BUFFER;
      LOG_WARN("data format is to long for internal buffer", K(ret), K(ori_ret));
    }
  }  // end if
  return ret;
}

X
xy0 已提交
4558
int ObTimeConverter::deduce_max_len_from_oracle_dfm(const ObString &format, int64_t &max_char_len)
O
oceanbase-admin 已提交
4559 4560 4561 4562 4563 4564 4565 4566 4567 4568
{
  int ret = OB_SUCCESS;
  max_char_len = 0;
  static_assert(sizeof(ELEMENTFLAG_MAX_LEN) / sizeof(ELEMENTFLAG_MAX_LEN[0]) == ObDFMFlag::MAX_FLAG_NUMBER,
      "ELEMENTFLAG_MAX_LEN shall contain all flags");
  if (OB_UNLIKELY(format.empty())) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("format is empty", K(ret));
  }
  if (OB_SUCC(ret)) {
X
xy0 已提交
4569
    const char *const format_begin_ptr = format.ptr();
O
oceanbase-admin 已提交
4570 4571 4572 4573 4574 4575 4576 4577
    int64_t last_elem_end_pos = 0;
    ObSEArray<ObDFMElem, ObDFMUtil::COMMON_ELEMENT_NUMBER> dfm_elems;
    // Parse elements from format string
    if (OB_FAIL(ObDFMUtil::parse_datetime_format_string(format, dfm_elems))) {
      LOG_WARN("parse_datetime_format_string failed", K(ret), K(format));
    }
    // accumulate max length for each element
    for (int i = 0; OB_SUCC(ret) && i < dfm_elems.count(); ++i) {
X
xy0 已提交
4578
      ObDFMElem &elem = dfm_elems.at(i);
O
oceanbase-admin 已提交
4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615
      if (OB_UNLIKELY(!elem.is_valid())) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("elem is invalid", K(ret), K(elem));
      }
      // Separate chars between elements
      if (OB_SUCC(ret)) {
        int64_t separate_chars_len = elem.offset_ - last_elem_end_pos;
        if (separate_chars_len > 0) {
          max_char_len += separate_chars_len;
        }
        last_elem_end_pos = elem.offset_ + ObDFMFlag::PATTERN[elem.elem_flag_].len_;
      }
      // Check every elem
      if (OB_UNLIKELY(elem.elem_flag_ <= ObDFMFlag::INVALID_FLAG || elem.elem_flag_ >= ObDFMFlag::MAX_FLAG_NUMBER)) {
        ret = OB_ERR_UNEXPECTED;
        LOG_WARN("elem_flag_ is invalid", K(ret), K(elem.elem_flag_));
      } else {
        max_char_len += ELEMENTFLAG_MAX_LEN[elem.elem_flag_];
      }
    }
    // Rest separate chars
    if (OB_SUCC(ret)) {
      int64_t separate_chars_len = format.length() - last_elem_end_pos;
      if (separate_chars_len > 0) {
        max_char_len += separate_chars_len;
      }
    }
    if (OB_UNLIKELY(OB_SIZE_OVERFLOW == ret)) {
      int ori_ret = ret;
      ret = OB_ERR_DATE_FORMAT_IS_TOO_LONG_FOR_INTERNAL_BUFFER;
      LOG_WARN("data format is to long for internal buffer", K(ret), K(ori_ret));
    }
  }
  return ret;
}

int ObTimeConverter::ob_time_to_str_format(
X
xy0 已提交
4616
    const ObTime &ob_time, const ObString &format, char *buf, int64_t buf_len, int64_t &pos, bool &res_null)
O
oceanbase-admin 已提交
4617 4618 4619 4620 4621 4622
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(format.ptr()) || OB_ISNULL(buf) || OB_UNLIKELY(format.length() <= 0 || buf_len <= 0)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("format or output string is invalid", K(ret), K(format), K(buf), K(buf_len));
  } else {
X
xy0 已提交
4623 4624 4625
    const char *format_ptr = format.ptr();
    const char *end_ptr = format.ptr() + format.length();
    const int32_t *parts = ob_time.parts_;
O
oceanbase-admin 已提交
4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724
    int32_t week_sunday = -1;
    int32_t week_monday = -1;
    int32_t delta_sunday = -2;
    int32_t delta_monday = -2;
    // used for am/pm conversation in order to avoid if-else tests.
    const int hour_converter[24] = {12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    while (format_ptr < end_ptr && OB_SUCCESS == ret) {
      if ('%' == *format_ptr) {
        format_ptr++;
        if (format_ptr >= end_ptr) {
          ret = OB_INVALID_ARGUMENT;
          break;
        }
        switch (*format_ptr) {
            /*The cases are not ordered alphabetically since Y y m d D are used frequently
             *in order to get better performance, we locate them in front of others
             */
          case 'Y': {  // Year, numeric, four digits
            ret = data_fmt_nd(buf, buf_len, pos, 4, ob_time.parts_[DT_YEAR]);
            break;
          }
          case 'y': {  // Year, numeric (two digits)
            int year = (ob_time.parts_[DT_YEAR]) % 100;
            ret = data_fmt_nd(buf, buf_len, pos, 2, year);
            break;
          }
          case 'M': {  // Month name (January..December)
            if (OB_UNLIKELY(0 == parts[DT_MON])) {
              res_null = true;
            }
            ret = data_fmt_s(buf, buf_len, pos, MON_NAMES[parts[DT_MON]].ptr_);
            break;
          }
          case 'm': {  // Month, numeric (00..12)
            ret = data_fmt_nd(buf, buf_len, pos, 2, parts[DT_MON]);
            break;
          }
          case 'D': {  // Day of the month with English suffix (0th, 1st, 2nd, 3rd...)
            ret = data_fmt_s(buf, buf_len, pos, DAY_NAME[parts[DT_MDAY]]);
            break;
          }
          case 'd': {  // Day of the month, numeric (00..31)
            ret = data_fmt_nd(buf, buf_len, pos, 2, parts[DT_MDAY]);
            break;
          }
          case 'a': {  // Abbreviated weekday name (Sun..Sat)
            if (OB_UNLIKELY(0 == parts[DT_WDAY])) {
              res_null = true;
            }
            ret = data_fmt_s(buf, buf_len, pos, WDAY_ABBR_NAMES[parts[DT_WDAY]].ptr_);
            break;
          }
          case 'b': {  // Abbreviated month name (Jan..Dec)
            if (OB_UNLIKELY(0 == parts[DT_MON])) {
              res_null = true;
            }
            ret = data_fmt_s(buf, buf_len, pos, MON_ABBR_NAMES[parts[DT_MON]].ptr_);
            break;
          }
          case 'c': {  // Month, numeric (0..12)
            ret = data_fmt_d(buf, buf_len, pos, parts[DT_MON]);
            break;
          }
          case 'e': {  // Day of the month, numeric (0..31)
            ret = data_fmt_d(buf, buf_len, pos, parts[DT_MDAY]);
            break;
          }
          case 'f': {  // Microseconds (000000..999999)
            ret = data_fmt_nd(buf, buf_len, pos, 6, parts[DT_USEC]);
            break;
          }
          case 'H': {  // Hour (00..23)
            ret = data_fmt_nd(buf, buf_len, pos, 2, parts[DT_HOUR]);
            break;
          }
          case 'h':    // Hour (01..12)
          case 'I': {  // Hour (01..12)
            int hour = hour_converter[parts[DT_HOUR]];
            ret = data_fmt_nd(buf, buf_len, pos, 2, hour);
            break;
          }
          case 'i': {  // Minutes, numeric (00..59)
            ret = data_fmt_nd(buf, buf_len, pos, 2, parts[DT_MIN]);
            break;
          }
          case 'j': {  // Day of year (001..366)
            ret = data_fmt_nd(buf, buf_len, pos, 3, parts[DT_YDAY]);
            break;
          }
          case 'k': {  // Hour (0..23)
            ret = data_fmt_d(buf, buf_len, pos, parts[DT_HOUR]);
            break;
          }
          case 'l': {  // Hour (1..12)
            int hour = hour_converter[parts[DT_HOUR]];
            ret = data_fmt_d(buf, buf_len, pos, hour);
            break;
          }
          case 'p': {  // AM or PM
X
xy0 已提交
4725
            const char *ptr = parts[DT_HOUR] < 12 ? "AM" : "PM";
O
oceanbase-admin 已提交
4726 4727 4728 4729 4730
            ret = data_fmt_s(buf, buf_len, pos, ptr);
            break;
          }
          case 'r': {  // Time, 12-hour (hh:mm:ss followed by AM or PM)
            int hour = hour_converter[parts[DT_HOUR]];
X
xy0 已提交
4731
            const char *ptr = parts[DT_HOUR] < 12 ? "AM" : "PM";
O
oceanbase-admin 已提交
4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822
            ret = databuff_printf(buf, buf_len, pos, "%02d:%02d:%02d %s", hour, parts[DT_MIN], parts[DT_SEC], ptr);
            break;
          }
          case 'S':    // Seconds (00..59)
          case 's': {  // Seconds (00..59)
            ret = data_fmt_nd(buf, buf_len, pos, 2, parts[DT_SEC]);
            break;
          }
          case 'T': {  // Time, 24-hour (hh:mm:ss)
            ret = databuff_printf(buf, buf_len, pos, "%02d:%02d:%02d", parts[DT_HOUR], parts[DT_MIN], parts[DT_SEC]);
            break;
          }
          case 'U': {  // Week (00..53), where Sunday is the first day of the week
            ret = data_fmt_nd(buf, buf_len, pos, 2, ob_time_to_week(ob_time, WEEK_MODE[0]));
            break;
          }
          case 'u': {  // Week (00..53), where Monday is the first day of the week
            ret = data_fmt_nd(buf, buf_len, pos, 2, ob_time_to_week(ob_time, WEEK_MODE[1]));
            break;
          }
          case 'V': {  // Week (01..53), where Sunday is the first day of the week; used with %X
            // due to the face that V is often used with X.
            // In order to optimize the implementation, we set the right delta value which will possibly be used for %X
            // case latter. week_sunday != -1 means that its value has been computed in %X case ever.
            ret = data_fmt_nd(buf,
                buf_len,
                pos,
                2,
                (-1 == week_sunday) ? ob_time_to_week(ob_time, WEEK_MODE[2], delta_sunday) : week_sunday);
            break;
          }
          case 'v': {  // Week (01..53), where Monday is the first day of the week; used with %x
            ret = data_fmt_nd(buf,
                buf_len,
                pos,
                2,
                (-1 == week_monday) ? ob_time_to_week(ob_time, WEEK_MODE[3], delta_monday) : week_monday);
            break;
          }
          case 'W': {  // Weekday name (Sunday..Saturday)
            if (OB_UNLIKELY(0 == parts[DT_WDAY])) {
              res_null = true;
            }
            ret = data_fmt_s(buf, buf_len, pos, WDAY_NAMES[parts[DT_WDAY]].ptr_);
            break;
          }
          case 'w': {  // Day of the week (0=Sunday..6=Saturday)
            if (OB_UNLIKELY(0 == parts[DT_WDAY])) {
              res_null = true;
            }
            ret = data_fmt_d(buf, buf_len, pos, parts[DT_WDAY] % DAYS_PER_WEEK);
            break;
          }
          case 'X': {  // Year for the week where Sunday is the first day of the week, numeric, four digits; used with
                       // %V
            // due to the face that %X is often used with %V.
            // In order to optimize the implementation, we set the right week_sunday value which will possibly be used
            // for %V case latter.
            if (-2 == delta_sunday) {
              week_sunday = ob_time_to_week(ob_time, WEEK_MODE[2], delta_sunday);
            }
            int32_t year = ob_time.parts_[DT_YEAR] + delta_sunday;
            if (OB_UNLIKELY(-1 == year)) {
              ret = data_fmt_nd(buf, buf_len, pos, 4, 0);
            } else {
              ret = data_fmt_nd(buf, buf_len, pos, 4, year);
            }
            break;
          }
          case 'x': {  // Year for the week, where Monday is the first day of the week, numeric, four digits; used with
                       // %v
            if (-2 == delta_monday) {
              week_monday = ob_time_to_week(ob_time, WEEK_MODE[3], delta_monday);
            }
            int32_t year = ob_time.parts_[DT_YEAR] + delta_monday;
            if (OB_UNLIKELY(-1 == year)) {
              ret = data_fmt_nd(buf, buf_len, pos, 4, 0);
            } else {
              ret = data_fmt_nd(buf, buf_len, pos, 4, year);
            }
            break;
          }
          case '%': {  // A literal "%" character
            if (pos >= buf_len) {
              ret = OB_SIZE_OVERFLOW;
              break;
            }
            buf[pos++] = '%';
            break;
          }
          default: {
J
jg0 已提交
4823 4824 4825 4826 4827
            if (pos >= buf_len) {
              ret = OB_SIZE_OVERFLOW;
              break;
            }
            buf[pos++] = *format_ptr;
O
oceanbase-admin 已提交
4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844
            break;
          }
        }
        if (OB_SUCC(ret)) {
          format_ptr++;
        }
      } else if (pos >= buf_len) {
        ret = OB_SIZE_OVERFLOW;
        break;
      } else {
        buf[pos++] = *(format_ptr++);
      }
    }
  }
  return ret;
}

X
xy0 已提交
4845 4846
int check_and_get_tz_info(ObTime &ob_time, const ObTimeConvertCtx &cvrt_ctx, const ObTimeZoneInfo *&tz_info,
    ObTimeZoneInfoPos *&literal_tz_info, ObTZInfoIDPosMap *&tz_id_pos_map)
O
oceanbase-admin 已提交
4847 4848
{
  int ret = OB_SUCCESS;
X
xy0 已提交
4849
  ObTZInfoMap *tz_info_map = NULL;
O
oceanbase-admin 已提交
4850 4851 4852 4853 4854 4855 4856
  if (OB_UNLIKELY(ob_time.is_tz_name_valid_)) {  // use string literal tz_inifo
    // tz_info_ is expected to be not null, but sometimes we don't set it.
    if (NULL == cvrt_ctx.tz_info_) {
      if (HAS_TYPE_ORACLE(ob_time.mode_)) {
        ret = OB_INVALID_ARGUMENT;
        LOG_WARN("tz_info_ is NULL", K(ret));
      }
X
xy0 已提交
4857
    } else if (OB_ISNULL(tz_info_map = const_cast<ObTZInfoMap *>(cvrt_ctx.tz_info_->get_tz_info_map()))) {
O
oceanbase-admin 已提交
4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874
      ret = OB_ERR_UNEXPECTED;
      LOG_WARN("tz_info_map is NULL", K(ret));
    } else if (OB_FAIL(tz_info_map->get_tz_info_by_name(ob_time.get_tz_name_str(), literal_tz_info))) {
      LOG_WARN("fail to get_tz_info_by_name", K(ob_time), K(ret));
      tz_info_map->id_map_.revert(literal_tz_info);
      literal_tz_info = NULL;
    } else {
      literal_tz_info->set_error_on_overlap_time(cvrt_ctx.tz_info_->is_error_on_overlap_time());
      tz_info = literal_tz_info;
      tz_id_pos_map = &(tz_info_map->id_map_);
    }
  } else {  // use session tz_info
    tz_info = cvrt_ctx.tz_info_;
  }
  return ret;
}

X
xy0 已提交
4875
int ObTimeConverter::ob_time_to_datetime(ObTime &ob_time, const ObTimeConvertCtx &cvrt_ctx, int64_t &value)
O
oceanbase-admin 已提交
4876 4877 4878 4879 4880 4881 4882 4883
{
  int ret = OB_SUCCESS;
  if (ZERO_DATE == ob_time.parts_[DT_DATE]) {
    value = ZERO_DATETIME;
  } else {
    // there is no leap second, and the valid range of datetime and timestamp is not same as mysql.
    // so we don't handle leap second and shift things, delete all related codes.
    int64_t usec = ob_time.parts_[DT_DATE] * USECS_PER_DAY + ob_time_to_time(ob_time);
X
xy0 已提交
4884 4885 4886
    const ObTimeZoneInfo *tz_info = NULL;
    ObTimeZoneInfoPos *literal_tz_info = NULL;
    ObTZInfoIDPosMap *tz_id_pos_map = NULL;
O
oceanbase-admin 已提交
4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907
    if (usec > DATETIME_MAX_VAL || usec < DATETIME_MIN_VAL) {
      ret = OB_DATETIME_FUNCTION_OVERFLOW;
      LOG_WARN("datetime filed overflow", K(ret), K(usec));
    } else {
      value = usec;
      if (OB_FAIL(check_and_get_tz_info(ob_time, cvrt_ctx, tz_info, literal_tz_info, tz_id_pos_map))) {
        LOG_WARN("fail to check_and_get_tz_info", K(ob_time), K(ret));
      } else if (OB_FAIL(sub_timezone_offset(tz_info, cvrt_ctx.is_timestamp_, ob_time.get_tzd_abbr_str(), value))) {
        LOG_WARN("failed to adjust value with time zone offset", K(ret));
      }
    }

    if (NULL != literal_tz_info && NULL != tz_id_pos_map) {
      tz_id_pos_map->revert(literal_tz_info);
      tz_id_pos_map = NULL;
      literal_tz_info = NULL;
    }
  }
  return ret;
}

X
xy0 已提交
4908
int ObTimeConverter::ob_time_to_otimestamp(ObTime &ob_time, ObOTimestampData &value)
O
oceanbase-admin 已提交
4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943
{
  int ret = OB_SUCCESS;
  if (!HAS_TYPE_ORACLE(ob_time.mode_)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("it is not oracle type", K(ob_time), K(ret));
  } else if (OB_FAIL(validate_oracle_timestamp(ob_time))) {
    LOG_WARN("fail to validate_oracle_timestamp", K(ob_time), K(ret));
  } else {
    int64_t usec = ob_time.parts_[DT_DATE] * USECS_PER_DAY + ob_time_to_time(ob_time);
    value.time_us_ = usec;
    value.time_ctx_.set_tail_nsec(ob_time.parts_[DT_USEC] % NSECS_PER_USEC);
    if (!HAS_TYPE_TIMEZONE(ob_time.mode_)) {
      // do nothing
    } else if (OB_INVALID_INDEX == ob_time.time_zone_id_) {
      value.time_ctx_.store_tz_id_ = 0;
      value.time_ctx_.set_offset_min(ob_time.parts_[DT_OFFSET_MIN]);
    } else {
      value.time_ctx_.store_tz_id_ = 1;
      value.time_ctx_.set_tz_id(ob_time.time_zone_id_);
      value.time_ctx_.set_tran_type_id(ob_time.transition_type_id_);
    }
  }
  return ret;
}

/*
 * +--------+--------+--------+--------+--------+
 * +  1968  |  1969  |  1970  |  1971  |  1972  +
 * +--------+--------+--------+--------+--------+
 * |<-D->|  |        |<-------A------->|<-B->|
 * |<-------C------->|
 *  A and C is the whole year, with A >= 0 and C < 0.
 *  B and D is the rest part (month and day), both >= 0.
 */

X
xy0 已提交
4944
int32_t ObTimeConverter::ob_time_to_date(ObTime &ob_time)
O
oceanbase-admin 已提交
4945 4946 4947 4948 4949
{
  int32_t value = 0;
  if (ZERO_DATE == ob_time.parts_[DT_DATE] && !HAS_TYPE_ORACLE(ob_time.mode_)) {
    value = ZERO_DATE;
  } else {
X
xy0 已提交
4950
    int32_t *parts = ob_time.parts_;
O
oceanbase-admin 已提交
4951 4952 4953 4954 4955 4956 4957 4958 4959
    parts[DT_YDAY] = DAYS_UNTIL_MON[IS_LEAP_YEAR(parts[DT_YEAR])][parts[DT_MON] - 1] + parts[DT_MDAY];
    int32_t days_of_years = (parts[DT_YEAR] - EPOCH_YEAR4) * DAYS_PER_NYEAR;
    int32_t leap_year_count = LEAP_YEAR_COUNT(parts[DT_YEAR] - 1) - LEAP_YEAR_COUNT(EPOCH_YEAR4 - 1);
    value = static_cast<int32_t>(days_of_years + leap_year_count + parts[DT_YDAY] - 1);
    parts[DT_WDAY] = WDAY_OFFSET[value % DAYS_PER_WEEK][EPOCH_WDAY];
  }
  return value;
}

X
xy0 已提交
4960
int64_t ObTimeConverter::ob_time_to_time(const ObTime &ob_time)
O
oceanbase-admin 已提交
4961 4962 4963 4964 4965 4966
{
  return ((ob_time.parts_[DT_HOUR] * MINS_PER_HOUR + ob_time.parts_[DT_MIN]) * SECS_PER_MIN + ob_time.parts_[DT_SEC]) *
             USECS_PER_SEC +
         (HAS_TYPE_ORACLE(ob_time.mode_) ? ob_time.parts_[DT_USEC] / NSECS_PER_USEC : ob_time.parts_[DT_USEC]);
}

X
xy0 已提交
4967
int ObTimeConverter::ob_interval_to_interval(const ObInterval &ob_interval, int64_t &value)
O
oceanbase-admin 已提交
4968 4969 4970 4971 4972 4973
{
  int ret = OB_SUCCESS;
  if (ob_interval.parts_[DT_YEAR] > 0 || ob_interval.parts_[DT_MON] > 0) {
    ret = OB_INTERVAL_WITH_MONTH;
    LOG_WARN("Interval with year or month can't be converted to useconds", K(ret));
  } else {
X
xy0 已提交
4974
    const int32_t *parts = ob_interval.parts_;
O
oceanbase-admin 已提交
4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986
    value = 0;
    for (int32_t i = DT_MDAY; i <= DT_USEC; ++i) {
      value *= DT_PART_BASE[i];
      value += parts[i];
    }
    if (DT_MODE_NEG & ob_interval.mode_) {
      value = -value;
    }
  }
  return ret;
}

X
xy0 已提交
4987
int32_t ObTimeConverter::ob_time_to_week(const ObTime &ob_time, ObDTMode mode)
O
oceanbase-admin 已提交
4988 4989 4990 4991 4992
{
  int32_t temp = 0;  // trivial parameter.adapted for ob_time_to_week(const ObTime &, ObDTMode , int32_t)
  return ob_time_to_week(ob_time, mode, temp);
}

X
xy0 已提交
4993
int32_t ObTimeConverter::ob_time_to_week(const ObTime &ob_time, ObDTMode mode, int32_t &delta)
O
oceanbase-admin 已提交
4994
{
X
xy0 已提交
4995
  const int32_t *parts = ob_time.parts_;
O
oceanbase-admin 已提交
4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029
  int32_t is_sun_begin = IS_SUN_BEGIN(mode);
  int32_t is_zero_begin = IS_ZERO_BEGIN(mode);
  int32_t is_ge_4_begin = IS_GE_4_BEGIN(mode);
  int32_t week = 0;
  if (parts[DT_YDAY] > DAYS_PER_NYEAR - 3 && is_ge_4_begin && !is_zero_begin) {
    // maybe the day is in week 1 of next year.
    int32_t days_cur_year = DAYS_PER_YEAR[IS_LEAP_YEAR(parts[DT_YEAR])];
    int32_t wday_next_yday1 = WDAY_OFFSET[days_cur_year - parts[DT_YDAY] + 1][parts[DT_WDAY]];
    int32_t yday_next_week1 = YDAY_WEEK1[wday_next_yday1][is_sun_begin][is_ge_4_begin];
    if (parts[DT_YDAY] >= days_cur_year + yday_next_week1) {
      week = 1;
      delta = 1;
    }
  }
  if (0 == week) {
    int32_t wday_cur_yday1 = WDAY_OFFSET[(1 - parts[DT_YDAY]) % DAYS_PER_WEEK][parts[DT_WDAY]];
    int32_t yday_cur_week1 = YDAY_WEEK1[wday_cur_yday1][is_sun_begin][is_ge_4_begin];
    if (parts[DT_YDAY] < yday_cur_week1 && !is_zero_begin) {
      // the day is in last week of prev year.
      int32_t days_prev_year = DAYS_PER_YEAR[IS_LEAP_YEAR(parts[DT_YEAR] - 1)];
      int32_t wday_prev_yday1 = WDAY_OFFSET[(1 - days_prev_year - parts[DT_YDAY]) % DAYS_PER_WEEK][parts[DT_WDAY]];
      int32_t yday_prev_week1 = YDAY_WEEK1[wday_prev_yday1][is_sun_begin][is_ge_4_begin];
      week = (days_prev_year + parts[DT_YDAY] - yday_prev_week1 + DAYS_PER_WEEK) / DAYS_PER_WEEK;
      delta = -1;
    } else {
      week = (parts[DT_YDAY] - yday_cur_week1 + DAYS_PER_WEEK) / DAYS_PER_WEEK;
      delta = 0;
    }
  }
  return week;
}
////////////////////////////////
// below are other utility functions:

X
xy0 已提交
5030
int ObTimeConverter::validate_datetime(ObTime &ob_time, const bool is_dayofmonth)
O
oceanbase-admin 已提交
5031
{
X
xy0 已提交
5032
  const int32_t *parts = ob_time.parts_;
O
oceanbase-admin 已提交
5033 5034 5035 5036 5037 5038 5039 5040 5041
  int ret = OB_SUCCESS;
  if (!HAS_TYPE_ORACLE(ob_time.mode_) && !is_dayofmonth && OB_UNLIKELY(0 == parts[DT_MON] && 0 == parts[DT_MDAY])) {
    if (!(0 == parts[DT_YEAR] && 0 == parts[DT_HOUR] && 0 == parts[DT_MIN] && 0 == parts[DT_SEC] &&
            0 == parts[DT_USEC])) {
      ret = OB_INVALID_DATE_VALUE;
    } else {
      ob_time.parts_[DT_DATE] = ZERO_DATE;
    }
  } else {
X
xy0 已提交
5042 5043
    const int64_t *part_min = (HAS_TYPE_ORACLE(ob_time.mode_) ? TZ_PART_MIN : DT_PART_MIN);
    const int64_t *part_max = (HAS_TYPE_ORACLE(ob_time.mode_) ? TZ_PART_MAX : DT_PART_MAX);
O
oceanbase-admin 已提交
5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062
    for (int i = 0; OB_SUCC(ret) && i < DATETIME_PART_CNT; ++i) {
      if (is_dayofmonth && (DT_MON == i || DT_MDAY == i) && 0 == parts[i]) {
        /* do nothing */
      } else if (!(part_min[i] <= parts[i] && parts[i] <= part_max[i])) {
        ret = OB_INVALID_DATE_VALUE;
      }
    }
    if (OB_SUCC(ret)) {
      int is_leap = IS_LEAP_YEAR(parts[DT_YEAR]);
      if (is_dayofmonth && (0 == parts[DT_MDAY] || (0 == parts[DT_MON] && parts[DT_MDAY] <= 31))) {
        /* do nothing */
      } else if (parts[DT_MDAY] > DAYS_PER_MON[is_leap][parts[DT_MON]]) {
        ret = OB_INVALID_DATE_VALUE;
      }
    }
  }
  return ret;
}

X
xy0 已提交
5063
int ObTimeConverter::validate_oracle_timestamp(const ObTime &ob_time)
O
oceanbase-admin 已提交
5064
{
X
xy0 已提交
5065
  const int32_t *parts = ob_time.parts_;
O
oceanbase-admin 已提交
5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091
  int ret = OB_SUCCESS;
  for (int i = 0; OB_SUCC(ret) && i < DATETIME_PART_CNT; ++i) {
    if (parts[i] < TZ_PART_MIN[i] || parts[i] > TZ_PART_MAX[i]) {
      ret = OB_INVALID_DATE_VALUE;
    }
  }
  if (OB_SUCC(ret)) {
    int is_leap = IS_LEAP_YEAR(parts[DT_YEAR]);
    if (parts[DT_MDAY] > DAYS_PER_MON[is_leap][parts[DT_MON]]) {
      ret = OB_ERR_DATE_NOT_VALID_FOR_MONTH_SPECIFIED;
    }
  }

  if (OB_SUCC(ret)) {
    if (OB_INVALID_INDEX != ob_time.time_zone_id_) {
      if (OB_UNLIKELY(!ObOTimestampData::is_valid_tz_id(ob_time.time_zone_id_)) ||
          OB_UNLIKELY(!ObOTimestampData::is_valid_tran_type_id(ob_time.transition_type_id_))) {
        ret = OB_INVALID_DATE_VALUE;
      }
    } else if (OB_UNLIKELY(!ObOTimestampData::is_valid_offset_min(parts[DT_OFFSET_MIN]))) {
      ret = OB_INVALID_DATE_VALUE;
    }
  }
  return ret;
}

X
xy0 已提交
5092
int ObTimeConverter::validate_oracle_date(const ObTime &ob_time)
O
oceanbase-admin 已提交
5093
{
X
xy0 已提交
5094
  const int32_t *parts = ob_time.parts_;
O
oceanbase-admin 已提交
5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109
  int ret = OB_SUCCESS;
  for (int i = 0; OB_SUCC(ret) && i < ORACLE_DATE_PART_CNT; ++i) {
    if (parts[i] < TZ_PART_MIN[i] || parts[i] > TZ_PART_MAX[i]) {
      ret = OB_INVALID_DATE_VALUE;
    }
  }
  if (OB_SUCC(ret)) {
    int is_leap = IS_LEAP_YEAR(parts[DT_YEAR]);
    if (parts[DT_MDAY] > DAYS_PER_MON[is_leap][parts[DT_MON]]) {
      ret = OB_INVALID_DATE_VALUE;
    }
  }
  return ret;
}

X
xy0 已提交
5110
int ObTimeConverter::validate_basic_part_of_ob_time_oracle(const ObTime &ob_time)
O
oceanbase-admin 已提交
5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127
{
  int ret = OB_SUCCESS;
  for (int i = 0; OB_SUCC(ret) && i < DATETIME_PART_CNT; ++i) {
    if (ob_time.parts_[i] < TZ_PART_MIN[i] || ob_time.parts_[i] > TZ_PART_MAX[i]) {
      ret = get_oracle_err_when_datetime_out_of_range(i);
    }
  }

  if (OB_SUCC(ret)) {
    int is_leap = IS_LEAP_YEAR(ob_time.parts_[DT_YEAR]);
    if (ob_time.parts_[DT_MDAY] > DAYS_PER_MON[is_leap][ob_time.parts_[DT_MON]]) {
      ret = OB_ERR_DAY_OF_MONTH_RANGE;
    }
  }
  return ret;
}

X
xy0 已提交
5128
int ObTimeConverter::validate_tz_part_of_ob_time_oracle(const ObTime &ob_time)
O
oceanbase-admin 已提交
5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141
{
  int ret = OB_SUCCESS;
  if (OB_INVALID_INDEX != ob_time.time_zone_id_) {
    if (OB_UNLIKELY(!ObOTimestampData::is_valid_tz_id(ob_time.time_zone_id_)) ||
        OB_UNLIKELY(!ObOTimestampData::is_valid_tran_type_id(ob_time.transition_type_id_))) {
      ret = OB_INVALID_DATE_VALUE;
    }
  } else if (OB_UNLIKELY(!ObOTimestampData::is_valid_offset_min(ob_time.parts_[DT_OFFSET_MIN]))) {
    ret = OB_INVALID_DATE_VALUE;
  }
  return ret;
}

X
xy0 已提交
5142
int ObTimeConverter::validate_time(ObTime &ob_time)
O
oceanbase-admin 已提交
5143 5144
{
  int ret = OB_SUCCESS;
X
xy0 已提交
5145
  int32_t *parts = ob_time.parts_;
O
oceanbase-admin 已提交
5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178
  if (parts[DT_MDAY] > 0) {
    if (parts[DT_MDAY] * HOURS_PER_DAY + parts[DT_HOUR] > INT32_MAX) {
      parts[DT_HOUR] = TIME_MAX_HOUR + 1;
    } else {
      parts[DT_HOUR] += static_cast<const int32_t>(parts[DT_MDAY] * HOURS_PER_DAY);
    }
    parts[DT_MDAY] = 0;
  }
  if (OB_SUCC(ret)) {
    if (parts[DT_MIN] > DT_PART_MAX[DT_MIN] || parts[DT_SEC] > DT_PART_MAX[DT_SEC]) {
      ret = OB_INVALID_DATE_VALUE;
    } else if (parts[DT_HOUR] > TIME_MAX_HOUR) {
      parts[DT_HOUR] = TIME_MAX_HOUR + 1;
    }
  }
  int32_t flag = parts[DT_HOUR] | parts[DT_MIN] | parts[DT_SEC];
  if (0 == flag) {
    ret = OB_SUCCESS;  // all are zero...valid !
  }
  return ret;
}

OB_INLINE int ObTimeConverter::validate_year(int64_t year)
{
  int ret = OB_SUCCESS;
  if (0 != year && (year < YEAR_MIN_YEAR || year > YEAR_MAX_YEAR)) {
    ret = OB_DATA_OUT_OF_RANGE;
    LOG_WARN("year is invalid out of range", K(ret));
  }
  return ret;
}

int ObTimeConverter::set_ob_time_part_directly(
X
xy0 已提交
5179
    ObTime &ob_time, int64_t &conflict_bitset, const int64_t part_offset, const int32_t part_value)
O
oceanbase-admin 已提交
5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(part_offset >= TOTAL_PART_CNT)) {
    ret = OB_INVALID_ARGUMENT;
  } else {
    ob_time.parts_[part_offset] = part_value;
    conflict_bitset |= (1 << part_offset);
  }
  return ret;
}

/*
 * element group may cause conflict on parts in ob_time
 * 1. SSSSS vs HH, HH24, HH12, MI, SS
 * 2. DDD vs DD MM/Mon/Month
 *
 * while call this function, the part_value must be the final value
 */
int ObTimeConverter::set_ob_time_part_may_conflict(
X
xy0 已提交
5199
    ObTime &ob_time, int64_t &conflict_bitset, const int64_t part_offset, const int32_t part_value)
O
oceanbase-admin 已提交
5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(part_offset >= TOTAL_PART_CNT)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", K(ret), K(part_offset));
  } else {
    if (0 != (conflict_bitset & (1 << part_offset))) {
      // already has data in ob_time.part_[part_name], validate it
      if (OB_UNLIKELY(part_value != ob_time.parts_[part_offset])) {
        ret = get_oracle_err_when_datetime_parts_conflict(part_offset);
        LOG_WARN("set time conflict", K(ret), K(part_offset), K(part_value), K(ob_time));
      }
    } else {
      conflict_bitset |= (1 << part_offset);
      ob_time.parts_[part_offset] = part_value;
    }
  }

  return ret;
}

// Not set DT_YEAR with set_ob_time_part_may_conflict, reason:
int ObTimeConverter::set_ob_time_year_may_conflict(
X
xy0 已提交
5223
    ObTime &ob_time, int32_t &julian_year_value, int32_t check_year, int32_t set_year, bool overwrite)
O
oceanbase-admin 已提交
5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239
{
  int ret = OB_SUCCESS;
  if (ZERO_DATE != julian_year_value) {
    if (julian_year_value != check_year) {
      ret = OB_ERR_YEAR_CONFLICTS_WITH_JULIAN_DATE;
      LOG_WARN("year conflicts with Julian date", K(ret), K(julian_year_value), K(check_year));
    } else if (overwrite) {
      ob_time.parts_[DT_YEAR] = set_year;
    }
  } else {
    ob_time.parts_[DT_YEAR] = set_year;
    julian_year_value = check_year;
  }
  return ret;
}

X
xy0 已提交
5240
int ObTimeConverter::time_overflow_trunc(int64_t &value)
O
oceanbase-admin 已提交
5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315
{
  int ret = OB_SUCCESS;
  if (value > TIME_MAX_VAL) {
    // we need some ob error codes that map to ER_TRUNCATED_WRONG_VALUE,
    // so we get OB_INVALID_DATE_FORMAT / OB_INVALID_DATE_VALUE / OB_ERR_TRUNCATED_WRONG_VALUE.
    // another requirement comes from cast function in ob_obj_cast.cpp is this time object will be
    // set to TIME_MAX_VAL (838:59:59), rather than ZERO_VAL (00:00:00),
    // so we get the ONLY one: OB_ERR_TRUNCATED_WRONG_VALUE, because the other two will direct to
    // ZERO_VAL of the temporal types, like cast 'abc' or '1998-76-54' to date.
    ret = OB_ERR_TRUNCATED_WRONG_VALUE;
    value = TIME_MAX_VAL;
  }
  return ret;
}

// DO NOT remove these functions!

// int ObTimeConverter::find_time_range(int64_t t, const int64_t *range_boundaries,
//                                     uint64_t higher_bound, uint64_t& result)
//{
//  int ret = OB_SUCCESS;
//  uint64_t i, lower_bound= 0;
//  /*
//    Function will work without this assertion but result would be meaningless.
//  */
//  if(!(higher_bound > 0 && t >= range_boundaries[0]))
//  {
//    _OB_LOG(ERROR, "Invalid higher_bound = %lu or t = %ld", higher_bound, t);
//    ret = OB_INVALID_ARGUMENT_FOR_USEC_TO_TIME;
//    result = -1;
//  } else {
//    /*
//    Do binary search for minimal interval which contain t. We preserve:
//    range_boundaries[lower_bound] <= t < range_boundaries[higher_bound]
//    invariant and decrease this higher_bound - lower_bound gap twice
//    times on each step.
//    */
//    while (higher_bound - lower_bound > 1)
//    {
//      i= (lower_bound + higher_bound) >> 1;
//      if (range_boundaries[i] <= t)
//        lower_bound= i;
//      else
//        higher_bound= i;
//    }
//    result = lower_bound;
//  }
//  return ret;
//}
//
// int ObTimeConverter::find_transition_type(int64_t t, const ObTimeZoneInfo *sp, TRAN_TYPE_INFO *&result)
//{
//  int ret = OB_SUCCESS;
//  if (OB_UNLIKELY(sp->timecnt == 0 || t < sp->ats[0])){
//    /*
//      If we have not any transitions or t is before first transition let
//      us use fallback time type.
//    */
//    result = sp->fallback_tti;
//  } else {
//    /*
//    Do binary search for minimal interval between transitions which
//    contain t. With this localtime_r on real data may takes less
//    time than with linear search (I've seen 30% speed up).
//    */
//    uint64_t index = 0;
//    if(OB_SUCCESS == find_time_range(t, sp->ats, sp->timecnt, index)){
//      result = &(sp->ttis[sp->types[index]]);
//    } else {
//      ret = OB_INVALID_ARGUMENT_FOR_USEC_TO_TIME;
//    }
//  }
//  return ret;
//}

X
xy0 已提交
5316
int ObTimeConverter::get_datetime_digits(const char *&str, const char *end, int32_t max_len, ObTimeDigits &digits)
O
oceanbase-admin 已提交
5317 5318 5319 5320 5321 5322
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(str) || OB_ISNULL(end) || OB_UNLIKELY(str > end || max_len <= 0)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("str or end or max_len is invalid", K(ret), K(str), K(end), K(max_len));
  } else {
X
xy0 已提交
5323 5324
    const char *pos = str;
    const char *digit_end = str + max_len < end ? str + max_len : end;
O
oceanbase-admin 已提交
5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341
    int32_t value = 0;
    for (; OB_SUCC(ret) && pos < digit_end && isdigit(*pos); ++pos) {
      if (value * 10LL > INT32_MAX - (*pos - '0')) {
        ret = OB_OPERATE_OVERFLOW;
        LOG_WARN("datetime part value is out of range", K(ret));
      } else {
        value = value * 10 + *pos - '0';
      }
    }
    digits.ptr_ = str;
    digits.len_ = static_cast<int32_t>(pos - str);
    digits.value_ = value;
    str = pos;
  }
  return ret;
}

X
xy0 已提交
5342
int ObTimeConverter::get_datetime_delims(const char *&str, const char *end, ObTimeDelims &delims)
O
oceanbase-admin 已提交
5343 5344 5345 5346 5347 5348
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(str) || OB_ISNULL(end) || OB_UNLIKELY(str > end)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("str or end or max_len is invalid", K(ret), K(str), K(end));
  } else {
X
xy0 已提交
5349
    const char *pos = str;
O
oceanbase-admin 已提交
5350 5351 5352 5353 5354 5355 5356 5357 5358
    for (; pos < end && !isdigit(*pos); ++pos) {}
    delims.ptr_ = str;
    delims.len_ = static_cast<int32_t>(pos - str);
    str = pos;
  }
  return ret;
}

OB_INLINE int ObTimeConverter::get_datetime_digits_delims(
X
xy0 已提交
5359
    const char *&str, const char *end, int32_t max_len, ObTimeDigits &digits, ObTimeDelims &delims)
O
oceanbase-admin 已提交
5360 5361 5362 5363 5364 5365 5366 5367 5368 5369
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(get_datetime_digits(str, end, max_len, digits))) {
    LOG_WARN("failed to get digits from datetime string", K(ret));
  } else if (OB_FAIL(get_datetime_delims(str, end, delims))) {
    LOG_WARN("failed to get delims from datetime string", K(ret));
  }
  return ret;
}

X
xy0 已提交
5370
OB_INLINE void ObTimeConverter::skip_delims(const char *&str, const char *end)
O
oceanbase-admin 已提交
5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381
{
  // str < end best come before any other condition using *str, because end maybe points to memory
  // that can't be access, of course the chance for this situation is very very small.
  for (; str < end && !isdigit(*str); ++str) {}
}

OB_INLINE bool ObTimeConverter::is_year4(int64_t first_token_len)
{
  return (4 == first_token_len || 8 == first_token_len || first_token_len >= 14);
}

X
xy0 已提交
5382
OB_INLINE bool ObTimeConverter::is_single_colon(const ObTimeDelims &delims)
O
oceanbase-admin 已提交
5383 5384 5385 5386
{
  return (NULL != delims.ptr_ && ':' == *delims.ptr_ && 1 == delims.len_);
}

X
xy0 已提交
5387
OB_INLINE bool ObTimeConverter::is_space_end_with_single_colon(const ObTimeDelims &delims)
O
oceanbase-admin 已提交
5388 5389 5390
{
  bool ret = false;
  if (NULL != delims.ptr_ && delims.len_ > 0) {
X
xy0 已提交
5391 5392
    const char *pos = delims.ptr_;
    const char *end = delims.ptr_ + delims.len_;
O
oceanbase-admin 已提交
5393 5394 5395 5396 5397 5398
    for (; pos < end && isspace(*pos); ++pos) {}
    ret = (pos + 1 == end && ':' == *pos);
  }
  return ret;
}

X
xy0 已提交
5399
OB_INLINE bool ObTimeConverter::is_single_dot(const ObTimeDelims &delims)
O
oceanbase-admin 已提交
5400 5401 5402 5403
{
  return (1 == delims.len_ && NULL != delims.ptr_ && '.' == *delims.ptr_);
}

X
xy0 已提交
5404
OB_INLINE bool ObTimeConverter::is_all_spaces(const ObTimeDelims &delims)
O
oceanbase-admin 已提交
5405 5406 5407
{
  bool ret = false;
  if (NULL != delims.ptr_ && delims.len_ > 0) {
X
xy0 已提交
5408 5409
    const char *pos = delims.ptr_;
    const char *end = delims.ptr_ + delims.len_;
O
oceanbase-admin 已提交
5410 5411 5412 5413 5414 5415
    for (; pos < end && isspace(*pos); ++pos) {}
    ret = (pos == end);
  }
  return ret;
}

X
xy0 已提交
5416
OB_INLINE bool ObTimeConverter::has_any_space(const ObTimeDelims &delims)
O
oceanbase-admin 已提交
5417 5418 5419
{
  bool ret = false;
  if (NULL != delims.ptr_ && delims.len_ > 0) {
X
xy0 已提交
5420 5421
    const char *pos = delims.ptr_;
    const char *end = delims.ptr_ + delims.len_;
O
oceanbase-admin 已提交
5422 5423 5424 5425 5426 5427
    for (; pos < end && !isspace(*pos); ++pos) {}
    ret = (pos < end);
  }
  return ret;
}

X
xy0 已提交
5428
OB_INLINE bool ObTimeConverter::is_negative(const char *&str, const char *end)
O
oceanbase-admin 已提交
5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443
{
  // date_add('2015-12-31', interval ' +-@-=  1 - 2' day_hour) => 2016-01-01 02:00:00 .
  // date_add('2015-12-31', interval '  -@-=  1 - 2' day_hour) => 2015-12-29 22:00:00 .
  // date_add('2015-12-31', interval '  -@-=+ 1 - 2' day_hour) => 2015-12-29 22:00:00 .
  // it is negative only when minus sign appear before plus sign,
  // or there is minus sign but no plus sign.
  if (!OB_ISNULL(str)) {
    for (; str < end && !isdigit(*str) && '+' != *str && '-' != *str; ++str) {}
  }
  bool ret = (str < end) && ('-' == *str);
  skip_delims(str, end);
  return ret;
}

OB_INLINE int ObTimeConverter::normalize_usecond_round(
X
xy0 已提交
5444
    ObTimeDigits &digits, const int64_t max_precision, const bool use_strict_check /*false*/)
O
oceanbase-admin 已提交
5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471
{
  int ret = OB_SUCCESS;
  if (OB_UNLIKELY(digits.value_ < 0) || OB_UNLIKELY(digits.len_ < 0) ||
      OB_UNLIKELY(digits.len_ > INT32_MAX_DIGITS_LEN) || OB_UNLIKELY(max_precision > INT32_MAX_DIGITS_LEN) ||
      OB_UNLIKELY(max_precision < 0)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("digtis is not invalid", K(ret), K(digits.value_), K(digits.len_), K(max_precision));
  } else {
    if (digits.len_ < max_precision) {
      // .123 means 123000.
      digits.value_ *= static_cast<int32_t>(power_of_10[max_precision - digits.len_]);
    } else if (digits.len_ > max_precision) {
      if (use_strict_check) {
        ret = OB_INVALID_DATE_FORMAT;
        LOG_WARN("digtis len is oversize", K(ret), K(digits.len_), K(max_precision));
      } else {
        // .1234567 will round to 123457.
        digits.value_ /= static_cast<int32_t>(power_of_10[digits.len_ - max_precision]);
        if (digits.ptr_[max_precision] >= '5') {
          ++digits.value_;
        }
      }
    }
  }
  return ret;
}

X
xy0 已提交
5472
OB_INLINE int ObTimeConverter::normalize_usecond_trunc(ObTimeDigits &digits, bool need_trunc)
O
oceanbase-admin 已提交
5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489
{
  int ret = OB_SUCCESS;
  if (digits.value_ < 0 || digits.len_ < 0 || digits.len_ > INT32_MAX_DIGITS_LEN) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("digtis is not invalid", K(ret), K(digits.value_), K(digits.len_));
  } else {
    if (digits.len_ < 6) {
      // .123 means 123000.
      digits.value_ *= static_cast<int32_t>(power_of_10[6 - digits.len_]);
    } else if (digits.len_ > 6 && need_trunc) {
      // .1234567 will trunc to 123456.
      digits.value_ /= static_cast<int32_t>(power_of_10[digits.len_ - 6]);
    }
  }
  return ret;
}

X
xy0 已提交
5490
OB_INLINE int ObTimeConverter::apply_date_space_rule(const ObTimeDelims *delims)
O
oceanbase-admin 已提交
5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(delims)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("delims is null", K(ret));
  } else if (has_any_space(delims[DT_YEAR]) || has_any_space(delims[DT_MON]) || has_any_space(delims[DT_HOUR]) ||
             has_any_space(delims[DT_MIN])) {
    ret = OB_INVALID_DATE_FORMAT;
    LOG_WARN("invalid datetime string", K(ret));
  }
  return ret;
}

X
xy0 已提交
5504
OB_INLINE void ObTimeConverter::apply_date_year2_rule(ObTimeDigits &year)
O
oceanbase-admin 已提交
5505 5506 5507 5508 5509 5510
{
  if (year.len_ <= 2) {
    year.value_ += (year.value_ < EPOCH_YEAR2 ? 2000 : 1900);
  }
}

X
xy0 已提交
5511
OB_INLINE void ObTimeConverter::apply_date_year2_rule(int32_t &year)
O
oceanbase-admin 已提交
5512 5513 5514 5515 5516 5517
{
  if (year < 100) {
    year += (year < EPOCH_YEAR2 ? 2000 : 1900);
  }
}

X
xy0 已提交
5518
OB_INLINE void ObTimeConverter::apply_date_year2_rule(int64_t &year)
O
oceanbase-admin 已提交
5519 5520 5521 5522 5523 5524 5525
{
  if (year < 100) {
    year += (year < EPOCH_YEAR2 ? 2000 : 1900);
  }
}

OB_INLINE int ObTimeConverter::apply_usecond_delim_rule(
X
xy0 已提交
5526
    ObTimeDelims &second, ObTimeDigits &usecond, const int64_t max_precision, const bool use_strict_check)
O
oceanbase-admin 已提交
5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537
{
  int ret = OB_SUCCESS;
  if (is_single_dot(second) && usecond.value_ > 0) {
    ret = normalize_usecond_round(usecond, max_precision, use_strict_check);
  } else {
    usecond.value_ = 0;
  }
  return ret;
}

int ObTimeConverter::apply_datetime_for_time_rule(
X
xy0 已提交
5538
    ObTime &ob_time, const ObTimeDigits *digits, const ObTimeDelims *delims)
O
oceanbase-admin 已提交
5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(digits) || OB_ISNULL(delims)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("digits or delims is null", K(ret));
  } else {
    int32_t delim_cnt = 0;
    bool has_space = false;
    int32_t i = 0;
    // '1011121314.15' => NULL.
    // '101112131415.16' => 13:14:15.160000 .
    // '101112131415.16.17' => NULL.
    // '101112131415..16' => 13:14:15 .
    // '101112131415..16.17' => 13:14:15 .
    // '101112131415.1611112.17' => 13:14:15.161111 .
    // delims between second and usecond, delims after usecond should be handled separately, so sub 2.
    for (; i < DATETIME_PART_CNT - 2; ++i) {
      delim_cnt += delims[i].len_;
      if (has_any_space(delims[i])) {
        has_space = true;
      }
    }
    if (is_single_dot(delims[DT_SEC]) && digits[DT_USEC].len_ <= 6) {
      delim_cnt += delims[DT_USEC].len_;
    }
    if (delim_cnt > 0 && !has_space) {
      ob_time.mode_ |= DT_TYPE_NONE;
    }
  }
  return ret;
}

S
st0 已提交
5571
// for convert utc time to local time, use get_timezone_offset and no gap/overlap time exist.
X
xy0 已提交
5572
OB_INLINE int ObTimeConverter::add_timezone_offset(const ObTimeZoneInfo *tz_info, int64_t &value)
O
oceanbase-admin 已提交
5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585
{
  int ret = OB_SUCCESS;
  if (NULL != tz_info && (ZERO_DATETIME != value || lib::is_oracle_mode())) {
    int32_t offset = 0;
    if (OB_FAIL(tz_info->get_timezone_offset(USEC_TO_SEC(value), offset))) {
      LOG_WARN("failed to get offset between utc and local", K(ret));
    } else {
      value += SEC_TO_USEC(offset);
    }
  }
  return ret;
}

S
st0 已提交
5586
// for convert local time to utc time, gap/overlap time may exist.
X
xy0 已提交
5587 5588
OB_INLINE int ObTimeConverter::sub_timezone_offset(const ObTimeZoneInfo *tz_info, bool is_timestamp,
    const ObString &tz_abbr_str, int64_t &value, const bool is_oracle_mode)
O
oceanbase-admin 已提交
5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605
{
  int ret = OB_SUCCESS;
  if (tz_info != NULL && (ZERO_DATETIME != value || is_oracle_mode)) {
    if (is_timestamp) {
      int32_t offset_sec = 0;
      int32_t tz_id = OB_INVALID_INDEX;
      int32_t tran_type_id = OB_INVALID_INDEX;
      if (OB_FAIL(tz_info->get_timezone_sub_offset(USEC_TO_SEC(value), tz_abbr_str, offset_sec, tz_id, tran_type_id))) {
        LOG_WARN("failed to get offset between utc and local", K(ret));
      } else {
        value -= SEC_TO_USEC(offset_sec);
      }
    }
  }
  return ret;
}

X
xy0 已提交
5606 5607
OB_INLINE int ObTimeConverter::sub_timezone_offset(const ObTimeZoneInfo &tz_info, const ObString &tz_abbr_str,
    int64_t &value_us, int32_t &offset_min, int32_t &tz_id, int32_t &tran_type_id)
O
oceanbase-admin 已提交
5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633
{
  int ret = OB_SUCCESS;
  int32_t offset_sec = 0;
  tran_type_id = OB_INVALID_INDEX;
  if (OB_FAIL(tz_info.get_timezone_sub_offset(USEC_TO_SEC(value_us), tz_abbr_str, offset_sec, tz_id, tran_type_id))) {
    LOG_WARN("failed to get offset between utc and local", K(ret));
  } else if (OB_INVALID_INDEX == tz_id) {
    if (OB_UNLIKELY(!ObOTimestampData::is_valid_offset_min(static_cast<int32_t>(SEC_TO_MIN(offset_sec))))) {
      ret = OB_INVALID_DATE_VALUE;
      LOG_WARN("invalid offset_sec", K(offset_sec), K(ret));
    }
  } else {
    if (OB_UNLIKELY(!ObOTimestampData::is_valid_tz_id(tz_id)) ||
        OB_UNLIKELY(!ObOTimestampData::is_valid_tran_type_id(tran_type_id))) {
      ret = OB_INVALID_DATE_VALUE;
      LOG_WARN("invalid tz_id", K(tz_id), K(ret));
    }
  }

  if (OB_SUCC(ret)) {
    value_us -= SEC_TO_USEC(offset_sec);
    offset_min = static_cast<int32_t>(SEC_TO_MIN(offset_sec));
  }
  return ret;
}

X
xy0 已提交
5634
int ObTimeConverter::get_str_array_idx(const ObString &str, const ObTimeConstStr *str_arr, int32_t count, int32_t &idx)
O
oceanbase-admin 已提交
5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(str.ptr())) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("failed to get index from array by string", K(ret));
  } else {
    int32_t i = 1;
    for (; i <= count; i++) {
      if (str.length() >= str_arr[i].len_ && 0 == STRNCASECMP(str.ptr(), str_arr[i].ptr_, str_arr[i].len_)) {
        break;
      }
    }
    if (i > count) {
      ret = OB_INVALID_DATE_FORMAT;
      LOG_WARN("invalid datetime string", K(ret), K(str));
    } else {
      idx = i;
    }
  }
  return ret;
}

X
xy0 已提交
5657
void ObTimeConverter::get_first_day_of_isoyear(ObTime &ob_time)
O
oceanbase-admin 已提交
5658 5659 5660 5661 5662 5663 5664
{
  int32_t wday = ob_time.parts_[DT_WDAY];
  int32_t week = ob_time_to_week(ob_time, WEEK_MODE[3]);
  int32_t offset = ((week - 1) * 7 + (wday - 1));
  ob_time.parts_[DT_DATE] -= offset;
}

X
xy0 已提交
5665
int ObTimeConverter::get_round_day_of_isoyear(ObTime &ob_time)
O
oceanbase-admin 已提交
5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700
{
  int ret = OB_SUCCESS;
  int32_t wday = ob_time.parts_[DT_WDAY];

  int32_t week = ob_time_to_week(ob_time, WEEK_MODE[3]);
  int32_t offset = ((week - 1) * 7 + (wday - 1));
  const int32_t add_day = (ob_time.parts_[DT_MON] > DT_PART_MAX[DT_MON] / 2 ? 1 : 0);
  int32_t days = ob_time.parts_[DT_DATE] - offset + add_day * DAYS_PER_YEAR[IS_LEAP_YEAR(ob_time.parts_[DT_YEAR])];

  if (OB_FAIL(date_to_ob_time(days, ob_time))) {
    LOG_WARN("failed to convert date part to obtime", K(ret), K(days));
  } else {
    get_first_day_of_isoyear(ob_time);
  }
  return ret;
}

bool ObTimeConverter::is_valid_datetime(const int64_t usec)
{
  bool is_valid = true;
  if ((ZERO_DATETIME != usec) &&
      (usec > DATETIME_MAX_VAL || usec < (lib::is_oracle_mode() ? ORACLE_DATETIME_MIN_VAL : DATETIME_MIN_VAL))) {
    is_valid = false;
  }
  return is_valid;
}

bool ObTimeConverter::is_valid_otimestamp(const int64_t time_us, const int32_t tail_nsec)
{
  return ((lib::is_oracle_mode() ? ORACLE_DATETIME_MIN_VAL : DATETIME_MIN_VAL) <= time_us &&
          time_us <= DATETIME_MAX_VAL && ObOTimestampData::MIN_TAIL_NSEC <= tail_nsec &&
          tail_nsec <= ObOTimestampData::MAX_TAIL_NSEC);
}

void ObTimeConverter::calc_oracle_temporal_minus(
X
xy0 已提交
5701
    const ObOTimestampData &v1, const ObOTimestampData &v2, ObIntervalDSValue &result)
O
oceanbase-admin 已提交
5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720
{
  int64_t utc_diff = 0;
  int32_t nsec_diff = 0;

  utc_diff = v1.time_us_ - v2.time_us_;
  nsec_diff = static_cast<int32_t>(v1.time_ctx_.tail_nsec_) - static_cast<int32_t>(v2.time_ctx_.tail_nsec_);

  /*handle carry*/
  if ((utc_diff < 0 && nsec_diff > 0) || (utc_diff > 0 && nsec_diff < 0)) {
    int32_t sign = (nsec_diff < 0 ? -1 : 1);
    utc_diff += sign;
    nsec_diff -= sign * ObOTimestampData::BASE_TAIL_NSEC;
  }

  result.nsecond_ = utc_diff / USECS_PER_SEC;
  result.fractional_second_ = utc_diff % USECS_PER_SEC * NSECS_PER_USEC + nsec_diff;
}

int ObTimeConverter::date_add_nmonth(
X
xy0 已提交
5721
    const int64_t ori_date_value, const int64_t nmonth, int64_t &result_date_value, bool auto_adjust_mday)
O
oceanbase-admin 已提交
5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759
{
  int ret = OB_SUCCESS;
  ObTime ob_time(DT_TYPE_DATETIME);
  ObTimeConvertCtx cvrt_ctx(NULL, false);  // utc time no timezone
  if (OB_FAIL(datetime_to_ob_time(ori_date_value, NULL, ob_time))) {
    LOG_WARN("failed to convert date part to obtime", K(ret), K(ori_date_value));
  } else {
    bool is_last_day = false;
    if (auto_adjust_mday) {
      is_last_day =
          DAYS_PER_MON[IS_LEAP_YEAR(ob_time.parts_[DT_YEAR])][ob_time.parts_[DT_MON]] == ob_time.parts_[DT_MDAY];
    }
    int64_t total_month = ob_time.parts_[DT_YEAR] * MONTHS_PER_YEAR + ob_time.parts_[DT_MON] + nmonth;
    ob_time.parts_[DT_YEAR] = static_cast<int32_t>((total_month - 1) / MONTHS_PER_YEAR);
    ob_time.parts_[DT_MON] = static_cast<int32_t>((total_month - 1) % MONTHS_PER_YEAR + 1);
    if (auto_adjust_mday) {
      int32_t max_mday = DAYS_PER_MON[IS_LEAP_YEAR(ob_time.parts_[DT_YEAR])][ob_time.parts_[DT_MON]];
      if (ob_time.parts_[DT_MDAY] > max_mday || is_last_day) {
        ob_time.parts_[DT_MDAY] = max_mday;
      }
    }
    if (OB_FAIL(validate_basic_part_of_ob_time_oracle(ob_time))) {
      LOG_WARN("failed to validate ob_time", K(ret), K(ob_time));
    } else {
      ob_time.parts_[DT_DATE] = ob_time_to_date(ob_time);
    }
  }
  if (OB_SUCC(ret)) {
    if (OB_FAIL(ob_time_to_datetime(ob_time, cvrt_ctx, result_date_value))) {
      LOG_WARN("failed to calc result", K(ret));
    }
  }

  LOG_DEBUG("debug add nmonth to date", K(ob_time), K(ori_date_value), K(nmonth), K(result_date_value));
  return ret;
}

int ObTimeConverter::date_add_nsecond(
X
xy0 已提交
5760
    const int64_t ori_date_value, const int64_t nsecond, const int32_t fractional_second, int64_t &result_date_value)
O
oceanbase-admin 已提交
5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773
{
  int ret = OB_SUCCESS;
  int64_t seconds = nsecond + (fractional_second < 0 ? -1 : 0);
  result_date_value = ori_date_value + USECS_PER_SEC * seconds;
  if (OB_UNLIKELY(!is_valid_datetime(result_date_value))) {
    ret = OB_ERR_INVALID_YEAR_VALUE;
    LOG_WARN("invalid date value", K(ret), K(ori_date_value), K(seconds), K(result_date_value));
  }
  LOG_DEBUG("debug add nsecond to date", K(ori_date_value), K(seconds), K(result_date_value));
  return ret;
}

int ObTimeConverter::otimestamp_add_nmonth(const ObObjType type, const ObOTimestampData ori_value,
X
xy0 已提交
5774
    const ObTimeZoneInfo *tz_info, const int64_t nmonth, ObOTimestampData &result_value)
O
oceanbase-admin 已提交
5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802
{
  int ret = OB_SUCCESS;
  ObTime ob_time(DT_TYPE_ORACLE_TIMESTAMP);
  if (OB_FAIL(otimestamp_to_ob_time(type, ori_value, tz_info, ob_time, true))) {
    LOG_WARN("failed to convert otimestamp to ob time", K(ret));
  } else {
    int64_t total_month = ob_time.parts_[DT_YEAR] * MONTHS_PER_YEAR + ob_time.parts_[DT_MON] + nmonth;
    ob_time.parts_[DT_YEAR] = static_cast<int32_t>((total_month - 1) / MONTHS_PER_YEAR);
    ob_time.parts_[DT_MON] = static_cast<int32_t>((total_month - 1) % MONTHS_PER_YEAR + 1);
    if (OB_FAIL(validate_oracle_timestamp(ob_time))) {
      LOG_WARN("failed to validate ob_time", K(ret), K(ob_time));
    } else {
      ob_time.parts_[DT_DATE] = ob_time_to_date(ob_time);
    }

    if (OB_SUCC(ret)) {
      ObTimeConvertCtx time_cvrt_ctx(tz_info, false);
      if (OB_FAIL(ObTimeConverter::ob_time_to_utc(type, time_cvrt_ctx, ob_time))) {
        LOG_WARN("failed to convert ob_time to utc", K(ret));
      } else if (OB_FAIL(ob_time_to_otimestamp(ob_time, result_value))) {
        LOG_WARN("failed to calc days", K(ret));
      }
    }
  }
  return ret;
}

int ObTimeConverter::otimestamp_add_nsecond(const ObOTimestampData ori_value, const int64_t nsecond,
X
xy0 已提交
5803
    const int32_t fractional_second, ObOTimestampData &result_value)
O
oceanbase-admin 已提交
5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831
{
  int ret = OB_SUCCESS;
  result_value = ori_value;
  int32_t tail_nsec = result_value.time_ctx_.tail_nsec_ +
                      (fractional_second >= 0 ? 1 : -1) * (abs(fractional_second) % ObOTimestampData::BASE_TAIL_NSEC);
  int32_t tail_carry = 0;
  if (tail_nsec < 0) {
    tail_carry = -1;
    tail_nsec += ObOTimestampData::BASE_TAIL_NSEC;
  } else if (tail_nsec >= ObOTimestampData::BASE_TAIL_NSEC) {
    tail_carry = 1;
    tail_nsec -= ObOTimestampData::BASE_TAIL_NSEC;
  } else {
    // tail_carry = 0;
    // tail_nsec = tail_nsec;
  }
  result_value.time_us_ +=
      (USECS_PER_SEC * nsecond + fractional_second / ObOTimestampData::BASE_TAIL_NSEC + tail_carry);
  result_value.time_ctx_.set_tail_nsec(tail_nsec);
  if (OB_UNLIKELY(
          !is_valid_otimestamp(result_value.time_us_, static_cast<int32_t>(result_value.time_ctx_.tail_nsec_)))) {
    ret = OB_INVALID_DATE_VALUE;
    LOG_WARN("invalid timestamp value", K(ret), K(ori_value), K(nsecond), K(fractional_second), K(result_value));
  }
  LOG_DEBUG("debug add nsecond to timestamp", K(ori_value), K(nsecond), K(fractional_second), K(result_value));
  return ret;
}

S
st0 已提交
5832 5833
int ObTimeConverter::calc_last_date_of_the_month(
    const int64_t ori_datetime_value, int64_t &result_date_value, const ObObjType dest_type, const bool is_dayofmonth)
O
oceanbase-admin 已提交
5834 5835 5836 5837
{
  int ret = OB_SUCCESS;
  ObTime ob_time(DT_TYPE_DATETIME);
  ObTimeConvertCtx cvrt_ctx(NULL, false);  // utc time no timezone
S
st0 已提交
5838 5839 5840 5841 5842 5843
  const bool is_oracle_mode = lib::is_oracle_mode();
  if (!is_oracle_mode && ZERO_DATETIME == ori_datetime_value) {
    ret = OB_INVALID_DATE_VALUE;
    LOG_WARN("invalid datetime", K(ret), K(ori_datetime_value));
  } else if (OB_FAIL(datetime_to_ob_time(ori_datetime_value, NULL, ob_time))) {
    LOG_WARN("failed to convert date to obtime parts", K(ret), K(ori_datetime_value));
O
oceanbase-admin 已提交
5844 5845 5846 5847
  } else {
    int is_leap = IS_LEAP_YEAR(ob_time.parts_[DT_YEAR]);
    ob_time.parts_[DT_MDAY] = DAYS_PER_MON[is_leap][ob_time.parts_[DT_MON]];

S
st0 已提交
5848 5849 5850
    if (is_oracle_mode && OB_FAIL(validate_basic_part_of_ob_time_oracle(ob_time))) {
      LOG_WARN("failed to validate ob_time", K(ret), K(ob_time));
    } else if (!is_oracle_mode && OB_FAIL(validate_datetime(ob_time, is_dayofmonth))) {
O
oceanbase-admin 已提交
5851 5852 5853 5854 5855 5856
      LOG_WARN("failed to validate ob_time", K(ret), K(ob_time));
    } else {
      ob_time.parts_[DT_DATE] = ob_time_to_date(ob_time);
    }

    if (OB_SUCC(ret)) {
J
jg0 已提交
5857 5858 5859 5860 5861 5862 5863 5864 5865
      if (ObDateTimeType == dest_type) {
        if (OB_FAIL(ob_time_to_datetime(ob_time, cvrt_ctx, result_date_value))) {
          LOG_WARN("failed to calc result", K(ret));
        }
      } else if (ObDateType == dest_type) {
        result_date_value = static_cast<int64_t>(ob_time_to_date(ob_time));
      } else {
        ret = OB_INVALID_ARGUMENT;
        LOG_WARN("unexpected dest type", K(ret), K(dest_type));
O
oceanbase-admin 已提交
5866 5867 5868
      }
    }
  }
S
st0 已提交
5869
  LOG_DEBUG("debug calc_last_mday", K(ob_time), K(ori_datetime_value), K(result_date_value));
O
oceanbase-admin 已提交
5870 5871 5872 5873
  return ret;
}

int ObTimeConverter::calc_next_date_of_the_wday(
X
xy0 已提交
5874
    const int64_t ori_date_value, const ObString &wday_name, int64_t &result_date_value)
O
oceanbase-admin 已提交
5875 5876 5877 5878 5879 5880 5881 5882 5883 5884
{
  int ret = OB_SUCCESS;
  ObTime ob_time(DT_TYPE_DATETIME);

  if (OB_FAIL(datetime_to_ob_time(ori_date_value, NULL, ob_time))) {
    LOG_WARN("failed to convert date part to obtime", K(ret), K(ori_date_value));
  } else {
    int32_t wday = 0;
    bool found = false;
    for (int32_t i = 1; i <= DAYS_PER_WEEK && !found; ++i) {
X
xy0 已提交
5885
      const ObTimeConstStr &wday_pattern = WDAY_NAMES[i];
O
oceanbase-admin 已提交
5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908
      if (0 == wday_name.case_compare(wday_pattern.to_obstring())) {
        found = true;
        wday = i;
      }
    }
    if (!found) {
      ret = OB_ERR_INVALID_DAY_OF_THE_WEEK;
      LOG_WARN("invalid week day", K(ret), K(wday_name));
    } else {
      int32_t days_before_the_target_date = wday - ob_time.parts_[DT_WDAY];
      if (days_before_the_target_date <= 0) {
        days_before_the_target_date += DAYS_PER_WEEK;
      }
      if (OB_FAIL(date_add_nsecond(ori_date_value, days_before_the_target_date * SECS_PER_DAY, 0, result_date_value))) {
        LOG_WARN("fail to add nsecond", K(ret), K(days_before_the_target_date));
      }
    }
  }
  LOG_DEBUG("debug calc_next_mday_of_the_wday", K(ob_time), K(ori_date_value), K(result_date_value), K(wday_name));
  return ret;
}

int ObTimeConverter::calc_days_and_months_between_dates(
X
xy0 已提交
5909
    const int64_t date_value1, const int64_t date_value2, int64_t &months_diff, int64_t &rest_utc_diff)
O
oceanbase-admin 已提交
5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945
{
  int ret = OB_SUCCESS;
  ObTime ob_time1(DT_TYPE_DATETIME);
  ObTime ob_time2(DT_TYPE_DATETIME);

  if (OB_FAIL(datetime_to_ob_time(date_value1, NULL, ob_time1))) {
    LOG_WARN("failed to convert date to obtime parts", K(ret), K(date_value1));
  } else if (OB_FAIL(datetime_to_ob_time(date_value2, NULL, ob_time2))) {
    LOG_WARN("failed to convert date to obtime parts", K(ret), K(date_value2));
  } else {
    months_diff = (ob_time1.parts_[DT_YEAR] - ob_time2.parts_[DT_YEAR]) * MONTHS_PER_YEAR +
                  (ob_time1.parts_[DT_MON] - ob_time2.parts_[DT_MON]);
    int32_t last_month_day1 = DAYS_PER_MON[IS_LEAP_YEAR(ob_time1.parts_[DT_YEAR])][ob_time1.parts_[DT_MON]];
    int32_t last_month_day2 = DAYS_PER_MON[IS_LEAP_YEAR(ob_time2.parts_[DT_YEAR])][ob_time2.parts_[DT_MON]];
    if (ob_time1.parts_[DT_MDAY] == ob_time2.parts_[DT_MDAY] ||
        (ob_time1.parts_[DT_MDAY] == last_month_day1 && ob_time2.parts_[DT_MDAY] == last_month_day2)) {
      rest_utc_diff = 0;
    } else {
      rest_utc_diff = (ob_time1.parts_[DT_MDAY] - ob_time2.parts_[DT_MDAY]) * USECS_PER_DAY +
                      ((ob_time1.parts_[DT_HOUR] - ob_time2.parts_[DT_HOUR]) * MINS_PER_HOUR +
                          (ob_time1.parts_[DT_MIN] - ob_time2.parts_[DT_MIN])) *
                          USECS_PER_MIN +
                      (ob_time1.parts_[DT_SEC] - ob_time2.parts_[DT_SEC]) * USECS_PER_SEC;
      if (rest_utc_diff < 0 && months_diff > 0) {
        months_diff--;
        rest_utc_diff += 31 * USECS_PER_DAY;
      } else if (rest_utc_diff > 0 && months_diff < 0) {
        months_diff++;
        rest_utc_diff -= 31 * USECS_PER_DAY;
      }
    }
  }
  LOG_DEBUG("calc_days_and_months_between_dates", K(ret), K(ob_time1), K(ob_time2), K(months_diff), K(rest_utc_diff));
  return ret;
}

X
xy0 已提交
5946 5947
int ObTimeConverter::decode_otimestamp(const ObObjType obj_type, const char *data, const int64_t total_len,
    const ObTimeConvertCtx &cvrt_ctx, ObOTimestampData &otimestamp_val, int8_t &scale)
O
oceanbase-admin 已提交
5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967
{
  int ret = OB_SUCCESS;
  int8_t year_year = 0;
  int8_t year_month = 0;
  int8_t month = 0;
  int8_t day = 0;
  int8_t hour = 0;
  int8_t min = 0;
  int8_t second = 0;
  int32_t nanosecond = 0;
  int8_t offset_hour = 0;
  int8_t offset_min = 0;
  int8_t tz_name_length = 0;
  int8_t tz_abbr_length = 0;
  scale = -1;
  ObTime ob_time(DT_TYPE_ORACLE_TIMESTAMP);
  ObString tz_name;
  ObString tz_abbr;
  const bool is_timestamp_tz = (ObTimestampTZType == obj_type);

X
xy0 已提交
5968
  const char *old_data_ptr = data;
O
oceanbase-admin 已提交
5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042
  // read local time
  ObMySQLUtil::get_int1(data, year_year);
  ObMySQLUtil::get_int1(data, year_month);
  ObMySQLUtil::get_int1(data, month);
  ObMySQLUtil::get_int1(data, day);
  ObMySQLUtil::get_int1(data, hour);
  ObMySQLUtil::get_int1(data, min);
  ObMySQLUtil::get_int1(data, second);
  ObMySQLUtil::get_int4(data, nanosecond);
  ObMySQLUtil::get_int1(data, scale);
  if (is_timestamp_tz) {
    ObMySQLUtil::get_int1(data, offset_hour);
    ObMySQLUtil::get_int1(data, offset_min);

    // read tz_name
    ObMySQLUtil::get_int1(data, tz_name_length);
    tz_name.assign_ptr(data, static_cast<ObString::obstr_size_t>(tz_name_length));
    data += tz_name_length;

    // read tz_abbr
    ObMySQLUtil::get_int1(data, tz_abbr_length);
    tz_abbr.assign_ptr(data, static_cast<ObString::obstr_size_t>(tz_abbr_length));
    data += tz_abbr_length;
  }

  if (total_len > (data - old_data_ptr)) {
    // unknown token, do not use it
    data = old_data_ptr + total_len;
  }

  ob_time.parts_[DT_YEAR] =
      static_cast<int32_t>(year_year >= 0 ? (year_year * YEARS_PER_CENTURY + year_month)
                                          : (0 - (0 - year_year * YEARS_PER_CENTURY + year_month)));
  ob_time.parts_[DT_MON] = month;
  ob_time.parts_[DT_MDAY] = day;
  ob_time.parts_[DT_HOUR] = hour;
  ob_time.parts_[DT_MIN] = min;
  ob_time.parts_[DT_SEC] = second;
  ob_time.parts_[DT_USEC] = nanosecond;
  ob_time.parts_[DT_DATE] = ObTimeConverter::ob_time_to_date(ob_time);

  if (is_timestamp_tz) {
    ob_time.mode_ |= DT_TYPE_TIMEZONE;
    if (!tz_name.empty()) {
      if (OB_FAIL(ob_time.set_tz_name(tz_name))) {
        LOG_WARN("fail to set_tz_name", K(tz_name), K(ret));
      } else if (!tz_abbr.empty() && OB_FAIL(ob_time.set_tzd_abbr(tz_abbr))) {
        LOG_WARN("fail to set_tz_abbr", K(tz_abbr), K(ret));
      } else if (OB_FAIL(str_to_tz_offset(cvrt_ctx, ob_time))) {
        LOG_WARN("failed to convert string to tz_offset", K(ret));
      } else {
        // do not use offset time
      }
    } else {
      ob_time.parts_[DT_OFFSET_MIN] =
          static_cast<int32_t>(offset_hour >= 0 ? (offset_hour * MINS_PER_HOUR + offset_min)
                                                : (0 - (0 - offset_hour * MINS_PER_HOUR + offset_min)));
      ob_time.is_tz_name_valid_ = true;
    }
  }

  LOG_DEBUG("decode otimestamp", K(total_len), K(ob_time), K(obj_type), K(scale), K(ret));

  if (OB_SUCC(ret)) {
    if (OB_FAIL(ob_time_to_utc(obj_type, cvrt_ctx, ob_time))) {
      LOG_WARN("failed to convert ob_time to utc", K(ret));
    } else if (OB_FAIL(ob_time_to_otimestamp(ob_time, otimestamp_val))) {
      LOG_WARN("fail to ob_time_to_otimestamp", K(ob_time), K(otimestamp_val), K(ret));
    }
  }
  LOG_DEBUG("succ to decode otimestamp", K(total_len), K(ob_time), K(obj_type), K(otimestamp_val), K(scale), K(ret));
  return ret;
}

X
xy0 已提交
6043 6044
int ObTimeConverter::encode_otimestamp(const ObObjType obj_type, char *buf, const int64_t len, int64_t &pos,
    const ObTimeZoneInfo *tz_info, const ObOTimestampData &ot_data, const int8_t &scale)
O
oceanbase-admin 已提交
6045 6046 6047 6048 6049 6050
{
  int ret = OB_SUCCESS;
  const bool is_timestamp_tz = (ObTimestampTZType == obj_type);
  const bool store_utc_time = false;
  ObTime ob_time(DT_TYPE_ORACLE_TIMESTAMP);
  int64_t orig_pos = pos;
X
xy0 已提交
6051
  const ObOTimestampData &tmp_ot_data = round_otimestamp(scale, ot_data);
O
oceanbase-admin 已提交
6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123
  const int8_t tmp_scale = static_cast<int8_t>(scale < 0 ? DEFAULT_SCALE_FOR_ORACLE_FRACTIONAL_SECONDS : scale);
  if (OB_FAIL(otimestamp_to_ob_time(obj_type, tmp_ot_data, tz_info, ob_time, store_utc_time))) {
    LOG_WARN("failed to convert timestamp_tz to ob time", K(ret));
  } else if (!valid_oracle_year(ob_time)) {
    ret = OB_ERR_DATETIME_INTERVAL_INTERNAL_ERROR;
    LOG_WARN("invalid oracle timestamp", K(ret), K(ob_time));
  } else {
    const int32_t unsigned_year =
        ob_time.parts_[DT_YEAR] >= 0 ? ob_time.parts_[DT_YEAR] : (0 - ob_time.parts_[DT_YEAR]);
    int32_t century = static_cast<int32_t>(unsigned_year / YEARS_PER_CENTURY * (ob_time.parts_[DT_YEAR] >= 0 ? 1 : -1));
    int32_t decade = static_cast<int32_t>(unsigned_year % YEARS_PER_CENTURY);
    if (0 == century) {
      decade *= static_cast<int32_t>(ob_time.parts_[DT_YEAR] >= 0 ? 1 : -1);
    }
    if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, static_cast<int8_t>(century), pos))) {
      LOG_WARN("failed to store int", K(len), K(pos), K(ret));
    } else if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, static_cast<int8_t>(decade), pos))) {
      LOG_WARN("failed to store int", K(len), K(pos), K(ret));
    } else if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, static_cast<int8_t>(ob_time.parts_[DT_MON]), pos))) {
      LOG_WARN("failed to store int", K(len), K(pos), K(ret));
    } else if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, static_cast<int8_t>(ob_time.parts_[DT_MDAY]), pos))) {
      LOG_WARN("failed to store int", K(len), K(pos), K(ret));
    } else if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, static_cast<int8_t>(ob_time.parts_[DT_HOUR]), pos))) {
      LOG_WARN("failed to store int", K(len), K(pos), K(ret));
    } else if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, static_cast<int8_t>(ob_time.parts_[DT_MIN]), pos))) {
      LOG_WARN("failed to store int", K(len), K(pos), K(ret));
    } else if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, static_cast<int8_t>(ob_time.parts_[DT_SEC]), pos))) {
      LOG_WARN("failed to store int", K(len), K(pos), K(ret));
    } else if (OB_FAIL(ObMySQLUtil::store_int4(buf, len, static_cast<int32_t>(ob_time.parts_[DT_USEC]), pos))) {
      LOG_WARN("failed to store int", K(len), K(pos), K(ret));
    } else if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, tmp_scale, pos))) {
      LOG_WARN("failed to store int", K(len), K(pos), K(ret));
    }
  }

  if (OB_SUCC(ret) && is_timestamp_tz) {
    const int32_t unsigned_offset =
        (ob_time.parts_[DT_OFFSET_MIN] >= 0 ? ob_time.parts_[DT_OFFSET_MIN] : (0 - ob_time.parts_[DT_OFFSET_MIN]));
    int32_t offset_hour =
        static_cast<int32_t>(unsigned_offset / MINS_PER_HOUR * (ob_time.parts_[DT_OFFSET_MIN] >= 0 ? 1 : -1));
    int32_t offset_minute = static_cast<int32_t>(unsigned_offset % MINS_PER_HOUR);
    if (0 == offset_hour) {
      offset_minute *= static_cast<int32_t>(ob_time.parts_[DT_OFFSET_MIN] >= 0 ? 1 : -1);
    }
    ObString tz_name_str;
    ObString tz_abbr_str;
    if (ob_time.is_tz_name_valid_) {
      tz_name_str.assign_ptr(ob_time.tz_name_, static_cast<int32_t>(strlen(ob_time.tz_name_)));
      tz_abbr_str.assign_ptr(ob_time.tzd_abbr_, static_cast<int32_t>(strlen(ob_time.tzd_abbr_)));
    }
    if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, static_cast<int8_t>(offset_hour), pos))) {
      LOG_WARN("failed to store int", K(len), K(pos), K(ret));
    } else if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, static_cast<int8_t>(offset_minute), pos))) {
      LOG_WARN("failed to store int", K(len), K(pos), K(ret));
    } else if (OB_FAIL(ObMySQLUtil::write_segment_str(buf, len, pos, tz_name_str))) {
      LOG_WARN("failed to write_segment_str", K(len), K(pos), K(tz_name_str), K(ret));
    } else if (OB_FAIL(ObMySQLUtil::write_segment_str(buf, len, pos, tz_abbr_str))) {
      LOG_WARN("failed to write_segment_str", K(len), K(pos), K(tz_abbr_str), K(ret));
    }
  }
  LOG_DEBUG("succ to encode otimestamp",
      K(len),
      "data_len",
      pos - orig_pos,
      K(ot_data),
      K(scale),
      K(tmp_ot_data),
      K(ob_time),
      K(ret));
  return ret;
}

X
xy0 已提交
6124 6125
int ObTimeConverter::interval_ym_to_str(const ObIntervalYMValue &value, const ObScale scale, char *buf, int64_t buf_len,
    int64_t &pos, bool fmt_with_interval)
O
oceanbase-admin 已提交
6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144
{
  int ret = OB_SUCCESS;
  char format_str[] = "%c%02d-%02d";
  const int scale_idx = 4;
  int8_t year_scale = ObIntervalScaleUtil::ob_scale_to_interval_ym_year_scale(static_cast<int8_t>(scale));
  ObIntervalParts display_part;

  interval_ym_to_display_part(value, display_part);
  format_str[scale_idx] = static_cast<char>('0' + year_scale);

  if (OB_UNLIKELY(!ObIntervalScaleUtil::scale_check(year_scale))) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("failed to check_scale", K(ret), K(year_scale));
  } else if (fmt_with_interval && OB_FAIL(databuff_printf(buf, buf_len, pos, "INTERVAL\'"))) {
    LOG_WARN("fail to print INTERVAL", K(ret));
  } else if (OB_FAIL(databuff_printf(buf,
                 buf_len,
                 pos,
                 format_str,
C
chuancy 已提交
6145
                 display_part.is_negative_ ? '-' : '+',
O
oceanbase-admin 已提交
6146 6147 6148 6149 6150 6151 6152 6153 6154
                 display_part.parts_[ObIntervalParts::YEAR_PART],
                 display_part.parts_[ObIntervalParts::MONTH_PART]))) {
    LOG_WARN("fail to print interval values", K(ret));
  } else if (fmt_with_interval && OB_FAIL(databuff_printf(buf, buf_len, pos, "\'YEAR TO MONTH"))) {
    LOG_WARN("fail to print DAY TO SECOND", K(ret));
  }
  return ret;
}

X
xy0 已提交
6155 6156
int ObTimeConverter::interval_ds_to_str(const ObIntervalDSValue &value, const ObScale scale, char *buf, int64_t buf_len,
    int64_t &pos, bool fmt_with_interval)
O
oceanbase-admin 已提交
6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179
{
  int ret = OB_SUCCESS;
  char format_str_part1[] = "%c%02d %02d:%02d:%02d";
  char format_str_part2[] = ".%02d";
  const int day_scale_idx = 4;
  const int fs_scale_idx = 3;
  int8_t day_scale = ObIntervalScaleUtil::ob_scale_to_interval_ds_day_scale(static_cast<int8_t>(scale));
  int8_t fs_scale = ObIntervalScaleUtil::ob_scale_to_interval_ds_second_scale(static_cast<int8_t>(scale));
  ObIntervalParts display_part;

  interval_ds_to_display_part(value, display_part);
  format_str_part1[day_scale_idx] = static_cast<char>('0' + day_scale);
  format_str_part2[fs_scale_idx] = static_cast<char>('0' + fs_scale);

  if (OB_UNLIKELY(!ObIntervalScaleUtil::scale_check(day_scale) || !ObIntervalScaleUtil::scale_check(fs_scale))) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("failed to check_scale", K(ret), K(day_scale), K(fs_scale));
  } else if (fmt_with_interval && OB_FAIL(databuff_printf(buf, buf_len, pos, "INTERVAL\'"))) {
    LOG_WARN("fail to print INTERVAL", K(ret));
  } else if (OB_FAIL(databuff_printf(buf,
                 buf_len,
                 pos,
                 format_str_part1,
C
chuancy 已提交
6180
                 display_part.is_negative_ ? '-' : '+',
O
oceanbase-admin 已提交
6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208
                 display_part.parts_[ObIntervalParts::DAY_PART],
                 display_part.parts_[ObIntervalParts::HOUR_PART],
                 display_part.parts_[ObIntervalParts::MINUTE_PART],
                 display_part.parts_[ObIntervalParts::SECOND_PART]))) {
    LOG_WARN("fail to print interval values part1", K(ret));
  } else if (fs_scale > 0 && OB_FAIL(databuff_printf(buf,
                                 buf_len,
                                 pos,
                                 format_str_part2,
                                 display_part.parts_[ObIntervalParts::FRECTIONAL_SECOND_PART] /
                                     power_of_10[MAX_SCALE_FOR_ORACLE_TEMPORAL - fs_scale]))) {
    LOG_WARN("fail to print interval values part2", K(ret));
  } else if (fmt_with_interval && OB_FAIL(databuff_printf(buf, buf_len, pos, "\'DAY TO SECOND"))) {
    LOG_WARN("fail to print DAY TO SECOND", K(ret));
  }
  return ret;
}

/**
 * @brief convert str to interval year to month
 * @param str         [in]        input string
 * @param value       [out]       result
 * @param scale       [in out]    input expected scale, output real scale
 * @param part_begin  [in]        first part to resolve
 * @param part_end    [in]        last part to resolve
 * @return
 */
int ObTimeConverter::str_to_interval_ym(
X
xy0 已提交
6209
    const ObString &str, ObIntervalYMValue &value, ObScale &scale, ObDateUnitType part_begin, ObDateUnitType part_end)
O
oceanbase-admin 已提交
6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326
{
  int ret = OB_SUCCESS;
  int match_ret = OB_SUCCESS;
  ObDFMParseCtx parse_ctx(str.ptr(), str.length());
  enum { YEAR_VALUE = 0, MONTH_VALUE, VALUE_COUNT };
  const int64_t expected_leading_precision =
      ObIntervalScaleUtil::ob_scale_to_interval_ym_year_scale(static_cast<int8_t>(scale));
  const int64_t expected_len = MAX_SCALE_FOR_ORACLE_TEMPORAL;

  // target
  bool is_negative_value = false;
  int32_t year_value = 0;
  int32_t month_value = 0;
  int64_t real_match_len = 0;
  int8_t real_leading_precision = 0;
  bool is_leading_precision = true;

  if (OB_UNLIKELY(part_begin != part_end && (DATE_UNIT_YEAR != part_begin && DATE_UNIT_MONTH != part_end)) ||
      OB_UNLIKELY(part_begin == part_end && (DATE_UNIT_YEAR != part_begin && DATE_UNIT_MONTH != part_begin))) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid argument", K(ret), K(part_begin), K(part_end));
  }

  if (OB_SUCC(ret)) {
    ObDFMUtil::skip_blank_chars(parse_ctx);
    if (OB_UNLIKELY(parse_ctx.is_parse_finish())) {
      ret = OB_ERR_INTERVAL_INVALID;
      LOG_WARN("input str is empty", K(ret));
    } else if ('-' == parse_ctx.cur_ch_[0] || '+' == parse_ctx.cur_ch_[0]) {
      is_negative_value = ('-' == parse_ctx.cur_ch_[0]);
      parse_ctx.update(1);
    } else {
      is_negative_value = false;
    }
  }

  if (OB_SUCC(ret) && part_begin == DATE_UNIT_YEAR) {
    ObDFMUtil::skip_blank_chars(parse_ctx);
    if (OB_FAIL(ObDFMUtil::match_int_value(parse_ctx, expected_len + 1, real_match_len, year_value))) {
      LOG_WARN("failed to match interval year value", K(ret), K(parse_ctx));
    } else if (OB_UNLIKELY(real_match_len > expected_len)) {
      ret = OB_ERR_THE_LEADING_PRECISION_OF_THE_INTERVAL_IS_TOO_SMALL;
      LOG_WARN("year scale exceed max value", K(ret), K(real_match_len), K(expected_len));
    } else {
      parse_ctx.update(real_match_len);
    }
    is_leading_precision = false;
  }

  if (OB_SUCC(ret) && part_begin != part_end) {
    ObDFMUtil::skip_blank_chars(parse_ctx);
    if (OB_UNLIKELY(parse_ctx.is_parse_finish()) || OB_UNLIKELY('-' != parse_ctx.cur_ch_[0])) {
      ret = OB_ERR_INTERVAL_INVALID;
      LOG_WARN("parse finish unexpected", K(ret), K(parse_ctx));
    } else {
      parse_ctx.update(1);
    }
  }

  if (OB_SUCC(ret) && part_end == DATE_UNIT_MONTH) {
    ObDFMUtil::skip_blank_chars(parse_ctx);
    if (OB_FAIL(ObDFMUtil::match_int_value(parse_ctx, expected_len + 1, real_match_len, month_value))) {
      match_ret = ret;
      ret = OB_ERR_INTERVAL_INVALID;
      LOG_WARN("failed to match interval month value", K(match_ret), K(ret));
    } else if (OB_UNLIKELY(real_match_len > expected_len)) {
      ret = OB_ERR_THE_LEADING_PRECISION_OF_THE_INTERVAL_IS_TOO_SMALL;
      LOG_WARN("year scale exceed max value", K(ret), K(real_match_len), K(expected_len));
    } else if (!is_leading_precision && OB_FAIL(ObIntervalLimit::MONTH.validate(month_value))) {
      LOG_WARN("invalid interval month", K(ret), K(month_value));
    } else {
      parse_ctx.update(real_match_len);
    }
    if (is_leading_precision) {
      is_leading_precision = false;
    }
  }

  if (OB_SUCC(ret)) {
    ObDFMUtil::skip_blank_chars(parse_ctx);
    if (OB_UNLIKELY(!parse_ctx.is_parse_finish())) {
      ret = OB_ERR_INTERVAL_INVALID;
      LOG_WARN("input have extra chars", K(ret), K(parse_ctx));
    } else {
      // the result year scale is equal to the leading precision, though the leading precision may behind the 'day' item
      value.set_value(is_negative_value, year_value, month_value);
      real_leading_precision = value.calc_leading_scale();
      if (OB_UNLIKELY(real_leading_precision > expected_leading_precision)) {
        ret = OB_ERR_THE_LEADING_PRECISION_OF_THE_INTERVAL_IS_TOO_SMALL;
        LOG_WARN("the leading precision of the interval is too small",
            K(ret),
            K(expected_leading_precision),
            K(real_leading_precision));
      } else {
        scale = ObIntervalScaleUtil::interval_ym_scale_to_ob_scale(real_leading_precision);
      }
      LOG_DEBUG("convert string to interval year to month succ",
          K(value),
          K(scale),
          K(str),
          K(real_leading_precision),
          K(expected_leading_precision));
    }
  }
  return ret;
}

/**
 * @brief convert string to interval ds values struct
 * @param str         [in]        input string
 * @param value       [out]       result
 * @param scale       [in out]    input expected scale, output real scale
 * @param part_begin  [in]        first part to resolve
 * @param part_end    [in]        last part to resolve
 * @return
 */
int ObTimeConverter::str_to_interval_ds(
X
xy0 已提交
6327
    const ObString &str, ObIntervalDSValue &value, ObScale &scale, ObDateUnitType part_begin, ObDateUnitType part_end)
O
oceanbase-admin 已提交
6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490
{
  int ret = OB_SUCCESS;
  int match_ret = OB_SUCCESS;
  ObDFMParseCtx parse_ctx(str.ptr(), str.length());
  int64_t real_match_len = 0;
  const int64_t expected_leading_precision =
      ObIntervalScaleUtil::ob_scale_to_interval_ds_day_scale(static_cast<int8_t>(scale));
  const int64_t expected_second_precision =
      ObIntervalScaleUtil::ob_scale_to_interval_ds_second_scale(static_cast<int8_t>(scale));
  const int64_t expected_len = MAX_SCALE_FOR_ORACLE_TEMPORAL;

  enum { DAY_VALUE = 0, HOUR_VALUE, MINUTE_VALUE, SECOND_VALUE, VALUE_COUNT };
  static const ObOracleTimeLimiter limiters[VALUE_COUNT] = {
      ObIntervalLimit::DAY, ObIntervalLimit::HOUR, ObIntervalLimit::MINUTE, ObIntervalLimit::SECOND};
  static const char separators[VALUE_COUNT] = {' ', ' ', ':', ':'};
  bool is_leading_precision = true;

  // target
  bool is_negative_value = false;
  int8_t real_leading_precision = 0;
  int32_t fs_value = 0;
  int64_t real_second_precision = 0;
  int32_t time_part_values[VALUE_COUNT] = {0};

  if (OB_UNLIKELY(part_begin < DATE_UNIT_SECOND || part_begin > DATE_UNIT_DAY) ||
      OB_UNLIKELY(part_end < DATE_UNIT_SECOND || part_end > DATE_UNIT_DAY)) {
    ret = OB_ERR_UNEXPECTED;
    LOG_WARN("invalid argument", K(ret), K(part_begin), K(part_end));
  }

  // resolve sign
  if (OB_SUCC(ret)) {
    ObDFMUtil::skip_blank_chars(parse_ctx);
    if (OB_UNLIKELY(parse_ctx.is_parse_finish())) {
      ret = OB_ERR_INTERVAL_INVALID;
      LOG_WARN("input str is empty", K(ret));
    } else if ('-' == parse_ctx.cur_ch_[0] || '+' == parse_ctx.cur_ch_[0]) {
      is_negative_value = ('-' == parse_ctx.cur_ch_[0]);
      parse_ctx.update(1);
    } else {
      is_negative_value = false;
    }
  }

  // resolve time part : day, hour, minute, second
  for (int64_t iter = part_begin; OB_SUCC(ret) && iter >= part_end; iter--) {
    int64_t i = DATE_UNIT_DAY - iter;

    // skip blanks
    ObDFMUtil::skip_blank_chars(parse_ctx);

    // parse seperator
    if (' ' != separators[i] && iter != part_begin) {
      if (OB_UNLIKELY(parse_ctx.is_parse_finish() || separators[i] != parse_ctx.cur_ch_[0])) {
        ret = OB_ERR_INTERVAL_INVALID;
        LOG_WARN("unexpected separater", K(ret), K(parse_ctx), "separator", separators[i], "idx", i);
      } else {
        parse_ctx.update(1);
        // skip blanks
        ObDFMUtil::skip_blank_chars(parse_ctx);
      }
    }

    if (OB_SUCC(ret)) {
      if (OB_UNLIKELY(parse_ctx.is_parse_finish())) {
        ret = OB_ERR_INTERVAL_INVALID;
        LOG_WARN("missing input string", K(ret), K(iter));
      } else if (OB_FAIL(
                     ObDFMUtil::match_int_value(parse_ctx, expected_len + 1, real_match_len, time_part_values[i]))) {
        match_ret = ret;
        ret = OB_ERR_INTERVAL_INVALID;
        LOG_WARN("failed to match interval value", K(match_ret), K(ret), K(iter));
      } else if (OB_UNLIKELY(real_match_len > expected_len)) {
        ret = OB_ERR_THE_LEADING_PRECISION_OF_THE_INTERVAL_IS_TOO_SMALL;
        LOG_WARN("year scale exceed max value", K(ret), K(real_match_len), K(expected_len));
      } else if (!is_leading_precision && OB_FAIL(limiters[i].validate(time_part_values[i]))) {
        LOG_WARN("invalid interval value", K(ret), K(iter), K(time_part_values[i]));
      } else {
        parse_ctx.update(real_match_len);
      }
    }

    if (OB_SUCC(ret) && is_leading_precision) {
      is_leading_precision = false;
    }
  }

  // resolve optional part : fractional second
  if (OB_SUCC(ret) && part_end == DATE_UNIT_SECOND) {
    ObDFMUtil::skip_blank_chars(parse_ctx);
    if (parse_ctx.is_parse_finish()) {
      /*do nothing, real second precision and fractional second will be 0*/
    } else if (OB_UNLIKELY('.' != parse_ctx.cur_ch_[0])) {
      ret = OB_ERR_INTERVAL_INVALID;
      LOG_WARN("fractional second expected", K(ret), K(parse_ctx));
    } else if (FALSE_IT(parse_ctx.update(1))) {
    } else if (OB_FAIL(ObDFMUtil::match_int_value(parse_ctx, expected_len + 1, real_second_precision, fs_value))) {
      match_ret = ret;
      ret = OB_ERR_INTERVAL_INVALID;
      LOG_WARN("failed to match interval fractional second value", K(match_ret), K(ret), K(parse_ctx));
    } else if (OB_UNLIKELY(real_second_precision > expected_len)) {
      ret = OB_ERR_THE_LEADING_PRECISION_OF_THE_INTERVAL_IS_TOO_SMALL;
      LOG_WARN("second scale exceed max value", K(ret), K(real_second_precision));
    } else {
      fs_value = static_cast<int32_t>(fs_value * power_of_10[MAX_SCALE_FOR_ORACLE_TEMPORAL - real_second_precision]);
      parse_ctx.update(real_second_precision);
    }
  }

  if (OB_SUCC(ret)) {
    ObDFMUtil::skip_blank_chars(parse_ctx);
    if (OB_UNLIKELY(!parse_ctx.is_parse_finish())) {
      ret = OB_ERR_INTERVAL_INVALID;
      LOG_WARN("input have extra chars", K(ret), K(parse_ctx));
    } else {
      value.set_value(is_negative_value,
          time_part_values[DAY_VALUE],
          time_part_values[HOUR_VALUE],
          time_part_values[MINUTE_VALUE],
          time_part_values[SECOND_VALUE],
          fs_value);
      // check and round second precision
      if (real_second_precision > expected_second_precision) {
        if (OB_FAIL(round_interval_ds(expected_second_precision, value))) {
          LOG_WARN(
              "fail to round interval ds", K(ret), K(value), K(real_second_precision), K(expected_second_precision));
        }
      }

      // calc scale after round
      // because the carry introduced by 'round()' may enlarge the leading precision
      if (OB_SUCC(ret)) {
        real_leading_precision = value.calc_leading_scale();
      }

      // check leading precision
      if (OB_SUCC(ret)) {
        if (OB_UNLIKELY(real_leading_precision > expected_leading_precision)) {
          ret = OB_ERR_THE_LEADING_PRECISION_OF_THE_INTERVAL_IS_TOO_SMALL;
          LOG_WARN("the leading precision of the interval is too small",
              K(ret),
              K(expected_leading_precision),
              K(real_leading_precision));
        } else {
          scale = ObIntervalScaleUtil::interval_ds_scale_to_ob_scale(
              static_cast<int8_t>(real_leading_precision), static_cast<int8_t>(real_second_precision));
        }
      }
    }
  }
  LOG_DEBUG("convert string to interval day to second succ",
      K(ret),
      K(value),
      K(scale),
      K(str),
      K(real_leading_precision),
      K(expected_leading_precision),
      K(real_second_precision),
      K(expected_second_precision));
  return ret;
}

int ObTimeConverter::encode_interval_ym(
X
xy0 已提交
6491
    char *buf, const int64_t len, int64_t &pos, const ObIntervalYMValue &value, const ObScale scale)
O
oceanbase-admin 已提交
6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505
{
  int ret = OB_SUCCESS;
  int8_t year_scale = ObIntervalScaleUtil::ob_scale_to_interval_ym_year_scale(static_cast<int8_t>(scale));
  const int64_t data_len = 7;
  ObIntervalParts display_parts;

  interval_ym_to_display_part(value, display_parts);
  if (OB_ISNULL(buf) || OB_UNLIKELY(len - pos < 0)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", KP(buf), K(len), K(pos));
  } else if (OB_UNLIKELY(len - pos < data_len)) {
    ret = OB_SIZE_OVERFLOW;
    LOG_WARN("invalid argument", KP(buf), K(len), K(pos), K(data_len));
  } else if (OB_FAIL(ObMySQLUtil::store_int1(
C
chuancy 已提交
6506
                 buf, len, static_cast<int8_t>(display_parts.is_negative_), pos))) {  // is_negative(1)
O
oceanbase-admin 已提交
6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519
    LOG_WARN("fail to store int", K(ret));
  } else if (OB_FAIL(ObMySQLUtil::store_int4(
                 buf, len, static_cast<int32_t>(display_parts.parts_[ObIntervalParts::YEAR_PART]), pos))) {  // years(4)
    LOG_WARN("fail to store int", K(ret));
  } else if (OB_FAIL(ObMySQLUtil::store_int1(
                 buf, len, static_cast<int8_t>(display_parts.parts_[ObIntervalParts::MONTH_PART]), pos))) {  // month(1)
    LOG_WARN("fail to store int", K(ret));
  } else if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, year_scale, pos))) {  // year scale(1)
    LOG_WARN("fail to store int", K(ret));
  }
  return ret;
}

X
xy0 已提交
6520
int ObTimeConverter::decode_interval_ym(const char *data, const int64_t len, ObIntervalYMValue &value, ObScale &scale)
O
oceanbase-admin 已提交
6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543
{
  int ret = OB_SUCCESS;
  int8_t is_negative = 0;
  int32_t year = 0;
  int8_t month = 0;
  int8_t year_scale = 0;
  const int64_t data_len = 7;

  if (OB_ISNULL(data) || OB_UNLIKELY(len < data_len)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("unexpected interval year to month length", KP(data), K(len));
  } else {
    ObMySQLUtil::get_int1(data, is_negative);
    ObMySQLUtil::get_int4(data, year);
    ObMySQLUtil::get_int1(data, month);
    ObMySQLUtil::get_int1(data, year_scale);
    value = ObIntervalYMValue(is_negative == 1, year, month);
    scale = ObIntervalScaleUtil::interval_ym_scale_to_ob_scale(year_scale);
  }
  return ret;
}

int ObTimeConverter::encode_interval_ds(
X
xy0 已提交
6544
    char *buf, const int64_t len, int64_t &pos, const ObIntervalDSValue &value, const ObScale scale)
O
oceanbase-admin 已提交
6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560
{
  int ret = OB_SUCCESS;
  int8_t day_scale = ObIntervalScaleUtil::ob_scale_to_interval_ds_day_scale(static_cast<int8_t>(scale));
  int8_t second_scale = ObIntervalScaleUtil::ob_scale_to_interval_ds_second_scale(static_cast<int8_t>(scale));
  const int64_t data_len = 14;
  ObIntervalParts display_parts;

  interval_ds_to_display_part(value, display_parts);

  if (OB_ISNULL(buf) || OB_UNLIKELY(len - pos < 0)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("invalid argument", KP(buf), K(len), K(pos));
  } else if (OB_UNLIKELY(len - pos < data_len)) {
    ret = OB_SIZE_OVERFLOW;
    LOG_WARN("invalid argument", KP(buf), K(len), K(pos), K(data_len));
  } else if (OB_FAIL(ObMySQLUtil::store_int1(
C
chuancy 已提交
6561
                 buf, len, static_cast<int8_t>(display_parts.is_negative_), pos))) {  // is_negative(1)
O
oceanbase-admin 已提交
6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591
    LOG_WARN("fail to store int", K(ret));
  } else if (OB_FAIL(ObMySQLUtil::store_int4(
                 buf, len, static_cast<int32_t>(display_parts.parts_[ObIntervalParts::DAY_PART]), pos))) {  // days(4)
    LOG_WARN("fail to store int", K(ret));
  } else if (OB_FAIL(ObMySQLUtil::store_int1(
                 buf, len, static_cast<int8_t>(display_parts.parts_[ObIntervalParts::HOUR_PART]), pos))) {  // hour(1)
    LOG_WARN("fail to store int", K(ret));
  } else if (OB_FAIL(ObMySQLUtil::store_int1(buf,
                 len,
                 static_cast<int8_t>(display_parts.parts_[ObIntervalParts::MINUTE_PART]),
                 pos))) {  // minute(1)
    LOG_WARN("fail to store int", K(ret));
  } else if (OB_FAIL(ObMySQLUtil::store_int1(buf,
                 len,
                 static_cast<int8_t>(display_parts.parts_[ObIntervalParts::SECOND_PART]),
                 pos))) {  // second(1)
    LOG_WARN("fail to store int", K(ret));
  } else if (OB_FAIL(ObMySQLUtil::store_int4(buf,
                 len,
                 static_cast<int32_t>(display_parts.parts_[ObIntervalParts::FRECTIONAL_SECOND_PART]),
                 pos))) {  // fractional second(4)
    LOG_WARN("fail to store int", K(ret));
  } else if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, day_scale, pos))) {  // day scale(1)
    LOG_WARN("fail to store int", K(ret));
  } else if (OB_FAIL(ObMySQLUtil::store_int1(buf, len, second_scale, pos))) {  // second scale(1)
    LOG_WARN("fail to store int", K(ret));
  }
  return ret;
}

X
xy0 已提交
6592
int ObTimeConverter::decode_interval_ds(const char *data, const int64_t len, ObIntervalDSValue &value, ObScale &scale)
O
oceanbase-admin 已提交
6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622
{
  int ret = OB_SUCCESS;
  int8_t is_negative = 0;
  int32_t day = 0;
  int8_t hour = 0;
  int8_t min = 0;
  int8_t second = 0;
  int32_t fs = 0;
  int8_t day_scale = 0;
  int8_t fs_scale = 0;
  const int64_t data_len = 14;

  if (OB_ISNULL(data) || OB_UNLIKELY(len < data_len)) {
    ret = OB_INVALID_ARGUMENT;
    LOG_WARN("unexpected interval day to second length", KP(data), K(len), K(ret));
  } else {
    ObMySQLUtil::get_int1(data, is_negative);
    ObMySQLUtil::get_int4(data, day);
    ObMySQLUtil::get_int1(data, hour);
    ObMySQLUtil::get_int1(data, min);
    ObMySQLUtil::get_int1(data, second);
    ObMySQLUtil::get_int4(data, fs);
    ObMySQLUtil::get_int1(data, day_scale);
    ObMySQLUtil::get_int1(data, fs_scale);
    value = ObIntervalDSValue(is_negative == 1, day, hour, min, second, fs);
    scale = ObIntervalScaleUtil::interval_ds_scale_to_ob_scale(day_scale, fs_scale);
  }
  return ret;
}

X
xy0 已提交
6623
void ObTimeConverter::interval_ym_to_display_part(const ObIntervalYMValue &value, ObIntervalParts &display_parts)
O
oceanbase-admin 已提交
6624 6625 6626 6627
{
  int64_t abs_nmonth = std::abs(value.get_nmonth());
  display_parts.parts_[ObIntervalParts::YEAR_PART] = static_cast<int32_t>(abs_nmonth / MONTHS_PER_YEAR);
  display_parts.parts_[ObIntervalParts::MONTH_PART] = static_cast<int32_t>(abs_nmonth % MONTHS_PER_YEAR);
C
chuancy 已提交
6628
  display_parts.is_negative_ = value.is_negative();
O
oceanbase-admin 已提交
6629 6630
}

X
xy0 已提交
6631
void ObTimeConverter::interval_ds_to_display_part(const ObIntervalDSValue &value, ObIntervalParts &display_parts)
O
oceanbase-admin 已提交
6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642
{
  int64_t abs_nsecond = std::abs(value.get_nsecond());
  int32_t abs_fs = std::abs(value.get_fs());
  int64_t rest_seconds_in_day = abs_nsecond % SECS_PER_DAY;
  int64_t rest_minutes_in_day = rest_seconds_in_day / SECS_PER_MIN;

  display_parts.parts_[ObIntervalParts::DAY_PART] = static_cast<int32_t>(abs_nsecond / SECS_PER_DAY);
  display_parts.parts_[ObIntervalParts::HOUR_PART] = static_cast<int32_t>(rest_minutes_in_day / MINS_PER_HOUR);
  display_parts.parts_[ObIntervalParts::MINUTE_PART] = static_cast<int32_t>(rest_minutes_in_day % MINS_PER_HOUR);
  display_parts.parts_[ObIntervalParts::SECOND_PART] = static_cast<int32_t>(rest_seconds_in_day % SECS_PER_MIN);
  display_parts.parts_[ObIntervalParts::FRECTIONAL_SECOND_PART] = abs_fs;
C
chuancy 已提交
6643
  display_parts.is_negative_ = value.is_negative();
O
oceanbase-admin 已提交
6644 6645
}

X
xy0 已提交
6646
int ObTimeConverter::iso_str_to_interval_ym(const ObString &str, ObIntervalYMValue &value)
O
oceanbase-admin 已提交
6647 6648 6649 6650 6651 6652
{
  int ret = OB_SUCCESS;
  ObIntervalParts interval_parts;
  if (OB_FAIL(iso_interval_str_parse(str, interval_parts, ObIntervalParts::YEAR_PART))) {
    LOG_WARN("fail to parse interval str", K(ret));
  } else {
C
chuancy 已提交
6653
    value.set_value(interval_parts.is_negative_,
O
oceanbase-admin 已提交
6654 6655 6656 6657 6658 6659 6660 6661 6662 6663
        interval_parts.parts_[ObIntervalParts::YEAR_PART],
        interval_parts.parts_[ObIntervalParts::MONTH_PART]);
    if (OB_FAIL(value.validate())) {
      LOG_WARN("invalid interval", K(ret), K(value));
    }
    LOG_DEBUG("result value", K(ret), K(value));
  }
  return ret;
}

X
xy0 已提交
6664
int ObTimeConverter::iso_str_to_interval_ds(const ObString &str, ObIntervalDSValue &value)
O
oceanbase-admin 已提交
6665 6666 6667 6668 6669 6670
{
  int ret = OB_SUCCESS;
  ObIntervalParts interval_parts;
  if (OB_FAIL(iso_interval_str_parse(str, interval_parts, ObIntervalParts::DAY_PART))) {
    LOG_WARN("fail to parse interval str", K(ret));
  } else {
C
chuancy 已提交
6671
    value.set_value(interval_parts.is_negative_,
O
oceanbase-admin 已提交
6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685
        interval_parts.parts_[ObIntervalParts::DAY_PART],
        interval_parts.parts_[ObIntervalParts::HOUR_PART],
        interval_parts.parts_[ObIntervalParts::MINUTE_PART],
        interval_parts.parts_[ObIntervalParts::SECOND_PART],
        interval_parts.parts_[ObIntervalParts::FRECTIONAL_SECOND_PART]);
    if (OB_FAIL(value.validate())) {
      LOG_WARN("invalid interval", K(ret), K(value));
    }
    LOG_DEBUG("result value", K(ret), K(value));
  }
  return ret;
}

int ObTimeConverter::iso_interval_str_parse(
X
xy0 已提交
6686
    const ObString &str, ObIntervalParts &interval_parts, ObIntervalParts::PartName begin_part)
O
oceanbase-admin 已提交
6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699
{
  int ret = OB_SUCCESS;
  static const char UNIT[ObIntervalParts::PART_CNT] = {'Y', 'M', 'D', 'H', 'M', '.', 'S'};
  static_assert(sizeof(UNIT) == ObIntervalParts::PART_CNT * sizeof(char), "array length not match");
  int64_t matched_part_set = 0;

  interval_parts.reset();
  ObDFMParseCtx parse_ctx(str.ptr(), str.length());

  if (OB_FAIL(ObDFMUtil::check_ctx_valid(parse_ctx, OB_ERR_INTERVAL_INVALID))) {
    LOG_WARN("empty input str", K(ret));
  } else if (parse_ctx.cur_ch_[0] == '-') {
    parse_ctx.update(1);
C
chuancy 已提交
6700
    interval_parts.is_negative_ = true;
O
oceanbase-admin 已提交
6701
  } else {
C
chuancy 已提交
6702
    interval_parts.is_negative_ = false;
O
oceanbase-admin 已提交
6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786
  }

  if (OB_FAIL(ret)) {
  } else if (OB_FAIL(ObDFMUtil::check_ctx_valid(parse_ctx, OB_ERR_INTERVAL_INVALID))) {
    LOG_WARN("parsing finish", K(ret));
  } else if (OB_FAIL(ObDFMUtil::match_char(parse_ctx, 'P', OB_ERR_INTERVAL_INVALID))) {
    LOG_WARN("parsing error", K(ret));
  } else if (OB_FAIL(ObDFMUtil::check_ctx_valid(parse_ctx, OB_ERR_INTERVAL_INVALID))) {
    LOG_WARN("parsing finish", K(ret));
  }

  bool is_before_t = true;
  int pos = begin_part;

  while (OB_SUCC(ret) && !parse_ctx.is_parse_finish() && pos < ObIntervalParts::PART_CNT) {
    if (parse_ctx.cur_ch_[0] == 'T') {
      if (OB_UNLIKELY(!is_before_t)) {
        ret = OB_ERR_INTERVAL_INVALID;
        LOG_WARN("parsing error", K(ret), K(parse_ctx), K(is_before_t));
      } else {
        parse_ctx.update(1);
        is_before_t = false;
      }
    } else {
      int32_t result_value = 0;
      int64_t value_len = 0;

      // parse number
      if (OB_FAIL(ObDFMUtil::match_int_value(parse_ctx, MAX_SCALE_FOR_ORACLE_TEMPORAL, value_len, result_value))) {
        ret = OB_ERR_INTERVAL_INVALID;  // overwrite error code
        LOG_WARN("fail to match int value", K(ret));
      } else if (FALSE_IT(parse_ctx.update(value_len))) {
      } else if (OB_FAIL(ObDFMUtil::check_ctx_valid(parse_ctx, OB_ERR_INTERVAL_INVALID))) {
        LOG_WARN("parsing finish", K(ret));
      }
      if (OB_SUCC(ret)) {
        for (;
             pos < ObIntervalParts::PART_CNT &&
             (static_cast<bool>(is_before_t ^ (pos < ObIntervalParts::HOUR_PART)) || UNIT[pos] != parse_ctx.cur_ch_[0]);
             ++pos) {}

        if (OB_UNLIKELY(pos >= ObIntervalParts::PART_CNT)) {
          // not found
          ret = OB_ERR_INTERVAL_INVALID;
          LOG_WARN("parsing error", K(ret), K(pos), K(is_before_t));
        } else {
          matched_part_set |= 1 << pos;
          if (pos == ObIntervalParts::FRECTIONAL_SECOND_PART) {
            if ((matched_part_set & 1 << ObIntervalParts::SECOND_PART) > 0) {
              interval_parts.parts_[ObIntervalParts::FRECTIONAL_SECOND_PART] =
                  result_value * power_of_10[MAX_SCALE_FOR_ORACLE_TEMPORAL - value_len];
            } else {
              interval_parts.parts_[ObIntervalParts::SECOND_PART] = result_value;
            }
          } else {
            interval_parts.parts_[pos] = result_value;
          }
        }
        parse_ctx.update(1);
      }
      LOG_DEBUG("parse one part", K(ret), K(parse_ctx), K(pos), K(value_len), K(result_value));
    }  // end if
  }    // end while

  if (OB_SUCC(ret)) {
    bool has_any_part = (matched_part_set > 0);
    bool has_dot = ((matched_part_set & 1 << ObIntervalParts::SECOND_PART) > 0);
    bool has_fsecond = ((matched_part_set & 1 << ObIntervalParts::FRECTIONAL_SECOND_PART) > 0);
    if (!parse_ctx.is_parse_finish() || !has_any_part || (has_dot && !has_fsecond)) {
      ret = OB_ERR_INTERVAL_INVALID;
      LOG_WARN("date picture end", K(ret), K(parse_ctx), K(has_dot), K(has_fsecond), K(has_any_part));
    }
  }
  LOG_DEBUG("iso interval str parse",
      K(ret),
      K(matched_part_set),
      "interval_parts",
      ObArrayWrap<int32_t>(interval_parts.parts_, ObIntervalParts::PART_CNT));

  return ret;
}

}  // namespace common
}  // namespace oceanbase