Class: EtOrbi::EoTime

Inherits:
Object
  • Object
show all
Defined in:
lib/et-orbi.rb

Overview

Our EoTime class (which quacks like a ::Time).

An EoTime instance should respond to most of the methods ::Time instances respond to. If a method is missing, feel free to open an issue to ask (politely) for it. If it makes sense, it’ll get added, else a workaround will get suggested. The immediate workaround is to call #to_t on the EoTime instance to get equivalent ::Time instance in the local, current, timezone.

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) ⇒ EoTime

Returns a new instance of EoTime.



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/et-orbi.rb', line 297

def initialize(s, zone)

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

  fail ArgumentError.new(
    "Cannot determine timezone from #{zone.inspect}" +
    "\n#{EtOrbi.render_nozone_time(s)}" +
    "\n#{EtOrbi.platform_info.sub(',debian:', ",\ndebian:")}" +
    "\nTry setting `ENV['TZ'] = 'Continent/City'` in your script " +
    "(see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)" +
    (defined?(TZInfo::Data) ? '' : "\nand adding gem 'tzinfo-data'")
  ) unless @zone

  @time = nil # cache for #to_time result
end

Instance Attribute Details

#secondsObject

instance methods



294
295
296
# File 'lib/et-orbi.rb', line 294

def seconds
  @seconds
end

#zoneObject

Returns the value of attribute zone.



295
296
297
# File 'lib/et-orbi.rb', line 295

def zone
  @zone
end

Class Method Details

.get_tzone(o) ⇒ Object



260
261
262
263
# File 'lib/et-orbi.rb', line 260

def get_tzone(o)

  EtOrbi.get_tzone(o)
end

.local(*a) ⇒ Object



285
286
287
288
# File 'lib/et-orbi.rb', line 285

def local(*a)

  EtOrbi.make_from_array(a, local_tzone)
end

.local_tzoneObject



265
266
267
268
# File 'lib/et-orbi.rb', line 265

def local_tzone

  EtOrbi.determine_local_tzone
end

.make(o) ⇒ Object



275
276
277
278
# File 'lib/et-orbi.rb', line 275

def make(o)

  EtOrbi.make_time(o)
end

.now(zone = nil) ⇒ Object



250
251
252
253
# File 'lib/et-orbi.rb', line 250

def now(zone=nil)

  EtOrbi.now(zone)
end

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



255
256
257
258
# File 'lib/et-orbi.rb', line 255

def parse(str, opts={})

  EtOrbi.parse(str, opts)
end

.platform_infoObject



270
271
272
273
# File 'lib/et-orbi.rb', line 270

def platform_info

  EtOrbi.platform_info
end

.utc(*a) ⇒ Object



280
281
282
283
# File 'lib/et-orbi.rb', line 280

def utc(*a)

  EtOrbi.make_from_array(a, EtOrbi.get_tzone('UTC'))
end

Instance Method Details

#+(t) ⇒ Object



424
# File 'lib/et-orbi.rb', line 424

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

#-(t) ⇒ Object



425
# File 'lib/et-orbi.rb', line 425

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

#<(o) ⇒ Object



417
# File 'lib/et-orbi.rb', line 417

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

#<=(o) ⇒ Object



418
# File 'lib/et-orbi.rb', line 418

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

#<=>(o) ⇒ Object



419
# File 'lib/et-orbi.rb', line 419

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

#==(o) ⇒ Object



407
408
409
410
411
412
# File 'lib/et-orbi.rb', line 407

def ==(o)

  o.is_a?(EoTime) &&
  o.seconds == @seconds &&
  (o.zone == @zone || o.zone.current_period == @zone.current_period)
end

#>(o) ⇒ Object

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



415
# File 'lib/et-orbi.rb', line 415

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

#>=(o) ⇒ Object



416
# File 'lib/et-orbi.rb', line 416

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

#add(t) ⇒ Object



421
# File 'lib/et-orbi.rb', line 421

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

#inc(t, dir = 1) ⇒ Object



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/et-orbi.rb', line 484

def inc(t, dir=1)

  case t
  when Numeric
    nt = self.dup
    nt.seconds += dir * t.to_f
    nt
  when ::Time, ::EtOrbi::EoTime
    fail ArgumentError.new(
      "Cannot add #{t.class} to EoTime") if dir > 0
    @seconds + dir * t.to_f
  else
    fail ArgumentError.new(
      "Cannot call add or subtract #{t.class} to EoTime instance")
  end
end

#is_dst?Boolean Also known as: isdst

Returns:

  • (Boolean)


375
376
377
378
# File 'lib/et-orbi.rb', line 375

def is_dst?

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

#iso8601(fraction_digits = 0) ⇒ Object



459
460
461
462
463
# File 'lib/et-orbi.rb', line 459

def iso8601(fraction_digits=0)

  s = (fraction_digits || 0) > 0 ? ".%#{fraction_digits}N" : ''
  strftime("%Y-%m-%dT%H:%M:%S#{s}%:z")
end

#localtime(zone = nil) ⇒ Object Also known as: translate



501
502
503
504
# File 'lib/et-orbi.rb', line 501

def localtime(zone=nil)

  EoTime.new(self.to_f, zone)
end

#monthdaysObject



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/et-orbi.rb', line 429

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



356
357
358
359
360
361
# File 'lib/et-orbi.rb', line 356

def strftime(format)

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

  to_time.strftime(format)
end

#subtract(t) ⇒ Object



422
# File 'lib/et-orbi.rb', line 422

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

#to_debug_sObject



381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/et-orbi.rb', line 381

def to_debug_s

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

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

#to_fObject



346
347
348
349
# File 'lib/et-orbi.rb', line 346

def to_f

  @seconds
end

#to_iObject



351
352
353
354
# File 'lib/et-orbi.rb', line 351

def to_i

  @seconds.to_i
end

#to_local_timeObject Also known as: to_t

Returns this ::EtOrbi::EoTime as a ::Time instance in the current timezone.

Has a #to_t alias.



368
369
370
371
# File 'lib/et-orbi.rb', line 368

def to_local_time

  Time.at(@seconds)
end

#to_sObject



454
455
456
457
# File 'lib/et-orbi.rb', line 454

def to_s

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

#to_time_sObject



479
480
481
482
# File 'lib/et-orbi.rb', line 479

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)”



468
469
470
471
472
473
474
475
476
477
# File 'lib/et-orbi.rb', line 468

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, to_utc_time

Returns this ::EtOrbi::EoTime as a ::Time instance in the current UTC timezone.



329
330
331
332
# File 'lib/et-orbi.rb', line 329

def utc

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

#utc?Boolean

Returns true if this ::EtOrbi::EoTime instance timezone is UTC. Returns false else.

Returns:

  • (Boolean)


337
338
339
340
# File 'lib/et-orbi.rb', line 337

def utc?

  %w[ zulu utc gmt ].include?(@zone.canonical_identifier.downcase)
end

#utc_offsetObject



396
397
398
399
# File 'lib/et-orbi.rb', line 396

def utc_offset

  @zone.period_for_utc(utc).utc_offset
end

#wday_in_monthObject



508
509
510
511
# File 'lib/et-orbi.rb', line 508

def wday_in_month

  [ count_weeks(-1), - count_weeks(1) ]
end