Class: EtOrbi::EoTime
- Inherits:
-
Object
- Object
- EtOrbi::EoTime
- Defined in:
- lib/et-orbi.rb
Constant Summary collapse
- WEEK_S =
7 * 24 * 3600
Instance Attribute Summary collapse
-
#seconds ⇒ Object
instance methods.
-
#zone ⇒ Object
Returns the value of attribute zone.
Class Method Summary collapse
- .centos_tz ⇒ Object
-
.debian_tz ⇒ Object
system tz determination.
- .determine_local_tzone ⇒ Object
- .determine_local_tzones ⇒ Object
-
.gather_tzs ⇒ Object
def self.find_tz.
- .get_offset_tzone(str) ⇒ Object
- .get_tzone(o) ⇒ Object
-
.list_iso8601_zones(s) ⇒ Object
en.wikipedia.org/wiki/ISO_8601 Postel’s law applies.
- .list_olson_zones(s) ⇒ Object
- .local_tzone ⇒ Object
- .make(o) ⇒ Object
-
.now(zone = nil) ⇒ Object
class methods.
- .osx_tz ⇒ Object
- .parse(str, opts = {}) ⇒ Object
- .platform_info ⇒ Object
- .time_to_eo_time(t) ⇒ Object
- .to_offset(n) ⇒ Object
Instance Method Summary collapse
- #+(t) ⇒ Object
- #-(t) ⇒ Object
- #<(o) ⇒ Object
- #<=(o) ⇒ Object
- #<=>(o) ⇒ Object
- #==(o) ⇒ Object
-
#>(o) ⇒ Object
alias eq? == # FIXME see Object#== (ri).
- #>=(o) ⇒ Object
- #_to_f(o) ⇒ Object
- #add(t) ⇒ Object
- #inc(t, dir) ⇒ Object
-
#initialize(s, zone) ⇒ EoTime
constructor
A new instance of EoTime.
- #is_dst? ⇒ Boolean (also: #isdst)
- #iso8601(fraction_digits = 0) ⇒ Object
- #monthdays ⇒ Object
- #strftime(format) ⇒ Object
-
#strfz(code) ⇒ Object
protected.
- #subtract(t) ⇒ Object
- #to_debug_s ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
-
#to_time ⇒ Object
Returns a Ruby Time instance.
- #to_time_s ⇒ Object
-
#to_utc_comparison_s ⇒ Object
Debug current time by showing local time / delta / utc time for example: “0120-7(0820)”.
- #utc ⇒ Object (also: #getutc, #getgm)
- #utc_offset ⇒ Object
Constructor Details
#initialize(s, zone) ⇒ EoTime
Returns a new instance of EoTime.
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/et-orbi.rb', line 279 def initialize(s, zone) @seconds = s.to_f @zone = self.class.get_tzone(zone || :local) #fail ArgumentError.new( # "cannot determine timezone from #{zone.inspect}" + # " (etz:#{ENV['TZ'].inspect},tnz:#{Time.now.zone.inspect}," + # "tzid:#{defined?(TZInfo::Data).inspect}," + # "rv:#{RUBY_VERSION.inspect},rp:#{RUBY_PLATFORM.inspect}," + # "stz:(#{self.class.gather_tzs.map { |k, v| "#{k}:#{v.inspect}"}.join(',')})) \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 fail ArgumentError.new( "cannot determine timezone from #{zone.inspect}" + "\n#{self.class.platform_info}" + "\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
#seconds ⇒ Object
instance methods
276 277 278 |
# File 'lib/et-orbi.rb', line 276 def seconds @seconds end |
#zone ⇒ Object
Returns the value of attribute zone.
277 278 279 |
# File 'lib/et-orbi.rb', line 277 def zone @zone end |
Class Method Details
.centos_tz ⇒ Object
519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'lib/et-orbi.rb', line 519 def self.centos_tz path = '/etc/sysconfig/clock' File.open(path, 'rb') do |f| until f.eof? if m = f.readline.match(/ZONE="([^"]+)"/); return m[1]; end end end if File.exist?(path) nil rescue; nil; end |
.debian_tz ⇒ Object
system tz determination
512 513 514 515 516 517 |
# File 'lib/et-orbi.rb', line 512 def self.debian_tz path = '/etc/timezone' File.exist?(path) ? File.read(path).strip : nil rescue; nil; end |
.determine_local_tzone ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/et-orbi.rb', line 170 def self.determine_local_tzone etz = ENV['TZ'] tz = ::TZInfo::Timezone.get(etz) rescue nil return tz if tz tz = Time.zone.tzinfo \ if Time.respond_to?(:zone) && Time.zone.respond_to?(:tzinfo) return tz if tz tzs = determine_local_tzones (etz && tzs.find { |z| z.name == etz }) || tzs.first end |
.determine_local_tzones ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/et-orbi.rb', line 186 def self.determine_local_tzones tabbs = (-6..5) .collect { |i| (Time.now + i * 30 * 24 * 3600).zone } .uniq .sort t = Time.now tu = t.dup.utc # /!\ dup is necessary, #utc modifies its target twin = Time.utc(t.year, 1, 1) # winter tsum = Time.utc(t.year, 7, 1) # summer ::TZInfo::Timezone.all.select do |tz| pabbs = [ tz.period_for_utc(twin).abbreviation.to_s, tz.period_for_utc(tsum).abbreviation.to_s ].uniq.sort pabbs == tabbs end end |
.gather_tzs ⇒ Object
def self.find_tz
debian_tz || centos_tz || osx_tz
end
546 547 548 549 |
# File 'lib/et-orbi.rb', line 546 def self.gather_tzs { :debian => debian_tz, :centos => centos_tz, :osx => osx_tz } end |
.get_offset_tzone(str) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/et-orbi.rb', line 129 def self.get_offset_tzone(str) # custom timezones, no DST, just an offset, like "+08:00" or "-01:30" m = str.match(/\A([+-][0-1][0-9]):?([0-5][0-9])?\z/) return nil unless m 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 nil end |
.get_tzone(o) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/et-orbi.rb', line 112 def self.get_tzone(o) #p [ :gtz, o ] return nil if o == nil return local_tzone if o == :local return o if o.is_a?(::TZInfo::Timezone) return ::TZInfo::Timezone.get('Zulu') if o == 'Z' 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
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/et-orbi.rb', line 214 def self.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
232 233 234 235 236 237 238 239 240 |
# File 'lib/et-orbi.rb', line 232 def self.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 |
.local_tzone ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/et-orbi.rb', line 155 def self.local_tzone @local_tzone = nil \ if @local_tzone_loaded_at && (Time.now > @local_tzone_loaded_at + 1800) @local_tzone = nil \ if @local_tzone_tz != ENV['TZ'] @local_tzone ||= begin @local_tzone_tz = ENV['TZ'] @local_tzone_loaded_at = Time.now determine_local_tzone end end |
.make(o) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/et-orbi.rb', line 76 def self.make(o) ot = case o when Time time_to_eo_time( o) when Date time_to_eo_time( o.respond_to?(:to_time) ? o.to_time : Time.parse(o.strftime('%Y-%m-%d %H:%M:%S'))) when String #Rufus::Scheduler.parse_in(o, :no_error => true) || self.parse(o) parse(o) else o end ot = EoTime.new(Time.now.to_f + ot, nil) if ot.is_a?(Numeric) fail ArgumentError.new( "cannot turn #{o.inspect} to a EoTime instance" ) unless ot.is_a?(EoTime) ot end |
.now(zone = nil) ⇒ Object
class methods
17 18 19 20 |
# File 'lib/et-orbi.rb', line 17 def self.now(zone=nil) EoTime.new(Time.now.to_f, zone) end |
.osx_tz ⇒ Object
532 533 534 535 536 537 538 539 |
# File 'lib/et-orbi.rb', line 532 def self.osx_tz path = '/etc/localtime' File.symlink?(path) ? File.readlink(path).split('/')[4..-1].join('/') : nil rescue; nil; 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 |
# File 'lib/et-orbi.rb', line 22 def self.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' #p [ '---', str ] 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 = izone = get_tzone(list_iso8601_zones(str).last) list_olson_zones(str).each { |s| break if zone; zone = get_tzone(s) } zone ||= local_tzone 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) secs = if izone local.to_f else zone.period_for_local(local).to_utc(local).to_f end EoTime.new(secs, zone) end |
.platform_info ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/et-orbi.rb', line 242 def self.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, 'eov' => EtOrbi::VERSION, }.collect(&etos).join(',') + ',' + gather_tzs.collect(&etos).join(',') + ')' end |
.time_to_eo_time(t) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/et-orbi.rb', line 63 def self.time_to_eo_time(t) z = get_tzone(t.zone) || ( local_tzone.period_for_local(t).abbreviation.to_s == t.zone && local_tzone ) || t.zone EoTime.new(t.to_f, z) end |
.to_offset(n) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/et-orbi.rb', line 104 def self.to_offset(n) i = n.to_i sn = i < 0 ? '-' : '+'; i = i.abs hr = i / 3600; mn = i % 3600; sc = i % 60 (sc > 0 ? "%s%02d:%02d:%02d" : "%s%02d:%02d") % [ sn, hr, mn, sc ] end |
Instance Method Details
#+(t) ⇒ Object
402 |
# File 'lib/et-orbi.rb', line 402 def +(t); inc(t, 1); end |
#-(t) ⇒ Object
403 |
# File 'lib/et-orbi.rb', line 403 def -(t); inc(t, -1); end |
#<(o) ⇒ Object
395 |
# File 'lib/et-orbi.rb', line 395 def <(o); @seconds < _to_f(o); end |
#<=(o) ⇒ Object
396 |
# File 'lib/et-orbi.rb', line 396 def <=(o); @seconds <= _to_f(o); end |
#<=>(o) ⇒ Object
397 |
# File 'lib/et-orbi.rb', line 397 def <=>(o); @seconds <=> _to_f(o); end |
#==(o) ⇒ Object
387 388 389 390 |
# File 'lib/et-orbi.rb', line 387 def ==(o) o.is_a?(EoTime) && o.seconds == @seconds && o.zone == @zone end |
#>(o) ⇒ Object
alias eq? == # FIXME see Object#== (ri)
393 |
# File 'lib/et-orbi.rb', line 393 def >(o); @seconds > _to_f(o); end |
#>=(o) ⇒ Object
394 |
# File 'lib/et-orbi.rb', line 394 def >=(o); @seconds >= _to_f(o); end |
#_to_f(o) ⇒ Object
500 501 502 503 504 505 506 507 |
# File 'lib/et-orbi.rb', line 500 def _to_f(o) fail ArgumentError( "comparison of EoTime with #{o.inspect} failed" ) unless o.is_a?(EoTime) || o.is_a?(Time) o.to_f end |
#add(t) ⇒ Object
399 |
# File 'lib/et-orbi.rb', line 399 def add(t); @time = nil; @seconds += t.to_f; end |
#inc(t, dir) ⇒ Object
486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/et-orbi.rb', line 486 def inc(t, dir) if t.is_a?(Numeric) nt = self.dup nt.seconds += dir * t.to_f nt elsif t.respond_to?(:to_f) @seconds + dir * t.to_f else fail ArgumentError.new( "cannot call EoTime #- or #+ with arg of class #{t.class}") end end |
#is_dst? ⇒ Boolean Also known as: isdst
351 352 353 354 |
# File 'lib/et-orbi.rb', line 351 def is_dst? @zone.period_for_utc(utc).std_offset != 0 end |
#iso8601(fraction_digits = 0) ⇒ Object
385 |
# File 'lib/et-orbi.rb', line 385 def iso8601(fraction_digits=0); to_time.iso8601(fraction_digits); end |
#monthdays ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/et-orbi.rb', line 407 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
335 336 337 338 339 340 |
# File 'lib/et-orbi.rb', line 335 def strftime(format) format = format.gsub(/%(\/?Z|:{0,2}z)/) { |f| strfz(f) } to_time.strftime(format) end |
#strfz(code) ⇒ Object
protected
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/et-orbi.rb', line 459 def strfz(code) return @zone.name if code == '%/Z' per = @zone.period_for_utc(utc) return per.abbreviation.to_s if code == '%Z' off = per.utc_total_offset # sn = off < 0 ? '-' : '+'; off = off.abs hr = off / 3600 mn = (off % 3600) / 60 sc = 0 fmt = if code == '%z' "%s%02d%02d" elsif code == '%:z' "%s%02d:%02d" else "%s%02d:%02d:%02d" end fmt % [ sn, hr, mn, sc ] end |
#subtract(t) ⇒ Object
400 |
# File 'lib/et-orbi.rb', line 400 def subtract(t); @time = nil; @seconds -= t.to_f; end |
#to_debug_s ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/et-orbi.rb', line 357 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_f ⇒ Object
325 326 327 328 |
# File 'lib/et-orbi.rb', line 325 def to_f @seconds end |
#to_i ⇒ Object
330 331 332 333 |
# File 'lib/et-orbi.rb', line 330 def to_i @seconds.to_i end |
#to_s ⇒ Object
432 433 434 435 |
# File 'lib/et-orbi.rb', line 432 def to_s strftime('%Y-%m-%d %H:%M:%S %z') end |
#to_time ⇒ Object
Returns a Ruby Time instance.
Warning: the timezone of that Time instance will be UTC.
346 347 348 349 |
# File 'lib/et-orbi.rb', line 346 def to_time @time ||= begin; u = utc; @zone.period_for_utc(u).to_local(u); end end |
#to_time_s ⇒ Object
451 452 453 454 |
# File 'lib/et-orbi.rb', line 451 def to_time_s strftime("%H:%M:%S.#{'%06d' % usec}") end |
#to_utc_comparison_s ⇒ Object
Debug current time by showing local time / delta / utc time for example: “0120-7(0820)”
440 441 442 443 444 445 446 447 448 449 |
# File 'lib/et-orbi.rb', line 440 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 |
#utc ⇒ Object Also known as: getutc, getgm
317 318 319 320 |
# File 'lib/et-orbi.rb', line 317 def utc Time.utc(1970, 1, 1) + @seconds end |
#utc_offset ⇒ Object
372 373 374 375 376 377 378 |
# File 'lib/et-orbi.rb', line 372 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 |