Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add duration. #3338

Merged
merged 29 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
719ecf1
Add duration.
Shylock-Hg Nov 22, 2021
0d0db53
Merge branch 'master' into feature/duration
Shylock-Hg Nov 22, 2021
7384e90
Add datetime/duration computation support.
Shylock-Hg Nov 24, 2021
33b84b1
Merge branch 'feature/duration' of github.com:Shylock-Hg/nebula into …
Shylock-Hg Nov 24, 2021
495eb04
Merge branch 'master' into feature/duration
Shylock-Hg Nov 24, 2021
0e8158b
Remove debug code.
Shylock-Hg Nov 24, 2021
f19adcc
Merge branch 'master' into feature/duration
Shylock-Hg Nov 24, 2021
47d94b2
Merge branch 'master' into feature/duration
Shylock-Hg Nov 30, 2021
769afb1
Add operator override.
Shylock-Hg Dec 1, 2021
0729f45
Merge branch 'master' into feature/duration
Shylock-Hg Dec 1, 2021
5b3c6a4
Support syntax.
Shylock-Hg Dec 2, 2021
2b353f0
Merge branch 'feature/duration' of github.com:Shylock-Hg/nebula into …
Shylock-Hg Dec 2, 2021
438e0a8
Merge branch 'master' into feature/duration
Shylock-Hg Dec 2, 2021
a415d76
Fix the compare behavior.
Shylock-Hg Dec 2, 2021
1f6428c
Merge branch 'master' into feature/duration
Shylock-Hg Dec 2, 2021
de558c7
Store property data in disk.
Shylock-Hg Dec 3, 2021
362f0ae
Merge branch 'feature/duration' of github.com:Shylock-Hg/nebula into …
Shylock-Hg Dec 3, 2021
0339144
Merge branch 'master' into feature/duration
Shylock-Hg Dec 3, 2021
beca931
Merge branch 'master' into feature/duration
Shylock-Hg Dec 6, 2021
2d23093
Merge branch 'master' into feature/duration
Shylock-Hg Dec 8, 2021
3e8bfe9
Merge branch 'master' into feature/duration
Shylock-Hg Dec 8, 2021
f454f9c
Merge branch 'master' into feature/duration
Shylock-Hg Dec 8, 2021
525aa8f
Merge branch 'master' into feature/duration
Shylock-Hg Dec 9, 2021
db6a4f1
Merge branch 'master' into feature/duration
Shylock-Hg Dec 10, 2021
cbeea71
Merge branch 'master' of https://github.com/vesoft-inc/nebula into fe…
Shylock-Hg Dec 15, 2021
729de56
Merge branch 'master' into feature/duration
Shylock-Hg Dec 15, 2021
7938ca1
Merge branch 'master' into feature/duration
Shylock-Hg Dec 15, 2021
e8a94bf
Merge branch 'master' into feature/duration
yixinglu Dec 18, 2021
f179fb4
Merge branch 'master' into feature/duration
Sophie-Xie Dec 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/datatypes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ nebula_add_library(
List.cpp
Set.cpp
Geography.cpp
Duration.cpp
)

nebula_add_subdirectory(test)
2 changes: 2 additions & 0 deletions src/common/datatypes/CommonCpp2Ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct Point;
struct LineString;
struct Polygon;
struct Geography;
struct Duration;
} // namespace nebula

namespace apache::thrift {
Expand All @@ -52,6 +53,7 @@ SPECIALIZE_CPP2OPS(nebula::Point);
SPECIALIZE_CPP2OPS(nebula::LineString);
SPECIALIZE_CPP2OPS(nebula::Polygon);
SPECIALIZE_CPP2OPS(nebula::Geography);
SPECIALIZE_CPP2OPS(nebula::Duration);

} // namespace apache::thrift

Expand Down
213 changes: 213 additions & 0 deletions src/common/datatypes/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ namespace nebula {
const int64_t kDaysSoFar[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
const int64_t kLeapDaysSoFar[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};

int8_t dayOfMonth(int16_t year, int8_t month) {
return isLeapYear(year) ? kLeapDaysSoFar[month] - kLeapDaysSoFar[month - 1]
: kDaysSoFar[month] - kDaysSoFar[month - 1];
}

Date::Date(uint64_t days) { fromInt(days); }

int64_t Date::toInt() const {
Expand Down Expand Up @@ -92,16 +97,224 @@ Date Date::operator-(int64_t days) const {
return Date(daysSince - days);
}

void Date::addDuration(const Duration& duration) {
Shylock-Hg marked this conversation as resolved.
Show resolved Hide resolved
int64_t tmp{0}, carry{0};
tmp = month + duration.months;
if (std::abs(tmp) > 12) {
carry = tmp / 12;
month = tmp % 12;
} else {
month = tmp;
}
if (month <= 0) {
carry--;
month += 12;
}
year += carry;

tmp = day + duration.days();
if (tmp > 0) {
int8_t dom = dayOfMonth(year, month);
while (tmp > dom) {
tmp -= dom;
month += 1;
if (month > 12) {
year += 1;
month = 1;
}
dom = dayOfMonth(year, month);
}
} else {
int8_t dom = (month == 1 ? dayOfMonth(year - 1, 12) : dayOfMonth(year, month - 1));
while (tmp <= 0) {
tmp += dom;
month -= 1;
if (month <= 0) {
year--;
month += 12;
}
dom = (month == 1 ? dayOfMonth(year - 1, 12) : dayOfMonth(year, month - 1));
}
}
day = tmp;
}

void Date::subDuration(const Duration& duration) { return addDuration(-duration); }

std::string Date::toString() const {
// It's in current timezone already
return folly::stringPrintf("%d-%02d-%02d", year, month, day);
}

void Time::addDuration(const Duration& duration) {
int64_t tmp{0}, carry{0};
tmp = microsec + duration.microsecondsInSecond();
if (std::abs(tmp) >= 1000000) {
carry = tmp / 1000000;
microsec = tmp % 1000000;
} else {
microsec = tmp;
}
if (microsec < 0) {
carry--;
microsec += 1000000;
}
tmp = sec + duration.seconds + carry;
carry = 0;
if (std::abs(tmp) >= 60) {
carry = tmp / 60;
sec = tmp % 60;
} else {
sec = tmp;
}
if (sec < 0) {
carry--;
sec += 60;
}
tmp = minute + carry;
carry = 0;
if (std::abs(tmp) >= 60) {
carry = tmp / 60;
minute = tmp % 60;
} else {
minute = tmp;
}
if (minute < 0) {
carry--;
minute += 60;
}
tmp = hour + carry;
carry = 0;
if (std::abs(tmp) >= 24) {
carry = tmp / 24;
hour = tmp % 24;
} else {
hour = tmp;
}
if (hour < 0) {
carry--;
hour += 24;
}
}

void Time::subDuration(const Duration& duration) { addDuration(-duration); }
Shylock-Hg marked this conversation as resolved.
Show resolved Hide resolved

std::string Time::toString() const {
// It's in current timezone already
return folly::stringPrintf("%02d:%02d:%02d.%06d", hour, minute, sec, microsec);
}

void DateTime::addDuration(const Duration& duration) {
// The origin fields of DateTime is unsigned, but there will be some negative intermediate results
// so I define some variable(field, tYear, tMonth) for this.
int64_t tmp{0}, carry{0}, field{0};
tmp = month + duration.months;
if (std::abs(tmp) > 12) {
carry = tmp / 12;
/*month*/ field = tmp % 12;
} else {
/*month*/ field = tmp;
}
if (/*month*/ field <= 0) {
carry--;
/*month*/ field += 12;
}
year += carry;
carry = 0;
month = field;
field = 0;

tmp = microsec + duration.microsecondsInSecond();
if (std::abs(tmp) >= 1000000) {
carry = tmp / 1000000;
/*microsec*/ field = tmp % 1000000;
} else {
/*microsec*/ field = tmp;
}
if (/*microsec*/ field < 0) {
carry--;
/*microsec*/ field += 1000000;
}
microsec = field;
field = 0;
tmp = sec + duration.seconds + carry;
carry = 0;
if (std::abs(tmp) >= 60) {
carry = tmp / 60;
/*sec*/ field = tmp % 60;
} else {
/*sec*/ field = tmp;
}
if (/*sec*/ field < 0) {
carry--;
/*sec*/ field += 60;
}
sec = field;
field = 0;
tmp = minute + carry;
carry = 0;
if (std::abs(tmp) >= 60) {
carry = tmp / 60;
/*minute*/ field = tmp % 60;
} else {
/*minute*/ field = tmp;
}
if (/*minute*/ field < 0) {
carry--;
/*minute*/ field += 60;
}
minute = field;
field = 0;
tmp = hour + carry;
carry = 0;
if (std::abs(tmp) >= 24) {
carry = tmp / 24;
/*hour*/ field = tmp % 24;
} else {
/*hour*/ field = tmp;
}
if (/*hour*/ field < 0) {
carry--;
/*hour*/ field += 24;
}
hour = field;
field = 0;

tmp = day + carry;
carry = 0;
if (tmp > 0) {
int8_t dom = dayOfMonth(year, month);
while (tmp > dom) {
tmp -= dom;
month += 1;
if (month > 12) {
year += 1;
month = 1;
}
dom = dayOfMonth(year, month);
}
} else {
int8_t dom = (month == 1 ? dayOfMonth(year - 1, 12) : dayOfMonth(year, month - 1));
int64_t tMonth = month;
int64_t tYear = year;
while (tmp <= 0) {
tmp += dom;
/*month*/ tMonth -= 1;
if (/*month*/ tMonth <= 0) {
/*year*/ tYear--;
/*month*/ tMonth += 12;
}
dom = (/*month*/ tMonth == 1 ? dayOfMonth(/*year*/ tYear - 1, 12)
: dayOfMonth(/*year*/ tYear, /*month*/ tMonth - 1));
}
month = tMonth;
year = tYear;
}
day = tmp;
}

void DateTime::subDuration(const Duration& duration) { return addDuration(-duration); }

std::string DateTime::toString() const {
// It's in current timezone already
return folly::stringPrintf(
Expand Down
62 changes: 62 additions & 0 deletions src/common/datatypes/Date.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <string>

#include "common/datatypes/Duration.h"

namespace nebula {

// In nebula only store UTC time, and the interpretation of time value based on
Expand All @@ -18,6 +20,21 @@ namespace nebula {
extern const int64_t kDaysSoFar[];
extern const int64_t kLeapDaysSoFar[];

// https://en.wikipedia.org/wiki/Leap_year#Leap_day
static inline bool isLeapYear(int16_t year) {
if (year % 4 != 0) {
return false;
} else if (year % 100 != 0) {
return true;
} else if (year % 400 != 0) {
return false;
} else {
return true;
}
}

int8_t dayOfMonth(int16_t year, int8_t month);

struct Date {
int16_t year; // Any integer
int8_t month; // 1 - 12
Expand Down Expand Up @@ -62,6 +79,9 @@ struct Date {
Date operator+(int64_t days) const;
Date operator-(int64_t days) const;

void addDuration(const Duration& duration);
void subDuration(const Duration& duration);

std::string toString() const;
folly::dynamic toJson() const { return toString(); }

Expand All @@ -71,6 +91,18 @@ struct Date {
void fromInt(int64_t days);
};

inline Date operator+(const Date& l, const Duration& r) {
Date d = l;
d.addDuration(r);
return d;
}

inline Date operator-(const Date& l, const Duration& r) {
Date d = l;
d.subDuration(r);
return d;
}

inline std::ostream& operator<<(std::ostream& os, const Date& d) {
os << d.toString();
return os;
Expand Down Expand Up @@ -114,6 +146,9 @@ struct Time {
return false;
}

void addDuration(const Duration& duration);
void subDuration(const Duration& duration);

std::string toString() const;
// 'Z' representing UTC timezone
folly::dynamic toJson() const { return toString() + "Z"; }
Expand All @@ -124,6 +159,18 @@ inline std::ostream& operator<<(std::ostream& os, const Time& d) {
return os;
}

inline Time operator+(const Time& l, const Duration& r) {
Time t = l;
t.addDuration(r);
return t;
}

inline Time operator-(const Time& l, const Duration& r) {
Time t = l;
t.subDuration(r);
return t;
}

struct DateTime {
#if defined(__GNUC__)
#pragma GCC diagnostic push
Expand Down Expand Up @@ -206,6 +253,9 @@ struct DateTime {
return false;
}

void addDuration(const Duration& duration);
void subDuration(const Duration& duration);

std::string toString() const;
// 'Z' representing UTC timezone
folly::dynamic toJson() const { return toString() + "Z"; }
Expand All @@ -216,6 +266,18 @@ inline std::ostream& operator<<(std::ostream& os, const DateTime& d) {
return os;
}

inline DateTime operator+(const DateTime& l, const Duration& r) {
DateTime dt = l;
dt.addDuration(r);
return dt;
}

inline DateTime operator-(const DateTime& l, const Duration& r) {
DateTime dt = l;
dt.subDuration(r);
return dt;
}

} // namespace nebula

namespace std {
Expand Down
15 changes: 15 additions & 0 deletions src/common/datatypes/Duration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#include "common/datatypes/Duration.h"

#include <folly/String.h>
#include <folly/hash/Hash.h>

#include <sstream>

namespace nebula {} // namespace nebula

namespace std {} // namespace std
Loading