Skip to content

Commit

Permalink
Add Timestamp to/from milliseconds conversion (#30)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #30

Differential Revision: D30270922

Pulled By: mbasmanova

fbshipit-source-id: cd04038078cf664e76c77e7349b5c4de18abf729
  • Loading branch information
mbasmanova authored and facebook-github-bot committed Aug 12, 2021
1 parent f148b28 commit 95dce60
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 2 additions & 8 deletions velox/serializers/PrestoSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void readValues<bool>(

Timestamp readTimestamp(ByteStream* source) {
int64_t millis = source->read<int64_t>();
return Timestamp(millis / 1000, (millis % 1000) * 1'000'000);
return Timestamp::fromMillis(millis);
}

template <>
Expand Down Expand Up @@ -721,19 +721,13 @@ inline void VectorStream::append(folly::Range<const StringView*> values) {
}
}

int64_t timestampToPrestoMillis(Timestamp timestamp) {
return timestamp.getSeconds() * 1000 + timestamp.getNanos() / 1000000;
}

template <>
void VectorStream::append(folly::Range<const Timestamp*> values) {
for (auto& value : values) {
appendOne(timestampToPrestoMillis(value));
appendOne(value.toMillis());
}
}

int64_t timestampToPrestoMillis(Timestamp timestamp);

template <>
void VectorStream::append(folly::Range<const bool*> values) {
// A bool constant is serialized via this. Accessing consecutive
Expand Down
8 changes: 8 additions & 0 deletions velox/type/Timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ struct Timestamp {
return nanos_;
}

int64_t toMillis() const {
return seconds_ * 1'000 + nanos_ / 1'000'000;
}

static Timestamp fromMillis(int64_t millis) {
return Timestamp(millis / 1'000, (millis % 1'000) * 1'000'000);
}

// Converts the unix epoch represented by this object (assumes it's GMT)
// to the given timezone.
void toTimezone(const date::time_zone& zone);
Expand Down

0 comments on commit 95dce60

Please sign in to comment.