Skip to content

Commit 3854661

Browse files
committed
Create TimeHelper module
1 parent ef8c027 commit 3854661

4 files changed

Lines changed: 68 additions & 20 deletions

File tree

gems/smithy-schema/lib/smithy-schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative 'smithy-schema/shapes'
44
require_relative 'smithy-schema/structure'
55
require_relative 'smithy-schema/document'
6+
require_relative 'smithy-schema/time_helper'
67
require_relative 'smithy-schema/type_registry'
78
require_relative 'smithy-schema/union'
89

gems/smithy-schema/lib/smithy-schema/document_utils.rb

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def apply_structure(data, shape, type)
9696

9797
def apply_timestamp(data, shape)
9898
data = data.is_a?(Numeric) ? Time.at(data) : Time.parse(data)
99-
time(data, timestamp_format(shape))
99+
TimeHelper.time(data, timestamp_format(shape))
100100
end
101101

102102
def apply_union(data, shape, type)
@@ -147,8 +147,8 @@ def extract_structure(data, shape, opts)
147147
def extract_timestamp(data, shape, opts)
148148
return unless data.is_a?(Time)
149149

150-
trait = timestamp_format(shape) if opts[:use_timestamp_format]
151-
time(data, trait)
150+
trait = opts[:use_timestamp_format] ? timestamp_format(shape) : 'epoch-seconds'
151+
TimeHelper.time(data, trait)
152152
end
153153

154154
# rubocop:disable Metrics/AbcSize
@@ -208,23 +208,6 @@ def timestamp_format(shape)
208208
'epoch-seconds'
209209
end
210210
end
211-
212-
def time(data, trait = nil)
213-
if trait
214-
case trait
215-
when 'http-date'
216-
data.utc.iso8601
217-
when 'date-time'
218-
data.utc.httpdate
219-
when 'epoch-seconds'
220-
data.utc.to_i
221-
else
222-
raise "unhandled timestamp format `#{value}`"
223-
end
224-
else
225-
data.utc.to_i # default format
226-
end
227-
end
228211
end
229212
end
230213
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
module Smithy
4+
module Schema
5+
# A module that provides helper methods to convert Time objects
6+
# based on the given TimestampFormat trait.
7+
# @api private
8+
module TimeHelper
9+
class << self
10+
# @param [Time] time
11+
# @param [String] trait TimestampFormat trait value
12+
# @return [Object] The time as TimestampFormat trait format
13+
def time(time, trait)
14+
raise ArgumentError, 'expected Time as input' unless time.is_a?(Time)
15+
16+
case trait
17+
when 'http-date'
18+
time.utc.iso8601
19+
when 'date-time'
20+
time.utc.httpdate
21+
when 'epoch-seconds'
22+
time.utc.to_i
23+
else
24+
raise ArgumentError, "unhandled timestamp format `#{trait}`"
25+
end
26+
end
27+
end
28+
end
29+
end
30+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../spec_helper'
4+
require 'time'
5+
6+
module Smithy
7+
module Schema
8+
describe TimeHelper do
9+
describe '#time' do
10+
let(:time) { Time.new(2002, 10, 31) }
11+
12+
it 'returns as http-date format' do
13+
expect(subject.time(time, 'http-date')).to eq('2002-10-31T08:00:00Z')
14+
end
15+
16+
it 'returns as date-time format' do
17+
expect(subject.time(time, 'date-time')).to eq('Thu, 31 Oct 2002 08:00:00 GMT')
18+
end
19+
20+
it 'returns as epoch-seconds format' do
21+
expect(subject.time(time, 'epoch-seconds')).to eq(1_036_051_200)
22+
end
23+
24+
it 'raises when given time is invalid ' do
25+
expect { subject.time('time', 'http-date') }.to raise_error(ArgumentError)
26+
end
27+
28+
it 'raises when given timestamp trait is unhandled' do
29+
expect { subject.time(time, 'foo') }.to raise_error(ArgumentError)
30+
end
31+
end
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)