Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Get current time with milliseconds precision. #523

Merged
52 changes: 28 additions & 24 deletions src/common/time/TimeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "common/datatypes/Map.h"
#include "common/fs/FileUtils.h"
#include "common/time/TimezoneInfo.h"
#include "common/time/WallClock.h"

DECLARE_string(timezone_name);

Expand Down Expand Up @@ -112,21 +113,17 @@ class TimeUtils {
}

static StatusOr<DateTime> localDateTime() {
DateTime dt;
time_t unixTime = std::time(NULL);
if (unixTime == -1) {
return Status::Error("Get unix time failed: %s.", std::strerror(errno));
}
return unixSecondsToDateTime(unixTime - getGlobalTimezone().utcOffsetSecs());
auto time = unixTime();
auto dt = unixSecondsToDateTime(time.seconds - getGlobalTimezone().utcOffsetSecs());
dt.microsec = time.milliseconds * 1000;
return dt;
Shylock-Hg marked this conversation as resolved.
Show resolved Hide resolved
}

static StatusOr<DateTime> utcDateTime() {
DateTime dt;
time_t unixTime = std::time(NULL);
if (unixTime == -1) {
return Status::Error("Get unix time failed: %s.", std::strerror(errno));
}
return unixSecondsToDateTime(unixTime);
auto time = unixTime();
auto dt = unixSecondsToDateTime(time.seconds);
dt.microsec = time.milliseconds * 1000;
return dt;
}

static StatusOr<Date> dateFromMap(const Map &m);
Expand Down Expand Up @@ -243,21 +240,17 @@ class TimeUtils {
}

static StatusOr<Time> localTime() {
Time dt;
time_t unixTime = std::time(NULL);
if (unixTime == -1) {
return Status::Error("Get unix time failed: %s.", std::strerror(errno));
}
return unixSecondsToTime(unixTime - getGlobalTimezone().utcOffsetSecs());
auto time = unixTime();
auto t = unixSecondsToTime(time.seconds - getGlobalTimezone().utcOffsetSecs());
t.microsec = time.milliseconds * 1000;
return t;
}

static StatusOr<Time> utcTime() {
Time dt;
time_t unixTime = std::time(NULL);
if (unixTime == -1) {
return Status::Error("Get unix time failed: %s.", std::strerror(errno));
}
return unixSecondsToTime(unixTime);
auto time = unixTime();
auto t = unixSecondsToTime(time.seconds);
t.microsec = time.milliseconds * 1000;
return t;
}

static Timezone &getGlobalTimezone() {
Expand All @@ -278,6 +271,17 @@ class TimeUtils {

static Timezone globalTimezone;

struct UnixTime {
int64_t seconds{0};
int64_t milliseconds{0};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is microseconds

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Milliseconds in fact.

};

// <seconds, milliseconds>
static UnixTime unixTime() {
auto ms = WallClock::fastNowInMilliSec();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use fastNowInMicroSec directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cypher only provide the milliseconds precision

return UnixTime{ms / 1000, ms % 1000};
}

// The result of a right-shift of a signed negative number is implementation-dependent
// (UB. see https://en.cppreference.com/w/cpp/language/operator_arithmetic).
// So make sure the result is what we expected, if right shift not filled highest bit by the
Expand Down