Class: PerfectTOML::LocalDateTime

Inherits:
LocalDateTimeBase show all
Defined in:
lib/perfect_toml.rb

Overview

Represents TOML's Local Date-Time

See https://toml.io/en/v1.0.0#local-date-time

Instance Method Summary collapse

Methods inherited from LocalDateTimeBase

#<=>, #==, #inspect, #pretty_print, #to_inline_toml

Constructor Details

#initialize(*args) ⇒ LocalDateTime

Returns a new instance of LocalDateTime.

Raises:



65
66
67
68
69
# File 'lib/perfect_toml.rb', line 65

def initialize(*args)
  @date = args[0].is_a?(LocalDate) ? args.shift : LocalDate.new(args.shift, args.shift, args.shift)
  @time = args[0].is_a?(LocalTime) ? args.shift : LocalTime.new(args.shift, args.shift, args.shift)
  raise ArgumentError, "wrong number of arguments" unless args.empty?
end

Instance Method Details

#dayObject



73
# File 'lib/perfect_toml.rb', line 73

def day = @date.day

#hourObject



74
# File 'lib/perfect_toml.rb', line 74

def hour = @time.hour

#minObject



75
# File 'lib/perfect_toml.rb', line 75

def min = @time.min

#monthObject



72
# File 'lib/perfect_toml.rb', line 72

def month = @date.month

#secObject



76
# File 'lib/perfect_toml.rb', line 76

def sec = @time.sec

#to_sObject

Returns a string representation in RFC 3339 format

ldt = PerfectTOML::LocalDateTime.new(1970, 1, 1, 2, 3, 4)
ldt.to_s #=> 1970-01-01T02:03:04


95
96
97
# File 'lib/perfect_toml.rb', line 95

def to_s
  "#{ @date }T#{ @time }"
end

#to_time(zone = nil) ⇒ Object

Converts to a Time object with the local timezone.

ldt = PerfectTOML::LocalDateTime.new(1970, 1, 1, 2, 3, 4)
ldt.to_time #=> 1970-01-01 02:03:04 +0900

You can specify timezone by passing an argument.

ldt.to_time("UTC")    #=> 1970-01-01 02:03:04 UTC
ldt.to_time("+00:00") #=> 1970-01-01 02:03:04 +0000


87
88
89
# File 'lib/perfect_toml.rb', line 87

def to_time(zone = nil)
  Time.new(@date.year, @date.month, @date.day, @time.hour, @time.min, @time.sec, zone)
end

#yearObject



71
# File 'lib/perfect_toml.rb', line 71

def year = @date.year