Class: RocketJob::Plugins::Rufus::ZoTime

Inherits:
Object
  • Object
show all
Defined in:
lib/rocket_job/plugins/rufus/zo_time.rb

Overview

Zoning|edTime, whatever.

Constant Summary collapse

WEEK_S =
7 * 24 * 3600

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s, zone) ⇒ ZoTime

Returns a new instance of ZoTime.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 34

def initialize(s, zone)

  @seconds = s.to_f
  @zone = self.class.get_tzone(zone || :current)

  fail ArgumentError.new(
    "cannot determine timezone from #{zone.inspect}" +
      " (etz:#{ENV['TZ'].inspect},tnz:#{Time.now.zone.inspect}," +
      "tzid:#{defined?(TZInfo::Data).inspect})\n" +
      "Try setting `ENV['TZ'] = 'Continent/City'` in your script " +
      "(see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)" +
      (defined?(TZInfo::Data) ? '' : " and adding 'tzinfo-data' to your gems")
  ) unless @zone

  @time = nil # cache for #to_time result
end

Instance Attribute Details

#secondsObject

Returns the value of attribute seconds.



31
32
33
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 31

def seconds
  @seconds
end

#zoneObject

Returns the value of attribute zone.



32
33
34
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 32

def zone
  @zone
end

Class Method Details

.centos_tzObject



402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 402

def self.centos_tz

  path = '/etc/sysconfig/clock'

  File.open(path, 'rb') do |f|
    until f.eof?
      m = f.readline.match(/ZONE="([^"]+)"/)
      return (::TZInfo::Timezone.get(m[1]) rescue nil) if m
    end
  end if File.exist?(path)

  nil
end

.debian_tzObject



394
395
396
397
398
399
400
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 394

def self.debian_tz

  path = '/etc/timezone'

  File.exist?(path) &&
    (::TZInfo::Timezone.get(File.read(path).strip) rescue nil)
end

.extract_iso8601_zone(s) ⇒ Object

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



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 210

def self.extract_iso8601_zone(s)

  m = s.match(
    /[0-2]\d(?::?[0-6]\d(?::?[0-6]\d))?\s*([+-]\d\d(?::?\d\d)?)\s*\z/)
  return nil unless m

  zs = m[1].split(':')
  zs << '00' if zs.length < 2

  zh = zs[0].to_i.abs

  return nil if zh > 24
  return nil if zh == 24 && zs[1].to_i != 0

  zs.join(':')
end

.get_tzone(str) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 282

def self.get_tzone(str)

  return str if str.is_a?(::TZInfo::Timezone)

  # discard quickly when it's certainly not a timezone

  return nil if str == nil
  return nil if str == '*'

  ostr = str
  str = :current if str == :local

  # use Rails' zone by default if Rails is present

  return Time.zone.tzinfo if (
  ENV['TZ'].nil? && str == :current &&
    Time.respond_to?(:zone) && Time.zone.respond_to?(:tzinfo)
  )

  # ok, it's a timezone then

  str = ENV['TZ'] || Time.now.zone if str == :current

  # utc_offset

  if str.is_a?(Numeric)
    i = str.to_i
    sn = i < 0 ? '-' : '+'; i = i.abs
    hr = i / 3600; mn = i % 3600; sc = i % 60
    str = (sc > 0 ? "%s%02d:%02d:%02d" : "%s%02d:%02d") % [ sn, hr, mn, sc ]
  end

  return nil if str.index('#')
  # counters "sun#2", etc... On OSX would go all the way to true

  # vanilla time zones

  z = (::TZInfo::Timezone.get(str) rescue nil)
  return z if z

  # time zone abbreviations

  if str.match(/\A[A-Z0-9-]{3,6}\z/)

    toff = Time.now.utc_offset
    toff = nil if str != Time.now.zone

    twin = Time.utc(Time.now.year, 1, 1) # winter
    tsum = Time.utc(Time.now.year, 7, 1) # summer

    z =
      ::TZInfo::Timezone.all.find do |tz|

        pwin = tz.period_for_utc(twin)
        psum = tz.period_for_utc(tsum)

        if toff
          (pwin.abbreviation.to_s == str && pwin.utc_offset == toff) ||
            (psum.abbreviation.to_s == str && psum.utc_offset == toff)
        else
          # returns the first tz with the given abbreviation, almost useless
          # favour fully named zones...
          pwin.abbreviation.to_s == str ||
            psum.abbreviation.to_s == str
        end
      end
    return z if z
  end

  # some time zone aliases

  return ::TZInfo::Timezone.get('Zulu') if %w[ Z ].include?(str)

  # custom timezones, no DST, just an offset, like "+08:00" or "-01:30"

  tz = (@custom_tz_cache ||= {})[str]
  return tz if tz

  if m = str.match(/\A([+-][0-1][0-9]):?([0-5][0-9])\z/)

    hr = m[1].to_i
    mn = m[2].to_i

    hr = nil if hr.abs > 11
    hr = nil if mn > 59
    mn = -mn if hr && hr < 0

    return (
    @custom_tz_cache[str] =
      begin
        tzi = TZInfo::TransitionDataTimezoneInfo.new(str)
        tzi.offset(str, hr * 3600 + mn * 60, 0, str)
        tzi.create_timezone
      end
    ) if hr
  end

  # try with ENV['TZ']

  z = ostr == :current && (::TZInfo::Timezone.get(ENV['TZ']) rescue nil)
  return z if z

  # ask the system

  z = ostr == :current && (debian_tz || centos_tz || osx_tz)
  return z if z

  # so it's not a timezone.

  nil
end

.local_tzoneObject



427
428
429
430
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 427

def self.local_tzone

  get_tzone(:local)
end

.make(o) ⇒ Object



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 432

def self.make(o)

  zt =
    case o
    when Time
      ZoTime.new(o.to_f, o.zone)
    when Date
      t =
        o.respond_to?(:to_time) ?
          o.to_time :
          Time.parse(o.strftime('%Y-%m-%d %H:%M:%S'))
      ZoTime.new(t.to_f, t.zone)
    when String
      Rufus::Scheduler.parse_in(o, :no_error => true) || self.parse(o)
    else
      o
    end

  zt = ZoTime.new(Time.now.to_f + zt, nil) if zt.is_a?(Numeric)

  fail ArgumentError.new(
    "cannot turn #{o.inspect} to a ZoTime instance"
  ) unless zt.is_a?(ZoTime)

  zt
end

.now(zone = nil) ⇒ Object



202
203
204
205
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 202

def self.now(zone=nil)

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

.osx_tzObject



416
417
418
419
420
421
422
423
424
425
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 416

def self.osx_tz

  path = '/etc/localtime'

  return nil unless File.exist?(path)

  ::TZInfo::Timezone.get(
    File.readlink(path).split('/')[4..-1].join('/')
  ) rescue nil
end

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



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 227

def self.parse(str, opts={})

  if defined?(::Chronic) && t = ::Chronic.parse(str, opts)
    return ZoTime.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`

  zone = nil

  s =
    str.gsub(/\S+/) do |w|
      if z = get_tzone(w)
        zone ||= z
        ''
      else
        w
      end
    end

  local = Time.parse(s)
  izone = extract_iso8601_zone(s)

  zone ||=
    if s.match(/\dZ\b/)
      get_tzone('Zulu')
      #elsif rold && izone
    elsif izone
      get_tzone(izone)
    elsif local.zone.nil? && izone
      get_tzone(local.strftime('%:z'))
    else
      get_tzone(:local)
    end

  secs =
    #if rold && izone
    if izone
      local.to_f
    else
      zone.period_for_local(local).to_utc(local).to_f
    end

  ZoTime.new(secs, zone)
end

Instance Method Details

#+(t) ⇒ Object



133
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 133

def +(t); inc(t, 1); end

#-(t) ⇒ Object



134
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 134

def -(t); inc(t, -1); end

#<(o) ⇒ Object



92
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 92

def <(o); @seconds < _to_f(o); end

#<=(o) ⇒ Object



93
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 93

def <=(o); @seconds <= _to_f(o); end

#<=>(o) ⇒ Object



94
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 94

def <=>(o); @seconds <=> _to_f(o); end

#==(o) ⇒ Object



84
85
86
87
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 84

def ==(o)

  o.is_a?(ZoTime) && o.seconds == @seconds && o.zone == @zone
end

#>(o) ⇒ Object

alias eq? == # FIXME see Object#== (ri)



90
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 90

def >(o); @seconds > _to_f(o); end

#>=(o) ⇒ Object



91
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 91

def >=(o); @seconds >= _to_f(o); end

#add(t) ⇒ Object



130
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 130

def add(t); @time = nil; @seconds += t.to_f; end

#is_dst?Boolean Also known as: isdst

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 109

def is_dst?

  @zone.period_for_utc(utc).std_offset != 0
end

#iso8601(fraction_digits = 0) ⇒ Object



82
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 82

def iso8601(fraction_digits=0); to_time.iso8601(fraction_digits); end

#monthdaysObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 138

def monthdays

  date = to_time

  pos = 1
  d = self.dup

  loop do
    d.add(-WEEK_S)
    break if d.month != date.month
    pos = pos + 1
  end

  neg = -1
  d = self.dup

  loop do
    d.add(WEEK_S)
    break if d.month != date.month
    neg = neg - 1
  end

  [ "#{date.wday}##{pos}", "#{date.wday}##{neg}" ]
end

#strftime(format) ⇒ Object



123
124
125
126
127
128
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 123

def strftime(format)

  format = format.gsub(/%(\/?Z|:{0,2}z)/) { |f| strfz(f) }

  to_time.strftime(format)
end

#substract(t) ⇒ Object



131
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 131

def substract(t); @time = nil; @seconds -= t.to_f; end

#to_debug_sObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 168

def to_debug_s

  uo = self.utc_offset
  uos = uo < 0 ? '-' : '+'
  uo = uo.abs
  uoh, uom = [ uo / 3600, uo % 3600 ]

  [
    'zt',
    self.strftime('%Y-%m-%d %H:%M:%S'),
    "%s%02d:%02d" % [ uos, uoh, uom ],
    "dst:#{self.isdst}"
  ].join(' ')
end

#to_fObject



104
105
106
107
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 104

def to_f

  @seconds
end

#to_iObject



99
100
101
102
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 99

def to_i

  @seconds.to_i
end

#to_sObject



163
164
165
166
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 163

def to_s

  strftime('%Y-%m-%d %H:%M:%S %z')
end

#to_timeObject

Returns a Ruby Time instance.

Warning: the timezone of that Time instance will be UTC.



72
73
74
75
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 72

def to_time

  @time ||= begin; u = utc; @zone.period_for_utc(u).to_local(u); end
end

#to_time_sObject



197
198
199
200
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 197

def to_time_s

  strftime("%H:%M:%S.#{'%06d' % usec}")
end

#to_utc_comparison_sObject

Debug current time by showing local time / delta / utc time for example: “0120-7(0820)”



186
187
188
189
190
191
192
193
194
195
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 186

def to_utc_comparison_s

  per = @zone.period_for_utc(utc)
  off = per.utc_total_offset

  off = off / 3600
  off = off >= 0 ? "+#{off}" : off.to_s

  strftime('%H%M') + off + utc.strftime('(%H%M)')
end

#utcObject Also known as: getutc, getgm



63
64
65
66
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 63

def utc

  Time.utc(1970, 1, 1) + @seconds
end

#utc_offsetObject



115
116
117
118
119
120
121
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 115

def utc_offset

  #@zone.period_for_utc(utc).utc_offset
  #@zone.period_for_utc(utc).utc_total_offset
  #@zone.period_for_utc(utc).std_offset
  @zone.period_for_utc(utc).utc_offset
end