struct

BSON::DateTime

Inherits Comparable < Struct < Value < Object

BSON UTC datetime: milliseconds since the Unix epoch, as a signed 64-bit integer.

Crystal Time cannot store every BSON datetime (for example year 10000). This type keeps the full range. Use #to_time or #to_time? when you need Time.

Constants

MAX_TIME_MS = 253402300799999_i64
MIN_TIME_MS = -62135596800000_i64

Inclusive Time.unix_ms range that Crystal can store.

Constructors

new(milliseconds : Int64)
Source
new(time : Time)
Source

Instance methods

<=>(other : DateTime)

The comparison operator. Returns 0 if the two objects are equal, a negative number if this object is considered less than other, a positive number if this object is considered greater than other, or nil if the two objects are not comparable.

Subclasses define this method to provide class-specific ordering.

The comparison operator is usually used to sort values:

# Sort in a descending way:
[3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]

# Sort in an ascending way:
[3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]
Source
==(other : Time) : Bool
Source
in_time_range?

true when Crystal Time can store this value.

Source
milliseconds
Source
relaxed?

Relaxed Extended JSON can use an ISO-8601 string for years 1970 through 9999.

Source
to_canonical_extjson(builder : JSON::Builder)

Serialize to a canonical extended json representation.

NOTE: see https://github.com/mongodb/specifications/blob/master/source/extended-json.rst

Source
to_json(builder : JSON::Builder)
Source
to_relaxed_extjson(builder : JSON::Builder)

Serialize to a relaxed extended json representation.

NOTE: see https://github.com/mongodb/specifications/blob/master/source/extended-json.rst

Source
to_time

Convert to Time. Raises when the value is outside the Crystal range.

Source
to_time?

Convert to Time, or nil when the value is outside the Crystal range.

Source
to_unix_ms
Source