NebulaGraph CPP Client  release-3.4
TimeConversion.h
1 /* Copyright (c) 2021 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 #pragma once
7 
8 #include <cstdint>
9 
10 #include "common/datatypes/Date.h"
11 
12 namespace nebula {
13 namespace time {
14 
16  public:
17  explicit TimeConversion(...) = delete;
18 
19  static int64_t dateTimeDiffSeconds(const DateTime &dateTime0, const DateTime &dateTime1);
20 
21  // unix time
22  static int64_t dateTimeToUnixSeconds(const DateTime &dateTime) {
23  return dateTimeDiffSeconds(dateTime, kEpoch);
24  }
25 
26  static DateTime unixSecondsToDateTime(int64_t seconds);
27 
28  // Shift the DateTime in timezone space
29  static DateTime dateTimeShift(const DateTime &dateTime, int64_t offsetSeconds) {
30  if (offsetSeconds == 0) {
31  return dateTime;
32  }
33  auto dt = unixSecondsToDateTime(dateTimeToUnixSeconds(dateTime) + offsetSeconds);
34  dt.microsec = dateTime.microsec;
35  return dt;
36  }
37 
38  // unix time
39  static int64_t dateToUnixSeconds(const Date &date) {
40  return dateTimeDiffSeconds(DateTime(date), kEpoch);
41  }
42 
43  static Date unixSecondsToDate(int64_t seconds) {
44  auto dateTime = unixSecondsToDateTime(seconds);
45  return Date(dateTime.year, dateTime.month, dateTime.day);
46  }
47 
48  // Shift the DateTime in timezone space
49  static Date dateShift(const Date &date, int64_t offsetSeconds) {
50  if (offsetSeconds == 0) {
51  return date;
52  }
53  return unixSecondsToDate(dateToUnixSeconds(date) + offsetSeconds);
54  }
55 
56  // unix time
57  static int64_t timeToSeconds(const Time &time) {
58  int64_t seconds = time.sec;
59  seconds += (time.minute * kSecondsOfMinute);
60  seconds += (time.hour * kSecondsOfHour);
61  return seconds;
62  }
63 
64  static Time unixSecondsToTime(int64_t seconds) {
65  Time t;
66  auto dt = unixSecondsToDateTime(seconds);
67  t.hour = dt.hour;
68  t.minute = dt.minute;
69  t.sec = dt.sec;
70  return t;
71  }
72 
73  // Shift the Time in timezone space
74  static Time timeShift(const Time &time, int64_t offsetSeconds) {
75  if (offsetSeconds == 0) {
76  return time;
77  }
78  auto t = unixSecondsToTime(timeToSeconds(time) + offsetSeconds);
79  t.microsec = time.microsec;
80  return t;
81  }
82 
83  // https://en.wikipedia.org/wiki/Leap_year#Leap_day
84  static bool isLeapYear(int16_t year) {
85  if (year % 4 != 0) {
86  return false;
87  } else if (year % 100 != 0) {
88  return true;
89  } else if (year % 400 != 0) {
90  return false;
91  } else {
92  return true;
93  }
94  }
95 
96  static const DateTime kEpoch;
97 
98  static constexpr int kDayOfLeapYear = 366;
99  static constexpr int kDayOfCommonYear = 365;
100 
101  static constexpr int64_t kSecondsOfMinute = 60;
102  static constexpr int64_t kSecondsOfHour = 60 * kSecondsOfMinute;
103  static constexpr int64_t kSecondsOfDay = 24 * kSecondsOfHour;
104 
105  private:
106  // The result of a right-shift of a signed negative number is
107  // implementation-dependent (UB. see
108  // https://en.cppreference.com/w/cpp/language/operator_arithmetic). So make
109  // sure the result is what we expected, if right shift not filled highest bit
110  // by the sign bit that the process will falls back to procedure which fill
111  // hightest bit by the sign bit value.
112  static int64_t shr(int64_t a, int b) {
113  int64_t one = 1;
114  return (-one >> 1 == -1 ? a >> b : (a + (a < 0)) / (one << b) - (a < 0));
115  }
116 };
117 
118 } // namespace time
119 } // namespace nebula