Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 15 additions & 10 deletions include/uid2/timestamp.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <chrono>
#include <cstdint>
#include <ostream>

Expand All @@ -10,27 +11,31 @@ class Timestamp {

static Timestamp Now();
static Timestamp FromEpochSecond(std::int64_t epochSeconds) { return FromEpochMilli(epochSeconds * 1000); }
static Timestamp FromEpochMilli(std::int64_t epochMilli) { return Timestamp(epochMilli); }
static Timestamp FromEpochMilli(std::int64_t epochMilli)
{
return Timestamp(std::chrono::duration_cast<Clock::duration>(std::chrono::milliseconds(epochMilli)));
}

std::int64_t GetEpochSecond() const { return epochMilli_ / 1000; }
std::int64_t GetEpochMilli() const { return epochMilli_; }
bool IsZero() const { return epochMilli_ == 0; }
std::int64_t GetEpochSecond() const { return std::chrono::duration_cast<std::chrono::seconds>(epoch_).count(); }
std::int64_t GetEpochMilli() const { return std::chrono::duration_cast<std::chrono::milliseconds>(epoch_).count(); }
bool IsZero() const { return epoch_ == std::chrono::system_clock::duration::zero(); }

Timestamp AddSeconds(std::int64_t seconds) const { return Timestamp(epochMilli_ + seconds * 1000); }
Timestamp AddSeconds(std::int64_t seconds) const { return Timestamp(std::chrono::duration_cast<Clock::duration>(epoch_ + std::chrono::seconds(seconds))); }
Timestamp AddDays(int days) const { return AddSeconds(static_cast<std::int64_t>(days) * 24 * 60 * 60); }

bool operator==(Timestamp other) const { return epochMilli_ == other.epochMilli_; }
bool operator==(Timestamp other) const { return GetEpochMilli() == other.GetEpochMilli(); }
bool operator!=(Timestamp other) const { return !operator==(other); }
bool operator<(Timestamp other) const { return epochMilli_ < other.epochMilli_; }
bool operator<(Timestamp other) const { return GetEpochMilli() < other.GetEpochMilli(); }
bool operator<=(Timestamp other) const { return !other.operator<(*this); }
bool operator>(Timestamp other) const { return other.operator<(*this); }
bool operator>=(Timestamp other) const { return !operator<(other); }

private:
explicit Timestamp(std::int64_t epochMilli) : epochMilli_(epochMilli) {}
using Clock = std::chrono::system_clock;
explicit Timestamp(Clock::duration epoch) : epoch_(epoch) {}

std::int64_t epochMilli_ = 0;
std::chrono::system_clock::duration epoch_ = {};

inline friend std::ostream& operator<<(std::ostream& os, Timestamp ts) { return (os << ts.epochMilli_); }
friend std::ostream& operator<<(std::ostream& os, Timestamp ts) { return (os << ts.GetEpochMilli()); }
};
} // namespace uid2
1 change: 1 addition & 0 deletions lib/external/json11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstdint>
#include <cstdio>
#include <limits>

Expand Down
2 changes: 1 addition & 1 deletion lib/timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
namespace uid2 {
Timestamp Timestamp::Now()
{
return Timestamp(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
return Timestamp(Clock::now().time_since_epoch());
}
} // namespace uid2
2 changes: 1 addition & 1 deletion lib/uid2base64urlcoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class UID2Base64UrlCoder {
public:
static std::string Encode(const std::vector<uint8_t>& input) { return macaron::Base64::EncodeBase64URL(input); }

static void Decode(const std::string& input, std::vector<uint8_t>& out) { return macaron::Base64::DecodeBase64URL(input, out); }
static void Decode(const std::string& input, std::vector<uint8_t>& out) { macaron::Base64::DecodeBase64URL(input, out); }
};
} // namespace uid2