Class: LibTAD::TADTime::TADDateTime
- Inherits:
-
Object
- Object
- LibTAD::TADTime::TADDateTime
- Defined in:
- lib/types/time/datetime.rb
Overview
Date and time, split up into components.
Instance Attribute Summary collapse
-
#day ⇒ Integer
readonly
The day component of the timestamp.
-
#hour ⇒ Integer
readonly
The hour component of the timestamp.
-
#minute ⇒ Integer
readonly
The minute component of the timestamp.
-
#month ⇒ Integer
readonly
The month component of the timestamp.
-
#second ⇒ Integer
readonly
The second component of the timestamp.
-
#year ⇒ Integer
readonly
The year component of the timestamp.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compare equality to another instance.
-
#from_json(hash) ⇒ ::LibTAD::TADTime::TADDateTime
Helper function for initializing from json.
-
#initialize(year: nil, month: nil, day: nil, hour: nil, minute: nil, second: nil) ⇒ TADDateTime
constructor
A new instance of TADDateTime.
-
#now ⇒ ::LibTAD::TADTime::TADDateTime
Get the current time.
-
#to_iso8601 ⇒ String
Helper function for formatting as ISO 8601.
-
#to_std! ⇒ ::DateTime
Try converting to Time from the standard library.
Constructor Details
#initialize(year: nil, month: nil, day: nil, hour: nil, minute: nil, second: nil) ⇒ TADDateTime
Returns a new instance of TADDateTime.
31 32 33 34 35 36 37 38 |
# File 'lib/types/time/datetime.rb', line 31 def initialize(year: nil, month: nil, day: nil, hour: nil, minute: nil, second: nil) @year = year @month = month @day = day @hour = hour @minute = minute @second = second end |
Instance Attribute Details
#day ⇒ Integer (readonly)
The day component of the timestamp.
17 18 19 |
# File 'lib/types/time/datetime.rb', line 17 def day @day end |
#hour ⇒ Integer (readonly)
The hour component of the timestamp.
21 22 23 |
# File 'lib/types/time/datetime.rb', line 21 def hour @hour end |
#minute ⇒ Integer (readonly)
The minute component of the timestamp.
25 26 27 |
# File 'lib/types/time/datetime.rb', line 25 def minute @minute end |
#month ⇒ Integer (readonly)
The month component of the timestamp.
13 14 15 |
# File 'lib/types/time/datetime.rb', line 13 def month @month end |
#second ⇒ Integer (readonly)
The second component of the timestamp.
29 30 31 |
# File 'lib/types/time/datetime.rb', line 29 def second @second end |
#year ⇒ Integer (readonly)
The year component of the timestamp.
9 10 11 |
# File 'lib/types/time/datetime.rb', line 9 def year @year end |
Instance Method Details
#==(other) ⇒ Boolean
Compare equality to another instance.
42 43 44 45 46 47 48 |
# File 'lib/types/time/datetime.rb', line 42 def ==(other) other.year == @year && other.month == @month && other.day == @day && other.minute == @minute && other.second == @second end |
#from_json(hash) ⇒ ::LibTAD::TADTime::TADDateTime
Helper function for initializing from json.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/types/time/datetime.rb', line 52 def from_json(hash) @year = hash&.fetch('year', nil) @month = hash&.fetch('month', nil) @day = hash&.fetch('day', nil) @hour = hash&.fetch('hour', nil) @minute = hash&.fetch('minute', nil) @second = hash&.fetch('second', nil) self end |
#now ⇒ ::LibTAD::TADTime::TADDateTime
Get the current time.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/types/time/datetime.rb', line 74 def now dt = ::Time.now @year = dt.year @month = dt.month @day = dt.day @hour = dt.hour @minute = dt.min @second = dt.sec self end |
#to_iso8601 ⇒ String
Helper function for formatting as ISO 8601.
65 66 67 68 69 70 |
# File 'lib/types/time/datetime.rb', line 65 def to_iso8601 year = @year.to_s.rjust(4, '0') month = @month.to_s.rjust(2, '0') day = @day.to_s.rjust(2, '0') "#{year}-#{month}-#{day}" end |
#to_std! ⇒ ::DateTime
Try converting to Time from the standard library.
88 89 90 |
# File 'lib/types/time/datetime.rb', line 88 def to_std! ::Time.new(@year, @month, @day, @hour, @minute, @second) end |