Module: AEMO::Time

Defined in:
lib/aemo/time.rb

Overview

AEMO::Time

provides time helpers for AEMO services.

Constant Summary collapse

NEMTIMEZONE =
'Australia/Brisbane'
TIMESTAMP14 =
'%Y%m%d%H%M%S'
TIMESTAMP14_PATTERN =
/^\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}$/
TIMESTAMP12 =
'%Y%m%d%H%M'
TIMESTAMP12_PATTERN =
/^\d{4}\d{2}\d{2}\d{2}\d{2}$/
TIMESTAMP8 =
'%Y%m%d'
TIMESTAMP8_PATTERN =
/^\d{4}\d{2}\d{2}$/

Class Method Summary collapse

Class Method Details

.format_timestamp12(time) ⇒ String

Format a time to a timestamp 12.

Parameters:

Returns:

  • (String)


32
33
34
# File 'lib/aemo/time.rb', line 32

def format_timestamp12(time)
  time.in_time_zone(NEMTIMEZONE).strftime(TIMESTAMP12)
end

.format_timestamp14(time) ⇒ String

Format a time to a timestamp 14.

Parameters:

Returns:

  • (String)


24
25
26
# File 'lib/aemo/time.rb', line 24

def format_timestamp14(time)
  time.in_time_zone(NEMTIMEZONE).strftime(TIMESTAMP14)
end

.format_timestamp8(time) ⇒ String

Format a time to a timestamp 8.

Parameters:

Returns:

  • (String)


40
41
42
# File 'lib/aemo/time.rb', line 40

def format_timestamp8(time)
  time.in_time_zone(NEMTIMEZONE).strftime(TIMESTAMP8)
end

.parse_timestamp12(string) ⇒ Time

Parse a 12 character timestamp.

Parameters:

  • string (String)

Returns:

Raises:



59
60
61
62
63
# File 'lib/aemo/time.rb', line 59

def parse_timestamp12(string)
  raise AEMO::TimeError unless string.match(TIMESTAMP12_PATTERN)

  ::Time.find_zone(NEMTIMEZONE).strptime(string, TIMESTAMP12)
end

.parse_timestamp14(string) ⇒ Time

Parse a 14 character timestamp.

Parameters:

  • string (String)

Returns:

Raises:



49
50
51
52
53
# File 'lib/aemo/time.rb', line 49

def parse_timestamp14(string)
  raise AEMO::TimeError unless string.match(TIMESTAMP14_PATTERN)

  ::Time.find_zone(NEMTIMEZONE).strptime(string, TIMESTAMP14)
end

.parse_timestamp8(string) ⇒ Time

Parse an 8 character date.

Parameters:

  • string (String)

Returns:

Raises:



69
70
71
72
73
# File 'lib/aemo/time.rb', line 69

def parse_timestamp8(string)
  raise AEMO::TimeError unless string.match(TIMESTAMP8_PATTERN)

  ::Time.find_zone(NEMTIMEZONE).strptime(string, TIMESTAMP8)
end

.valid_timestamp12?(string) ⇒ Boolean

Check if a string is a valid timestamp 12.

Parameters:

  • string (String)

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'lib/aemo/time.rb', line 91

def valid_timestamp12?(string)
  parse_timestamp12(string)

  true
rescue AEMO::TimeError
  false
end

.valid_timestamp14?(string) ⇒ Boolean

Check if a string is a valid timestamp 14.

Parameters:

  • string (String)

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'lib/aemo/time.rb', line 79

def valid_timestamp14?(string)
  parse_timestamp14(string)

  true
rescue AEMO::TimeError
  false
end

.valid_timestamp8?(string) ⇒ Boolean

Check if a string is a valid timestamp 8.

Parameters:

  • string (String)

Returns:

  • (Boolean)


103
104
105
106
107
108
109
# File 'lib/aemo/time.rb', line 103

def valid_timestamp8?(string)
  parse_timestamp8(string)

  true
rescue AEMO::TimeError
  false
end