Class: Temporal::Now

Inherits:
Object
  • Object
show all
Defined in:
lib/temporal/now.rb

Constant Summary collapse

ISO_TZ_OFFSET =

12:34 -12:34 1234 -1234

/[+-]\d\d:?\d\d/
ENDLINE_OFFSET =

12:34$ -12:34$ 1234$ -1234$

/#{ISO_TZ_OFFSET}$/o
BRACKETED_OFFSET =
+12:34
-12:34
+1234
-1234
/\[(#{ISO_TZ_OFFSET})\]/o
IANA_TZ =
ISO
/\[(\w+)\]/

Class Method Summary collapse

Class Method Details

.instantObject



9
10
11
# File 'lib/temporal/now.rb', line 9

def self.instant
  Temporal::Instant.new(now.to_f)
end

.now(tz = nil) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/temporal/now.rb', line 32

def self.now(tz = nil)
  if tz
    Time.now(in: validate_timezone(tz))
  else
    Time.now
  end
end

.plain_date_iso(tz = nil) ⇒ Object



13
14
15
16
# File 'lib/temporal/now.rb', line 13

def self.plain_date_iso(tz = nil)
  t = now(tz)
  Temporal::PlainDate.new(t.year, t.month, t.day)
end

.plain_date_time_iso(tz = nil) ⇒ Object



18
19
20
21
# File 'lib/temporal/now.rb', line 18

def self.plain_date_time_iso(tz = nil)
  t = now(tz)
  Temporal::PlainDateTime.new(t.year, t.month, t.day, t.hour, t.min, t.sec)
end

.plain_time_iso(tz = nil) ⇒ Object



23
24
25
26
# File 'lib/temporal/now.rb', line 23

def self.plain_time_iso(tz = nil)
  t = now(tz)
  Temporal::PlainTime.new(t.hour, t.min, t.sec)
end

.time_zone_idObject



7
# File 'lib/temporal/now.rb', line 7

def self.time_zone_id = "UTC"

.validate_timezone(value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/temporal/now.rb', line 45

def self.validate_timezone(value)
  value = value.downcase if value.respond_to? :downcase

  case value
  in "a".."i" | "k".."z"
    value.upcase
  in "utc" | "UTC" | /z$/
    "UTC"
  in IANA_TZ
    value[IANA_TZ, 1]
  in BRACKETED_OFFSET
    value[BRACKETED_OFFSET, 1]
  in ENDLINE_OFFSET
    value[ENDLINE_OFFSET]
  else
    raise RangeError, "Invalid value `#{value}` for timezone"
  end
end

.zoned_date_time_iso(tz = "UTC") ⇒ Object



28
29
30
# File 'lib/temporal/now.rb', line 28

def self.zoned_date_time_iso(tz = "UTC")
  Temporal::ZonedDateTime.new(now(tz).to_f, tz: validate_timezone(tz))
end