Module: J1::Utils::WinTZ

Extended by:
WinTZ
Included in:
WinTZ
Defined in:
lib/j1/utils/win_tz.rb

Instance Method Summary collapse

Instance Method Details

#calculate(timezone) ⇒ Object

Public: Calculate the Timezone for Windows when the config file has a defined

'timezone' key.

timezone - the IANA Time Zone specified in “_config.yml”

Returns a string that ultimately re-defines ENV in Windows



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/j1/utils/win_tz.rb', line 12

def calculate(timezone)
  External.require_with_graceful_fail("tzinfo")
  tz = TZInfo::Timezone.get(timezone)
  difference = Time.now.to_i - tz.now.to_i
  #
  # POSIX style definition reverses the offset sign.
  #   e.g. Eastern Standard Time (EST) that is 5Hrs. to the 'west' of Prime Meridian
  #   is denoted as:
  #     EST+5 (or) EST+05:00
  # Reference: http://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
  sign = difference < 0 ? "-" : "+"
  offset = sign == "-" ? "+" : "-" unless difference.zero?
  #
  # convert the difference (in seconds) to hours, as a rational number, and perform
  # a modulo operation on it.
  modulo = modulo_of(rational_hour(difference))
  #
  # Format the hour as a two-digit number.
  # Establish the minutes based on modulo expression.
  hh = format("%02d", absolute_hour(difference).ceil)
  mm = modulo.zero? ? "00" : "30"

  J1l.logger.debug "Timezone:", "#{timezone} #{offset}#{hh}:#{mm}"
  #
  # Note: The 3-letter-word below doesn't have a particular significance.
  "WTZ#{sign}#{hh}:#{mm}"
end