firebase::Timestamp

#include <timestamp.h>

A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time.

Summary

It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one. It is encoded assuming all minutes are 60 seconds long, i.e. leap seconds are "smeared" so that no leap second table is needed for interpretation. Range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.

See also: https://github.com/google/protobuf/blob/main/src/google/protobuf/timestamp.proto

Constructors and Destructors

Timestamp()
Creates a new timestamp representing the epoch (with seconds and nanoseconds set to 0).
Timestamp(int64_t seconds, int32_t nanoseconds)
Creates a new timestamp.
Timestamp(const Timestamp & other)
Copy constructor, Timestamp is trivially copyable.
Timestamp(Timestamp && other)
Move constructor, equivalent to copying.

Public functions

ToString() const
std::string
Returns a string representation of this Timestamp for logging/debugging purposes.
ToTimePoint() const
std::chrono::time_point< Clock, Duration >
Converts this Timestamp to a time_point.
nanoseconds() const
int32_t
The non-negative fractions of a second at nanosecond resolution.
operator=(const Timestamp & other)=default
Copy assignment operator, Timestamp is trivially copyable.
operator=(Timestamp && other)=default
Move assignment operator, equivalent to copying.
seconds() const
int64_t
The number of seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z.

Public static functions

FromTimePoint(std::chrono::time_point< std::chrono::system_clock > time_point)
Converts std::chrono::time_point to a Timestamp.
FromTimeT(time_t seconds_since_unix_epoch)
Converts time_t to a Timestamp.
Now()
Creates a new timestamp with the current date.

Friend classes

operator<<
friend std::ostream &
Outputs the string representation of this Timestamp to the given stream.

Public functions

Timestamp

 Timestamp()=default

Creates a new timestamp representing the epoch (with seconds and nanoseconds set to 0).

Timestamp

 Timestamp(
  int64_t seconds,
  int32_t nanoseconds
)

Creates a new timestamp.

Details
Parameters
seconds
The number of seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive; otherwise, assertion failure will be triggered.
nanoseconds
The non-negative fractions of a second at nanosecond resolution. Negative second values with fractions must still have non-negative nanoseconds values that count forward in time. Must be from 0 to 999,999,999 inclusive; otherwise, assertion failure will be triggered.

Timestamp

 Timestamp(
  const Timestamp & other
)=default

Copy constructor, Timestamp is trivially copyable.

Timestamp

 Timestamp(
  Timestamp && other
)=default

Move constructor, equivalent to copying.

ToString

std::string ToString() const 

Returns a string representation of this Timestamp for logging/debugging purposes.

ToTimePoint

std::chrono::time_point< Clock, Duration > ToTimePoint() const 

Converts this Timestamp to a time_point.

Important: if overflow would occur, the returned value will be the maximum or minimum value that Duration can hold. Note in particular that long long is insufficient to hold the full range of Timestamp values with nanosecond precision (which is why Duration defaults to microseconds).

nanoseconds

int32_t nanoseconds() const 

The non-negative fractions of a second at nanosecond resolution.

Negative second values with fractions still have non-negative nanoseconds values that count forward in time.

operator=

Timestamp & operator=(
  const Timestamp & other
)=default

Copy assignment operator, Timestamp is trivially copyable.

operator=

Timestamp & operator=(
  Timestamp && other
)=default

Move assignment operator, equivalent to copying.

seconds

int64_t seconds() const 

The number of seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z.

Public static functions

FromTimePoint

Timestamp FromTimePoint(
  std::chrono::time_point< std::chrono::system_clock > time_point
)

Converts std::chrono::time_point to a Timestamp.

Details
Parameters
time_point
The time point with system clock's epoch, which is presumed to be Unix epoch 1970-01-01T00:00:00Z. Can be negative to represent dates before the epoch. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive; otherwise, assertion failure will be triggered. Note that while the epoch of std::chrono::system_clock is unspecified, it's usually Unix epoch. If this assumption is broken, this constructor will produce incorrect results.

FromTimeT

Timestamp FromTimeT(
  time_t seconds_since_unix_epoch
)

Converts time_t to a Timestamp.

Details
Parameters
seconds_since_unix_epoch
The number of seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Can be negative to represent dates before the epoch. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive; otherwise, assertion failure will be triggered. Note that while the epoch of time_t is unspecified, it's usually Unix epoch. If this assumption is broken, this function will produce incorrect results.
Returns
a new timestamp with the given number of seconds and zero nanoseconds.

Now

Timestamp Now()

Creates a new timestamp with the current date.

The precision is up to nanoseconds, depending on the system clock.

Details
Returns
a new timestamp representing the current date.

Friend classes

operator<<

friend std::ostream & operator<<(std::ostream &out, const Timestamp &timestamp)

Outputs the string representation of this Timestamp to the given stream.

See also:ToString() for comments on the representation format.