Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom time format implementations #5123

Merged
Prev Previous commit
fixup! Add custom format implementations for ISO 8601, RFC 3339, RFC …
…2822
  • Loading branch information
straight-shoota authored and bcardiff committed Jun 9, 2018
commit fee9ec4da905e4d51fb03c286297d73d6a1c63fe
10 changes: 8 additions & 2 deletions spec/std/json/serialization_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,14 @@ describe "JSON serialization" do
{"foo" => {"bar" => 1}}.to_pretty_json(indent: " ").should eq(%({\n "foo": {\n "bar": 1\n }\n}))
end

it "does for time" do
Time.utc(2016, 11, 16, 12, 55, 48).to_json.should eq(%("2016-11-16T12:55:48Z"))
describe "Time" do
it "#to_json" do
Time.utc(2016, 11, 16, 12, 55, 48).to_json.should eq(%("2016-11-16T12:55:48Z"))
end

it "omit sub-second precision" do
Time.utc(2016, 11, 16, 12, 55, 48, nanosecond: 123456789).to_json.should eq(%("2016-11-16T12:55:48Z"))
end
end
end
end
2 changes: 1 addition & 1 deletion src/json/to_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ struct Time
#
# See `#from_json` for reference.
def to_json(json : JSON::Builder)
json.string(Time::Format::RFC_3339.format(self))
json.string(Time::Format::RFC_3339.format(self, fraction_digits: 0))
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/time.cr
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ struct Time
# This is similar to `.parse_rfc3339` but RFC 3339 defines a more strict format.
# In ISO 8601 for examples, field delimiters (`-`, `:`) are optional.
#
# Use `#to_rfc3339` to format a `Time` according to ISO 8601.
# Use `#to_rfc3339` to format a `Time` according to .
def self.parse_iso8601(time : String)
Format::ISO_8601_DATE_TIME.parse(time)
end
Expand Down
12 changes: 6 additions & 6 deletions src/time/format/custom/rfc_3339.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ struct Time::Format
end

# Formats a `Time` into the given *io*.
def self.format(time : Time, io : IO)
def self.format(time : Time, io : IO, fraction_digits = nil)
formatter = Formatter.new(time, io)
formatter.rfc_3339
formatter.rfc_3339(fraction_digits: fraction_digits)
io
end

# Formats a `Time` into a `String`.
def self.format(time : Time)
def self.format(time : Time, fraction_digits = nil)
String.build do |io|
format(time, io)
format(time, io, fraction_digits: fraction_digits)
end
end
end

module Pattern
def rfc_3339
def rfc_3339(fraction_digits = nil)
year_month_day
char 'T', 't', ' '
twenty_four_hour_time_with_seconds
second_fraction?
second_fraction?(fraction_digits: fraction_digits)
time_zone_z_or_offset(force_colon: true)
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/time/format/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ struct Time::Format
nanoseconds
end

def second_fraction?
unless time.nanosecond == 0
def second_fraction?(fraction_digits = nil)
unless time.nanosecond == 0 || fraction_digits == 0
char '.'
second_fraction
end
Expand Down
2 changes: 1 addition & 1 deletion src/time/format/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ struct Time::Format
@nanosecond = (decimals * 10 ** (precision_shift + nanoseconds_shift)).to_i
end

def second_fraction?
def second_fraction?(fraction_digits = nil)
if current_char == '.'
next_char
nanoseconds
Expand Down