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.



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

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



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

def seconds
  @seconds
end

#zoneObject

Returns the value of attribute zone.



297
298
299
# File 'lib/et-orbi.rb', line 297

def zone
  @zone
end

Class Method Details

.get_tzone(o) ⇒ Object



272
273
274
275
# File 'lib/et-orbi.rb', line 272

def get_tzone(o)

  EtOrbi.get_tzone(o)
end

.local_tzoneObject



277
278
279
280
# File 'lib/et-orbi.rb', line 277

def local_tzone

  EtOrbi.local_tzone
end

.make(o) ⇒ Object



287
288
289
290
# File 'lib/et-orbi.rb', line 287

def make(o)

  EtOrbi.make_time(o)
end

.now(zone = nil) ⇒ Object



262
263
264
265
# File 'lib/et-orbi.rb', line 262

def now(zone=nil)

  EtOrbi.now(zone)
end

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



267
268
269
270
# File 'lib/et-orbi.rb', line 267

def parse(str, opts={})

  EtOrbi.parse(str, opts)
end

.platform_infoObject



282
283
284
285
# File 'lib/et-orbi.rb', line 282

def platform_info

  EtOrbi.platform_info
end

Instance Method Details

#+(t) ⇒ Object



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

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

#-(t) ⇒ Object



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

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

#<(o) ⇒ Object



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

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

#<=(o) ⇒ Object



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

#==(o) ⇒ Object



410
411
412
413
414
415
# File 'lib/et-orbi.rb', line 410

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)



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

#add(t) ⇒ Object



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

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

#inc(t, dir = 1) ⇒ Object



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

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)


377
378
379
380
# File 'lib/et-orbi.rb', line 377

def is_dst?

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

#iso8601(fraction_digits = 0) ⇒ Object



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

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

#localtime(zone = nil) ⇒ Object



498
499
500
501
# File 'lib/et-orbi.rb', line 498

def localtime(zone=nil)

  EoTime.new(self.to_f, zone)
end

#monthdaysObject



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

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



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

def strftime(format)

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

  to_time.strftime(format)
end

#subtract(t) ⇒ Object



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

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

#to_debug_sObject



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

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



348
349
350
351
# File 'lib/et-orbi.rb', line 348

def to_f

  @seconds
end

#to_iObject



353
354
355
356
# File 'lib/et-orbi.rb', line 353

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.



370
371
372
373
# File 'lib/et-orbi.rb', line 370

def to_local_time

  Time.at(@seconds)
end

#to_sObject



457
458
459
460
# File 'lib/et-orbi.rb', line 457

def to_s

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

#to_time_sObject



476
477
478
479
# File 'lib/et-orbi.rb', line 476

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



465
466
467
468
469
470
471
472
473
474
# File 'lib/et-orbi.rb', line 465

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.



331
332
333
334
# File 'lib/et-orbi.rb', line 331

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)


339
340
341
342
# File 'lib/et-orbi.rb', line 339

def utc?

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

#utc_offsetObject



398
399
400
401
# File 'lib/et-orbi.rb', line 398

def utc_offset

  @zone.period_for_utc(utc).utc_offset
end

#wday_in_monthObject



503
504
505
506
# File 'lib/et-orbi.rb', line 503

def wday_in_month

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