timeParseTest.cpp 5.8 KB
Newer Older
J
Jun Li 已提交
1

H
hjxilinx 已提交
2 3 4
#include <gtest/gtest.h>
#include <cassert>
#include <iostream>
J
Jun Li 已提交
5
#include <inttypes.h>
H
hjxilinx 已提交
6

J
Jun Li 已提交
7
#include "os.h"
H
hjxilinx 已提交
8
#include "taos.h"
9
#include "ttoken.h"
H
hjxilinx 已提交
10 11 12 13 14 15 16 17 18 19
#include "tutil.h"

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

extern void deltaToUtcInitOnce();
/* test parse time function */
TEST(testCase, parse_time) {
J
Jun Li 已提交
20

H
hjxilinx 已提交
21 22
  taos_options(TSDB_OPTION_TIMEZONE, "GMT-8");
  deltaToUtcInitOnce();
J
Jun Li 已提交
23

H
hjxilinx 已提交
24 25 26 27 28
  char t1[] = "2018-1-1 1:1:1.952798";
  char t13[] = "1970-1-1 0:0:0";

  int64_t time = 0, time1 = 0;

dengyihao's avatar
dengyihao 已提交
29
  taosParseTime(t1, &time, strlen(t1), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
30 31
  EXPECT_EQ(time, 1514739661952);

dengyihao's avatar
dengyihao 已提交
32
  taosParseTime(t13, &time, strlen(t13), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
33 34 35
  EXPECT_EQ(time, timezone * MILLISECOND_PER_SECOND);

  char t2[] = "2018-1-1T1:1:1.952Z";
dengyihao's avatar
dengyihao 已提交
36
  taosParseTime(t2, &time, strlen(t2), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
37 38 39 40

  EXPECT_EQ(time, 1514739661952 + 28800000);

  char t3[] = "2018-1-1 1:01:01.952";
dengyihao's avatar
dengyihao 已提交
41
  taosParseTime(t3, &time, strlen(t3), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
42 43 44 45 46 47 48 49
  EXPECT_EQ(time, 1514739661952);

  char t4[] = "2018-1-1 1:01:01.9";
  char t5[] = "2018-1-1 1:01:1.900";
  char t6[] = "2018-01-01 1:1:1.90";
  char t7[] = "2018-01-01 01:01:01.9";
  char t8[] = "2018-01-01 01:01:01.9007865";

dengyihao's avatar
dengyihao 已提交
50 51
  taosParseTime(t4, &time, strlen(t4), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t5, &time1, strlen(t5), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
52 53
  EXPECT_EQ(time, time1);

dengyihao's avatar
dengyihao 已提交
54 55
  taosParseTime(t4, &time, strlen(t4), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t6, &time1, strlen(t6), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
56 57
  EXPECT_EQ(time, time1);

dengyihao's avatar
dengyihao 已提交
58 59
  taosParseTime(t4, &time, strlen(t4), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t7, &time1, strlen(t7), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
60 61
  EXPECT_EQ(time, time1);

dengyihao's avatar
dengyihao 已提交
62 63
  taosParseTime(t5, &time, strlen(t5), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t8, &time1, strlen(t8), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
64 65 66 67
  EXPECT_EQ(time, time1);

  char t9[] = "2017-4-3 1:1:2.980";
  char t10[] = "2017-4-3T2:1:2.98+9:00";
dengyihao's avatar
dengyihao 已提交
68 69
  taosParseTime(t9, &time, strlen(t9), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t10, &time1, strlen(t10), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
70 71 72
  EXPECT_EQ(time, time1);

  char t11[] = "2017-4-3T2:1:2.98+09:00";
dengyihao's avatar
dengyihao 已提交
73 74
  taosParseTime(t11, &time, strlen(t11), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t10, &time1, strlen(t10), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
75 76 77
  EXPECT_EQ(time, time1);

  char t12[] = "2017-4-3T2:1:2.98+0900";
dengyihao's avatar
dengyihao 已提交
78 79
  taosParseTime(t11, &time, strlen(t11), TSDB_TIME_PRECISION_MILLI, 0);
  taosParseTime(t12, &time1, strlen(t12), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
80 81
  EXPECT_EQ(time, time1);

J
Jun Li 已提交
82
  taos_options(TSDB_OPTION_TIMEZONE, "UTC");
H
hjxilinx 已提交
83
  deltaToUtcInitOnce();
J
Jun Li 已提交
84

dengyihao's avatar
dengyihao 已提交
85
  taosParseTime(t13, &time, strlen(t13), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
86 87 88 89
  EXPECT_EQ(time, 0);

  taos_options(TSDB_OPTION_TIMEZONE, "Asia/Shanghai");
  deltaToUtcInitOnce();
J
Jun Li 已提交
90

H
hjxilinx 已提交
91
  char t14[] = "1970-1-1T0:0:0Z";
dengyihao's avatar
dengyihao 已提交
92
  taosParseTime(t14, &time, strlen(t14), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
93 94 95
  EXPECT_EQ(time, 0);

  char t40[] = "1970-1-1 0:0:0.999999999";
dengyihao's avatar
dengyihao 已提交
96
  taosParseTime(t40, &time, strlen(t40), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
97 98 99
  EXPECT_EQ(time, 999 + timezone * MILLISECOND_PER_SECOND);

  char t41[] = "1997-1-1 0:0:0.999999999";
dengyihao's avatar
dengyihao 已提交
100
  taosParseTime(t41, &time, strlen(t41), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
101 102
  EXPECT_EQ(time, 852048000999);

S
Shengliang Guan 已提交
103
  // int64_t k = timezone;
H
hjxilinx 已提交
104
  char    t42[] = "1997-1-1T0:0:0.999999999Z";
dengyihao's avatar
dengyihao 已提交
105
  taosParseTime(t42, &time, strlen(t42), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
106 107 108 109 110
  EXPECT_EQ(time, 852048000999 - timezone * MILLISECOND_PER_SECOND);

  ////////////////////////////////////////////////////////////////////
  // illegal timestamp format
  char t15[] = "2017-12-33 0:0:0";
dengyihao's avatar
dengyihao 已提交
111
  EXPECT_EQ(taosParseTime(t15, &time, strlen(t15), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
112 113

  char t16[] = "2017-12-31 99:0:0";
dengyihao's avatar
dengyihao 已提交
114
  EXPECT_EQ(taosParseTime(t16, &time, strlen(t16), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
115 116

  char t17[] = "2017-12-31T9:0:0";
dengyihao's avatar
dengyihao 已提交
117
  EXPECT_EQ(taosParseTime(t17, &time, strlen(t17), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
118 119

  char t18[] = "2017-12-31T9:0:0.Z";
dengyihao's avatar
dengyihao 已提交
120
  EXPECT_EQ(taosParseTime(t18, &time, strlen(t18), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
121 122

  char t19[] = "2017-12-31 9:0:0.-1";
dengyihao's avatar
dengyihao 已提交
123
  EXPECT_EQ(taosParseTime(t19, &time, strlen(t19), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
124 125

  char t20[] = "2017-12-31 9:0:0.1+12:99";
dengyihao's avatar
dengyihao 已提交
126
  EXPECT_EQ(taosParseTime(t20, &time, strlen(t20), TSDB_TIME_PRECISION_MILLI, 0), 0);
H
hjxilinx 已提交
127 128 129
  EXPECT_EQ(time, 1514682000100);

  char t21[] = "2017-12-31T9:0:0.1+12:99";
dengyihao's avatar
dengyihao 已提交
130
  EXPECT_EQ(taosParseTime(t21, &time, strlen(t21), TSDB_TIME_PRECISION_MILLI, 0), -1);
H
hjxilinx 已提交
131 132

  char t22[] = "2017-12-31 9:0:0.1+13:1";
dengyihao's avatar
dengyihao 已提交
133
  EXPECT_EQ(taosParseTime(t22, &time, strlen(t22), TSDB_TIME_PRECISION_MILLI, 0), 0);
H
hjxilinx 已提交
134 135

  char t23[] = "2017-12-31T9:0:0.1+13:1";
dengyihao's avatar
dengyihao 已提交
136
  EXPECT_EQ(taosParseTime(t23, &time, strlen(t23), TSDB_TIME_PRECISION_MILLI, 0), 0);
H
hjxilinx 已提交
137 138 139


  //======================== add some case ============================//
J
Jun Li 已提交
140

H
hjxilinx 已提交
141
  char b1[] = "9999-12-31 23:59:59.999";
dengyihao's avatar
dengyihao 已提交
142
  taosParseTime(b1, &time, strlen(b1), TSDB_TIME_PRECISION_MILLI,0);
H
hjxilinx 已提交
143 144 145 146
  EXPECT_EQ(time, 253402271999999);


  char b2[] = "2020-01-01 01:01:01.321";
dengyihao's avatar
dengyihao 已提交
147
  taosParseTime(b2, &time, strlen(b2), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
148 149
  EXPECT_EQ(time, 1577811661321);

J
Jun Li 已提交
150
  taos_options(TSDB_OPTION_TIMEZONE, "America/New_York");
H
hjxilinx 已提交
151
  deltaToUtcInitOnce();
J
Jun Li 已提交
152

dengyihao's avatar
dengyihao 已提交
153
  taosParseTime(t13, &time, strlen(t13), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
154 155
  EXPECT_EQ(time, 18000 * MILLISECOND_PER_SECOND);

J
Jun Li 已提交
156
  taos_options(TSDB_OPTION_TIMEZONE, "Asia/Tokyo");
H
hjxilinx 已提交
157
  deltaToUtcInitOnce();
J
Jun Li 已提交
158

dengyihao's avatar
dengyihao 已提交
159
  taosParseTime(t13, &time, strlen(t13), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
160 161 162 163
  EXPECT_EQ(time, -32400 * MILLISECOND_PER_SECOND);

  taos_options(TSDB_OPTION_TIMEZONE, "Asia/Shanghai");
  deltaToUtcInitOnce();
J
Jun Li 已提交
164

dengyihao's avatar
dengyihao 已提交
165
  taosParseTime(t13, &time, strlen(t13), TSDB_TIME_PRECISION_MILLI, 0);
H
hjxilinx 已提交
166
  EXPECT_EQ(time, -28800 * MILLISECOND_PER_SECOND);
H
Haojun Liao 已提交
167

S
Shengliang Guan 已提交
168
  char t[] = "2021-01-08T02:11:40.000+00:00";
H
Haojun Liao 已提交
169
  taosParseTime(t, &time, strlen(t), TSDB_TIME_PRECISION_MILLI, 0);
J
Jun Li 已提交
170
  printf("%" PRId64 "\n", time);
H
hjxilinx 已提交
171 172 173
}