datetime.cpp 10.5 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
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 PSL v2 for more details. */

//
// Created by Longda on 2010
//

#include "common/time/datetime.h"

#include "pthread.h"
#include "stdio.h"
#include "string.h"
#include <iomanip>
#include <sstream>
#include <string>
namespace common {

25 26
DateTime::DateTime(std::string &xml_str)
{
羽飞's avatar
羽飞 已提交
27
  tm tmp;
28 29 30 31 32 33 34 35
  sscanf(xml_str.c_str(),
      "%04d-%02d-%02dT%02d:%02d:%02dZ",
      &tmp.tm_year,
      &tmp.tm_mon,
      &tmp.tm_mday,
      &tmp.tm_hour,
      &tmp.tm_min,
      &tmp.tm_sec);
羽飞's avatar
羽飞 已提交
36 37 38 39
  m_date = julian_date(tmp.tm_year, tmp.tm_mon, tmp.tm_mday);
  m_time = make_hms(tmp.tm_hour, tmp.tm_min, tmp.tm_sec, 0);
}

40 41
time_t DateTime::str_to_time_t(std::string &xml_str)
{
羽飞's avatar
羽飞 已提交
42
  tm tmp;
43 44 45 46 47 48 49 50
  sscanf(xml_str.c_str(),
      "%04d-%02d-%02dT%02d:%02d:%02dZ",
      &tmp.tm_year,
      &tmp.tm_mon,
      &tmp.tm_mday,
      &tmp.tm_hour,
      &tmp.tm_min,
      &tmp.tm_sec);
羽飞's avatar
羽飞 已提交
51 52 53 54 55
  m_date = julian_date(tmp.tm_year, tmp.tm_mon, tmp.tm_mday);
  m_time = make_hms(tmp.tm_hour, tmp.tm_min, tmp.tm_sec, 0);
  return to_time_t();
}

56 57
std::string DateTime::time_t_to_str(int timet)
{
羽飞's avatar
羽飞 已提交
58 59 60 61 62
  std::ostringstream oss;
  oss << std::dec << std::setw(10) << timet;
  return oss.str();
}

63 64
std::string DateTime::time_t_to_xml_str(time_t timet)
{
羽飞's avatar
羽飞 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  std::string ret_val;
  std::ostringstream oss;
  struct tm tmbuf;
  tm *tm_info = gmtime_r(&timet, &tmbuf);
  oss << tm_info->tm_year + 1900 << "-";
  if ((tm_info->tm_mon + 1) <= 9)
    oss << "0";
  oss << tm_info->tm_mon + 1 << "-";
  if (tm_info->tm_mday <= 9)
    oss << "0";
  oss << tm_info->tm_mday << "T";
  if (tm_info->tm_hour <= 9)
    oss << "0";
  oss << tm_info->tm_hour << ":";
  if (tm_info->tm_min <= 9)
    oss << "0";
  oss << tm_info->tm_min << ":";
  if (tm_info->tm_sec <= 9)
    oss << "0";
  oss << tm_info->tm_sec << "Z";
  ret_val = oss.str();
  return ret_val;
}

89 90
std::string DateTime::str_to_time_t_str(std::string &xml_str)
{
羽飞's avatar
羽飞 已提交
91 92
  tm tmp;
  std::ostringstream oss;
93 94 95 96 97 98 99 100
  sscanf(xml_str.c_str(),
      "%04d-%02d-%02dT%02d:%02d:%02dZ",
      &tmp.tm_year,
      &tmp.tm_mon,
      &tmp.tm_mday,
      &tmp.tm_hour,
      &tmp.tm_min,
      &tmp.tm_sec);
羽飞's avatar
羽飞 已提交
101 102 103 104 105 106 107
  m_date = julian_date(tmp.tm_year, tmp.tm_mon, tmp.tm_mday);
  m_time = make_hms(tmp.tm_hour, tmp.tm_min, tmp.tm_sec, 0);
  time_t timestamp = to_time_t();
  oss << std::dec << std::setw(10) << timestamp;
  return oss.str();
}

108 109
time_t DateTime::nowtimet()
{
羽飞's avatar
羽飞 已提交
110 111
  struct timeval tv;
  gettimeofday(&tv, 0);
112 113
  return tv.tv_sec;
  ;
羽飞's avatar
羽飞 已提交
114 115
}

116 117
DateTime DateTime::now()
{
羽飞's avatar
羽飞 已提交
118 119 120 121 122 123
  struct timeval tv;
  gettimeofday(&tv, 0);
  return from_time_t(tv.tv_sec, tv.tv_usec / 1000);
}

//! Return date and time as a string in Xml Schema date-time format
124 125
std::string DateTime::to_xml_date_time()
{
羽飞's avatar
羽飞 已提交
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

  std::string ret_val;
  tm tm_info;
  std::ostringstream oss;

  tm_info = to_tm();
  oss << tm_info.tm_year + 1900 << "-";
  if ((tm_info.tm_mon + 1) <= 9)
    oss << "0";
  oss << tm_info.tm_mon + 1 << "-";
  if (tm_info.tm_mday <= 9)
    oss << "0";
  oss << tm_info.tm_mday << "T";
  if (tm_info.tm_hour <= 9)
    oss << "0";
  oss << tm_info.tm_hour << ":";
  if (tm_info.tm_min <= 9)
    oss << "0";
  oss << tm_info.tm_min << ":";
  if (tm_info.tm_sec <= 9)
    oss << "0";
  oss << tm_info.tm_sec << "Z";
  ret_val = oss.str();
  return ret_val;
}

152 153
time_t DateTime::add_duration(std::string xml_duration)
{
羽飞's avatar
羽飞 已提交
154 155 156 157
  add_duration_date_time(xml_duration);
  return to_time_t();
}

158 159
void DateTime::add_duration_date_time(std::string xml_duration)
{
羽飞's avatar
羽飞 已提交
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 201 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
  // start datetime values
  int s_year, s_month, s_day;
  int s_hour, s_min, s_sec, s_millis = 0;
  get_ymd(s_year, s_month, s_day);
  get_hms(s_hour, s_min, s_sec, s_millis);

  // temp values
  int tmp_month, tmp_sec, tmp_min, tmp_hour, tmp_day;

  // duration values
  struct tm dur_t;
  parse_duration(xml_duration, dur_t);

  // end values
  int e_year, e_month, e_day, e_hour, e_min, e_sec, e_millis = 0;

  // months
  tmp_month = s_month + dur_t.tm_mon;
  e_month = ((tmp_month - 1) % 12) + 1;
  int carry_month = ((tmp_month - 1) / 12);

  // years
  e_year = s_year + dur_t.tm_year + carry_month;

  // seconds
  tmp_sec = s_sec + dur_t.tm_sec;
  e_sec = tmp_sec % 60;
  int carry_sec = tmp_sec / 60;

  // minutes
  tmp_min = s_min + dur_t.tm_min + carry_sec;
  e_min = tmp_min % 60;
  int carry_min = tmp_min / 60;

  // hours
  tmp_hour = s_hour + dur_t.tm_hour + carry_min;
  e_hour = tmp_hour % 24;
  int carry_hr = tmp_hour / 24;

  // days
  if (s_day > max_day_in_month_for(e_year, e_month)) {
    tmp_day = max_day_in_month_for(e_year, e_month);
  } else {
    if (s_day < 1) {
      tmp_day = 1;
    } else {
      tmp_day = s_day;
    }
  }
  e_day = tmp_day + dur_t.tm_mday + carry_hr;
  int carry_day = 0;
  while (true) {
    if (e_day < 1) {
      e_day = e_day + max_day_in_month_for(e_year, e_month - 1);
      carry_day = -1;
    } else {
      if (e_day > max_day_in_month_for(e_year, e_month)) {
        e_day -= max_day_in_month_for(e_year, e_month);
        carry_day = 1;
      } else {
        break;
      }
    }
    tmp_month = e_month + carry_day;
    e_month = ((tmp_month - 1) % 12) + 1;
    e_year = e_year + (tmp_month - 1) / 12;
  }
  m_date = julian_date(e_year, e_month, e_day);
  m_time = make_hms(e_hour, e_min, e_sec, e_millis);
  return;
}

232 233
int DateTime::max_day_in_month_for(int yr, int month)
{
羽飞's avatar
羽飞 已提交
234 235 236
  int tmp_month = ((month - 1) % 12) + 1;
  int tmp_year = yr + ((tmp_month - 1) / 12);

237 238
  if (tmp_month == MON_JAN || tmp_month == MON_MAR || tmp_month == MON_MAY || tmp_month == MON_JUL ||
      tmp_month == MON_AUG || tmp_month == MON_OCT || tmp_month == MON_DEC) {
羽飞's avatar
羽飞 已提交
239 240
    return 31;
  } else {
241
    if (tmp_month == MON_APR || tmp_month == MON_JUN || tmp_month == MON_SEP || tmp_month == MON_NOV)
羽飞's avatar
羽飞 已提交
242 243
      return 30;
    else {
244
      if (tmp_month == MON_FEB && ((0 == tmp_year % 400) || ((0 != tmp_year % 100) && 0 == tmp_year % 4))) {
羽飞's avatar
羽飞 已提交
245 246 247 248 249 250 251
        return 29;
      } else
        return 28;
    }
  }
}

252 253
void DateTime::parse_duration(std::string dur_str, struct tm &tm_t)
{
羽飞's avatar
羽飞 已提交
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
  std::string::size_type index = 0;
  bzero(&tm_t, sizeof(tm_t));
  if (dur_str[index] != 'P') {
    return;
  }
  int ind_t = dur_str.find('T', 0);

  index++;
  int ind_y = dur_str.find('Y', index);
  if (ind_y != -1) {
    int sign = 1;
    if (dur_str[index] == '-') {
      sign = -1;
      index++;
    }
    std::string sY = dur_str.substr(index, ind_y);
    sscanf(sY.c_str(), "%d", &tm_t.tm_year);
    tm_t.tm_year *= sign;
    index = ind_y + 1;
  }

  int ind_m = dur_str.find('M', index);
  if ((ind_m != -1) && (((ind_t > -1) && (ind_m < ind_t)) || ind_t == -1)) {
    int sign = 1;
    if (dur_str[index] == '-') {
      sign = -1;
      index++;
    }
    sscanf(dur_str.substr(index, ind_m).c_str(), "%d", &tm_t.tm_mon);
    tm_t.tm_mon *= sign;
    index = ind_m + 1;
  }

  int ind_d = dur_str.find('D', index);
  int sign = 1;
  if (ind_d != -1) {
    if (dur_str[index] == '-') {
      sign = -1;
      index++;
    }
    sscanf(dur_str.substr(index, ind_d).c_str(), "%d", &tm_t.tm_mday);
    tm_t.tm_mday *= sign;
    // index = ind_d + 1; // not used
  }

  if (ind_t == -1) {
    tm_t.tm_hour = tm_t.tm_min = tm_t.tm_sec = 0;
    return;
  }
  index = ind_t + 1;

  int ind_h = dur_str.find('H', index);
  if (ind_h != -1) {
    int sign = 1;
    if (dur_str[index] == '-') {
      sign = -1;
      index++;
    }
    sscanf(dur_str.substr(index, ind_h).c_str(), "%d", &tm_t.tm_hour);
    tm_t.tm_hour *= sign;
    index = ind_h + 1;
  }

  int ind_min = dur_str.find('M', index);
  if (ind_min != -1) {
    int sign = 1;
    if (dur_str[index] == '-') {
      sign = -1;
      index++;
    }
    sscanf(dur_str.substr(index, ind_min).c_str(), "%d", &tm_t.tm_min);
    tm_t.tm_min *= sign;
    index = ind_min + 1;
  }

  int ind_s = dur_str.find('S', index);
  if (ind_s != -1) {
    int sign = 1;
    if (dur_str[index] == '-') {
      sign = -1;
      index++;
    }
    sscanf(dur_str.substr(index, ind_s).c_str(), "%d", &tm_t.tm_sec);
    tm_t.tm_sec *= sign;
  }
  return;
}

// generate OBJ_ID_TIMESTMP_DIGITS types unique timestamp string
// caller doesn't need get any lock
#define OBJ_ID_TIMESTMP_DIGITS 14
345 346
std::string Now::unique()
{
羽飞's avatar
羽飞 已提交
347
  struct timeval tv;
羽飞's avatar
羽飞 已提交
348 349
  uint64_t temp;
  static uint64_t last_unique = 0;
羽飞's avatar
羽飞 已提交
350 351 352 353 354 355
#if defined(LINUX)
  static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
#elif defined(__MACH__)
  static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER;
#endif
  gettimeofday(&tv, NULL);
羽飞's avatar
羽飞 已提交
356
  temp = (((uint64_t)tv.tv_sec) << 20) + tv.tv_usec;
羽飞's avatar
羽飞 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
  pthread_mutex_lock(&mutex);
  if (temp > last_unique) {
    // record last timeStamp
    last_unique = temp;
  } else {
    // increase the last timeStamp and use it as unique timestamp
    // as NTP may sync local time clock backward.
    // after time catch up, will change back to use time again.
    last_unique++;
    // set the new last_unique as unique timestamp
    temp = last_unique;
  }
  pthread_mutex_unlock(&mutex);

  // further refine below code, the common time unique function
  //      should not cover OBJ_ID_TIMESTMP_DIGITS, which is only
  //      related with the object id.
  std::ostringstream oss;
375
  oss << std::hex << std::setw(OBJ_ID_TIMESTMP_DIGITS) << std::setfill('0') << temp;
羽飞's avatar
羽飞 已提交
376 377 378
  return oss.str();
}

379 380
bool DateTime::is_valid_xml_datetime(const std::string &str)
{
羽飞's avatar
羽飞 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  // check length. 20 is the length of a xml date
  if (str.length() != 20)
    return false;

  // check each character is correct
  const char *const flag = "0000-00-00T00:00:00Z";
  for (unsigned int i = 0; i < str.length(); ++i) {
    if (flag[i] == '0') {
      if (!isdigit(str[i]))
        return false;
    } else if (flag[i] != str[i]) {
      return false;
    }
  }

  // check month, date, hour, min, second is valid
  tm tmp;
398 399 400 401 402 403 404 405
  int ret = sscanf(str.c_str(),
      "%04d-%02d-%02dT%02d:%02d:%02dZ",
      &tmp.tm_year,
      &tmp.tm_mon,
      &tmp.tm_mday,
      &tmp.tm_hour,
      &tmp.tm_min,
      &tmp.tm_sec);
羽飞's avatar
羽飞 已提交
406 407

  if (ret != 6)
408
    return false;  // should have 6 match
羽飞's avatar
羽飞 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423

  if (tmp.tm_mon > 12 || tmp.tm_mon <= 0)
    return false;
  if (tmp.tm_mday > 31 || tmp.tm_mday <= 0)
    return false;
  if (tmp.tm_hour > 24 || tmp.tm_hour < 0)
    return false;
  if (tmp.tm_min > 60 || tmp.tm_min < 0)
    return false;
  if (tmp.tm_sec > 60 || tmp.tm_sec < 0)
    return false;

  return true;
}

424
}  // namespace common