Class: LibTAD::TADTime::TADDateTime

Inherits:
Object
  • Object
show all
Defined in:
lib/types/time/datetime.rb

Overview

Date and time, split up into components.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dayInteger (readonly)

The day component of the timestamp.

Returns:

  • (Integer)


17
18
19
# File 'lib/types/time/datetime.rb', line 17

def day
  @day
end

#hourInteger (readonly)

The hour component of the timestamp.

Returns:

  • (Integer)


21
22
23
# File 'lib/types/time/datetime.rb', line 21

def hour
  @hour
end

#minuteInteger (readonly)

The minute component of the timestamp.

Returns:

  • (Integer)


25
26
27
# File 'lib/types/time/datetime.rb', line 25

def minute
  @minute
end

#monthInteger (readonly)

The month component of the timestamp.

Returns:

  • (Integer)


13
14
15
# File 'lib/types/time/datetime.rb', line 13

def month
  @month
end

#secondInteger (readonly)

The second component of the timestamp.

Returns:

  • (Integer)


29
30
31
# File 'lib/types/time/datetime.rb', line 29

def second
  @second
end

#yearInteger (readonly)

The year component of the timestamp.

Returns:

  • (Integer)


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.

Returns:

  • (Boolean)


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_iso8601String

Helper function for formatting as ISO 8601.

Returns:

  • (String)


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.

Returns:

  • (::DateTime)


88
89
90
# File 'lib/types/time/datetime.rb', line 88

def to_std!
  ::Time.new(@year, @month, @day, @hour, @minute, @second)
end