module Time::EpochConverter

Overview

Converter to be used with JSON::Serializable and YAML::Serializable to serialize a Time instance as the number of seconds since the unix epoch. See Time#to_unix.

require "json"

class Person
  include JSON::Serializable

  @[JSON::Field(converter: Time::EpochConverter)]
  property birth_date : Time
end

person = Person.from_json(%({"birth_date": 1459859781}))
person.birth_date # => 2016-04-05 12:36:21 UTC
person.to_json    # => %({"birth_date":1459859781})

Defined in:

json/from_json.cr
json/to_json.cr
yaml/from_yaml.cr
yaml/to_yaml.cr

Class Method Summary

Class Method Detail

def self.from_json(value : JSON::PullParser) : TimeSource

def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : TimeSource

def self.to_json(value : Time, json : JSON::Builder) : NilSource

def self.to_yaml(value : Time, yaml : YAML::Nodes::Builder) : NilSource

© 2012–2021 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.2.1/Time/EpochConverter.html