Module: EtOrbi

Defined in:
lib/et-orbi.rb

Defined Under Namespace

Classes: EoTime

Constant Summary collapse

VERSION =
'1.1.1'

Class Method Summary collapse

Class Method Details

._make_infoObject

For ‘make info`



200
201
202
203
204
# File 'lib/et-orbi.rb', line 200

def _make_info

  puts render_nozone_time(Time.now.to_f)
  puts platform_info
end

.determine_local_tzoneObject



615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/et-orbi.rb', line 615

def determine_local_tzone

  etz = ENV['TZ']

  tz = ::TZInfo::Timezone.get(etz) rescue nil
  return tz if tz

  if Time.respond_to?(:zone) && Time.zone.respond_to?(:tzinfo)
    tz = Time.zone.tzinfo
    return tz if tz
  end

  tz = ::TZInfo::Timezone.get(os_tz) rescue nil
  return tz if tz

  tzs = determine_local_tzones
  (etz && tzs.find { |z| z.name == etz }) || tzs.first
end

.find_olson_zone(str) ⇒ Object



609
610
611
612
613
# File 'lib/et-orbi.rb', line 609

def find_olson_zone(str)

  list_olson_zones(str).each { |s| z = get_tzone(s); return z if z }
  nil
end

.get_tzone(o) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/et-orbi.rb', line 142

def get_tzone(o)

  return o if o.is_a?(::TZInfo::Timezone)
  return nil if o == nil
  return determine_local_tzone if o == :local
  return ::TZInfo::Timezone.get('Zulu') if o == 'Z'
  return o.tzinfo if o.respond_to?(:tzinfo)

  o = to_offset(o) if o.is_a?(Numeric)

  return nil unless o.is_a?(String)

  (@custom_tz_cache ||= {})[o] ||
  get_offset_tzone(o) ||
  (::TZInfo::Timezone.get(o) rescue nil)
end

.list_iso8601_zones(s) ⇒ Object

en.wikipedia.org/wiki/ISO_8601 Postel’s law applies



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/et-orbi.rb', line 581

def list_iso8601_zones(s)

  s.scan(
    %r{
      (?<=:\d\d)
      \s*
      (?:
        [-+]
        (?:[0-1][0-9]|2[0-4])
        (?:(?::)?(?:[0-5][0-9]|60))?
        (?![-+])
        |
        Z
      )
    }x
    ).collect(&:strip)
end

.list_olson_zones(s) ⇒ Object



599
600
601
602
603
604
605
606
607
# File 'lib/et-orbi.rb', line 599

def list_olson_zones(s)

  s.scan(
    %r{
      (?<=\s|\A)
      (?:[A-Za-z][A-Za-z0-9+_-]+)
      (?:\/(?:[A-Za-z][A-Za-z0-9+_-]+)){0,2}
    }x)
end

.make_from_array(a, zone) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/et-orbi.rb', line 118

def make_from_array(a, zone)

  t = Time.utc(*a)
  s = t.strftime("%Y-%m-%d %H:%M:%S.#{'%06d' % t.usec}")

  make_from_string(s, zone)
end

.make_from_date(d, zone) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/et-orbi.rb', line 109

def make_from_date(d, zone)

  make_from_time(
    d.respond_to?(:to_time) ?
    d.to_time :
    Time.parse(d.strftime('%Y-%m-%d %H:%M:%S')),
    zone)
end

.make_from_eotime(eot, zone) ⇒ Object



136
137
138
139
140
# File 'lib/et-orbi.rb', line 136

def make_from_eotime(eot, zone)

  return eot if zone == nil || zone == eot.zone
  EoTime.new(eot.to_f, zone)
end

.make_from_numeric(f, zone) ⇒ Object



131
132
133
134
# File 'lib/et-orbi.rb', line 131

def make_from_numeric(f, zone)

  EoTime.new(Time.now.to_f + f, zone)
end

.make_from_string(s, zone) ⇒ Object



126
127
128
129
# File 'lib/et-orbi.rb', line 126

def make_from_string(s, zone)

  parse(s, zone: zone)
end

.make_from_time(t, zone) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/et-orbi.rb', line 94

def make_from_time(t, zone)

  z =
    zone ||
    get_as_tzone(t) ||
    get_tzone(t.zone) ||
    get_local_tzone(t)

  z ||= t.zone
    # pass the abbreviation anyway,
    # it will be used in resulting the error message

  EoTime.new(t.to_f, z)
end

.make_time(*a) ⇒ Object Also known as: make



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/et-orbi.rb', line 72

def make_time(*a)

#p [ :mt, a ]
  zone = a.length > 1 ? get_tzone(a.last) : nil
  a.pop if zone
#p [ :mt, zone ]

  o = a.length > 1 ? a : a.first
#p [ :mt, :o, o ]

  case o
  when Time then make_from_time(o, zone)
  when Date then make_from_date(o, zone)
  when Array then make_from_array(o, zone)
  when String then make_from_string(o, zone)
  when Numeric then make_from_numeric(o, zone)
  when ::EtOrbi::EoTime then make_from_eotime(o, zone)
  else fail ArgumentError.new(
    "Cannot turn #{o.inspect} to a ::EtOrbi::EoTime instance")
  end
end

.now(zone = nil) ⇒ Object



17
18
19
20
# File 'lib/et-orbi.rb', line 17

def now(zone=nil)

  EoTime.new(Time.now.to_f, zone)
end

.os_tzObject



634
635
636
637
# File 'lib/et-orbi.rb', line 634

def os_tz

  @os_tz ||= (debian_tz || centos_tz || osx_tz)
end

.parse(str, opts = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/et-orbi.rb', line 22

def parse(str, opts={})

  if defined?(::Chronic) && t = ::Chronic.parse(str, opts)
    return EoTime.new(t, nil)
  end

  #rold = RUBY_VERSION < '1.9.0'
  #rold = RUBY_VERSION < '2.0.0'
  begin
    DateTime.parse(str)
  rescue
    fail ArgumentError, "No time information in #{str.inspect}"
  end #if rold
    #
    # is necessary since Time.parse('xxx') in Ruby < 1.9 yields `now`

  str_zone = get_tzone(list_iso8601_zones(str).last)
#p [ :parse, str, str_zone ]
#p ENV['TZ']

#p [ :parse, :oz, opts[:zone] ]
#p [ :parse, :sz, str_zone ]
#p [ :parse, :foz, find_olson_zone(str) ]
#p [ :parse, :ltz, local_tzone ]
  zone =
    opts[:zone] ||
    str_zone ||
    find_olson_zone(str) ||
    determine_local_tzone
#p [ :parse, :zone, zone ]

  str = str.sub(zone.name, '') unless zone.name.match(/\A[-+]/)
    #
    # for 'Sun Nov 18 16:01:00 Asia/Singapore 2012',
    # although where does rufus-scheduler have it from?

  local = Time.parse(str)
#p [ :parse, :local, local, local.zone ]

  secs =
    if str_zone
      local.to_f
    else
      zone.local_to_utc(local).to_f
    end
#p [ :parse, :secs, secs ]

  EoTime.new(secs, zone)
end

.platform_infoObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/et-orbi.rb', line 174

def platform_info

  etos = Proc.new { |k, v| "#{k}:#{v.inspect}" }

  '(' +
    {
      'etz' => ENV['TZ'],
      'tnz' => Time.now.zone,
      'tzid' => defined?(TZInfo::Data),
      'rv' => RUBY_VERSION,
      'rp' => RUBY_PLATFORM,
      'rorv' => (Rails::VERSION::STRING rescue nil),
      'astz' => ([ Time.zone.class, Time.zone.tzinfo.name ] rescue nil),
      'eov' => EtOrbi::VERSION,
      'eotnz' => EtOrbi::EoTime.now.zone,
      'eotnfz' => EtOrbi::EoTime.now.strftime('%z'),
      'eotlzn' => EtOrbi::EoTime.local_tzone.name,
    }.collect(&etos).join(',') + ',' +
    gather_tzs.collect(&etos).join(',') +
  ')'
end

.render_nozone_time(seconds) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/et-orbi.rb', line 159

def render_nozone_time(seconds)

  t =
    Time.utc(0) + seconds
  ts =
    t.strftime('%Y-%m-%d %H:%M:%S') +
    ".#{(seconds % 1).to_s.split('.').last}"
  tz =
    EtOrbi.determine_local_tzone
  z =
    tz ? tz.period_for_local(t).abbreviation.to_s : nil

  "(secs:#{seconds},utc~:#{ts.inspect},ltz~:#{z.inspect})"
end