Class: RocketJob::Plugins::Rufus::ZoTime
- Inherits:
-
Object
- Object
- RocketJob::Plugins::Rufus::ZoTime
- 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
-
#seconds ⇒ Object
Returns the value of attribute seconds.
-
#zone ⇒ Object
Returns the value of attribute zone.
Class Method Summary collapse
-
.extract_iso8601_zone(s) ⇒ Object
en.wikipedia.org/wiki/ISO_8601 Postel’s law applies.
- .get_tzone(str) ⇒ Object
- .local_tzone ⇒ Object
- .make(o) ⇒ Object
- .now(zone = nil) ⇒ Object
- .parse(str, opts = {}) ⇒ 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
- #add(t) ⇒ Object
-
#initialize(s, zone) ⇒ ZoTime
constructor
A new instance of ZoTime.
- #is_dst? ⇒ Boolean (also: #isdst)
- #iso8601(fraction_digits = 0) ⇒ Object
- #monthdays ⇒ Object
- #strftime(format) ⇒ Object
- #substract(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) ⇒ ZoTime
Returns a new instance of ZoTime.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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})" ) unless @zone @time = nil # cache for #to_time result end |
Instance Attribute Details
#seconds ⇒ Object
Returns the value of attribute seconds.
31 32 33 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 31 def seconds @seconds end |
#zone ⇒ Object
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
.extract_iso8601_zone(s) ⇒ Object
en.wikipedia.org/wiki/ISO_8601 Postel’s law applies
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 207 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
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 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 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 279 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 == '*' # ok, it's a timezone then ostr = str str = ENV['TZ'] || Time.now.zone if str == :current || str == :local # 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.nil? || 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 # last try with ENV['TZ'] z = (ostr == :local || ostr == :current) && (::TZInfo::Timezone.get(ENV['TZ']) rescue nil) return z if z # so it's not a timezone. nil end |
.local_tzone ⇒ Object
378 379 380 381 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 378 def self.local_tzone get_tzone(:local) end |
.make(o) ⇒ Object
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 383 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
199 200 201 202 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 199 def self.now(zone=nil) ZoTime.new(Time.now.to_f, zone) end |
.parse(str, opts = {}) ⇒ Object
224 225 226 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 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 224 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
130 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 130 def +(t); inc(t, 1); end |
#-(t) ⇒ Object
131 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 131 def -(t); inc(t, -1); end |
#<(o) ⇒ Object
89 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 89 def <(o); @seconds < _to_f(o); end |
#<=(o) ⇒ Object
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 |
#==(o) ⇒ Object
81 82 83 84 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 81 def ==(o) o.is_a?(ZoTime) && o.seconds == @seconds && o.zone == @zone end |
#>(o) ⇒ Object
alias eq? == # FIXME see Object#== (ri)
87 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 87 def >(o); @seconds > _to_f(o); end |
#>=(o) ⇒ Object
88 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 88 def >=(o); @seconds >= _to_f(o); end |
#add(t) ⇒ Object
127 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 127 def add(t); @time = nil; @seconds += t.to_f; end |
#is_dst? ⇒ Boolean Also known as: isdst
106 107 108 109 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 106 def is_dst? @zone.period_for_utc(utc).std_offset != 0 end |
#iso8601(fraction_digits = 0) ⇒ Object
79 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 79 def iso8601(fraction_digits=0); to_time.iso8601(fraction_digits); end |
#monthdays ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 135 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
120 121 122 123 124 125 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 120 def strftime(format) format = format.gsub(/%(\/?Z|:{0,2}z)/) { |f| strfz(f) } to_time.strftime(format) end |
#substract(t) ⇒ Object
128 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 128 def substract(t); @time = nil; @seconds -= t.to_f; end |
#to_debug_s ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 165 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_f ⇒ Object
101 102 103 104 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 101 def to_f @seconds end |
#to_i ⇒ Object
96 97 98 99 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 96 def to_i @seconds.to_i end |
#to_s ⇒ Object
160 161 162 163 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 160 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.
69 70 71 72 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 69 def to_time @time ||= begin; u = utc; @zone.period_for_utc(u).to_local(u); end end |
#to_time_s ⇒ Object
194 195 196 197 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 194 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)”
183 184 185 186 187 188 189 190 191 192 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 183 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
60 61 62 63 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 60 def utc Time.utc(1970, 1, 1) + @seconds end |
#utc_offset ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/rocket_job/plugins/rufus/zo_time.rb', line 112 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 |