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.



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/et-orbi.rb', line 302

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



299
300
301
# File 'lib/et-orbi.rb', line 299

def seconds
  @seconds
end

#zoneObject

Returns the value of attribute zone.



300
301
302
# File 'lib/et-orbi.rb', line 300

def zone
  @zone
end

Class Method Details

.get_tzone(o) ⇒ Object



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

def get_tzone(o)

  EtOrbi.get_tzone(o)
end

.local_tzoneObject



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

def local_tzone

  EtOrbi.local_tzone
end

.make(o) ⇒ Object



290
291
292
293
# File 'lib/et-orbi.rb', line 290

def make(o)

  EtOrbi.make_time(o)
end

.now(zone = nil) ⇒ Object



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

def now(zone=nil)

  EtOrbi.now(zone)
end

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



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

def parse(str, opts={})

  EtOrbi.parse(str, opts)
end

.platform_infoObject



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

def platform_info

  EtOrbi.platform_info
end

Instance Method Details

#+(t) ⇒ Object



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

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

#-(t) ⇒ Object



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

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

#<(o) ⇒ Object



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

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

#<=(o) ⇒ Object



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

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

#<=>(o) ⇒ Object



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

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

#==(o) ⇒ Object



413
414
415
416
417
418
# File 'lib/et-orbi.rb', line 413

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)



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

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

#>=(o) ⇒ Object



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

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

#add(t) ⇒ Object



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

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)


380
381
382
383
# File 'lib/et-orbi.rb', line 380

def is_dst?

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

#iso8601(fraction_digits = 0) ⇒ Object



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

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

#localtime(zone = nil) ⇒ Object



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

def localtime(zone=nil)

  EoTime.new(self.to_f, zone)
end

#monthdaysObject



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/et-orbi.rb', line 435

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



361
362
363
364
365
366
# File 'lib/et-orbi.rb', line 361

def strftime(format)

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

  to_time.strftime(format)
end

#subtract(t) ⇒ Object



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

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

#to_debug_sObject



386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/et-orbi.rb', line 386

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



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

def to_f

  @seconds
end

#to_iObject



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

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.



373
374
375
376
# File 'lib/et-orbi.rb', line 373

def to_local_time

  Time.at(@seconds)
end

#to_sObject



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

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.



334
335
336
337
# File 'lib/et-orbi.rb', line 334

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)


342
343
344
345
# File 'lib/et-orbi.rb', line 342

def utc?

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

#utc_offsetObject



401
402
403
404
# File 'lib/et-orbi.rb', line 401

def utc_offset

  @zone.period_for_utc(utc).utc_offset
end

#wday_in_monthObject



506
507
508
509
# File 'lib/et-orbi.rb', line 506

def wday_in_month

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