The TjTime class is based on the original Ruby class Time but provides lots of additional functionality.
The number of days per month. Leap years are taken care of separately.
Check if zone is a valid time zone.
# File lib/TjTime.rb, line 64 64: def TjTime.checkTimeZone(zone) 65: return true if zone == 'UTC' 66: 67: # Valid time zones must be of the form 'Region/City' 68: return false unless zone.include?('/') 69: 70: # Save curent value of TZ 71: tz = ENV['TZ'] 72: ENV['TZ'] = zone 73: newZone = Time.new.zone 74: # If the time zone is valid, the OS can convert a zone like 75: # 'America/Denver' into 'MST'. Unknown time zones are either not 76: # converted or cause a fallback to UTC. 77: # Since glibc 2.10 Time.new.zone only return the region for illegal 78: # zones instead of the full zone string like it does on earlier 79: # versions. 80: region = zone[0..zone.index('/') - 1] 81: res = (newZone != zone && newZone != region && newZone != 'UTC') 82: # Restore TZ if it was set earlier. 83: ENV['TZ'] = tz if tz 84: res 85: end
Creates a time based on given values, interpreted as UTC. See Time.gm() for details.
# File lib/TjTime.rb, line 53 53: def TjTime.gm(*args) 54: TjTime.new(Time.gm(*args)) 55: end
Creates a time based on given values, interpreted as local time. The result is stored as UTC time, though. See Time.local() for details.
# File lib/TjTime.rb, line 59 59: def TjTime.local(*args) 60: TjTime.new(Time.local(*args).gmtime) 61: end
The constructor is overloaded and accepts 3 kinds of arguments. If t is a Time object this is just copied to the @time variable. If it’s a string, it is parsed as a date. Or else it is interpreted as seconds after Epoch.
# File lib/TjTime.rb, line 35 35: def initialize(t) 36: if t.is_a?(Time) 37: @time = t 38: elsif t.is_a?(String) 39: d = DateTime.parse(t) 40: @time = Time.mktime(d.year, d.mon, d.day, d.hour, d.min, d.sec) 41: else 42: @time = Time.at(t) 43: end 44: end
Convert the time to seconds since Epoch and return the module of val.
# File lib/TjTime.rb, line 115 115: def %(val) 116: @time.to_i % val 117: end
Add secs number of seconds to the time.
# File lib/TjTime.rb, line 100 100: def +(secs) 101: TjTime.new(@time + secs) 102: end
Substract arg number of seconds or return the number of seconds between arg and this time.
# File lib/TjTime.rb, line 106 106: def -(arg) 107: if arg.is_a?(TjTime) 108: @time - arg.time 109: else 110: TjTime.new(@time - arg) 111: end 112: end
Return true if time is smaller than t.
# File lib/TjTime.rb, line 120 120: def <(t) 121: @time < t.time 122: end
Return true if time is smaller or equal than t.
# File lib/TjTime.rb, line 125 125: def <=(t) 126: @time <= t.time 127: end
Coparison operator for time with another time t.
# File lib/TjTime.rb, line 146 146: def <=>(t) 147: @time <=> t.time 148: end
Return true if time and t are identical.
# File lib/TjTime.rb, line 140 140: def ==(t) 141: return false if t.nil? 142: @time == t.time 143: end
Return true if time is larger than t.
# File lib/TjTime.rb, line 130 130: def >(t) 131: @time > t.time 132: end
Return true if time is larger or equal than t.
# File lib/TjTime.rb, line 135 135: def >=(t) 136: @time >= t.time 137: end
Align the date to a time grid. The grid distance is determined by clock.
# File lib/TjTime.rb, line 88 88: def align(clock) 89: TjTime.new((@time.to_i / clock) * clock) 90: end
Normalize time to the beginning of the current hour.
# File lib/TjTime.rb, line 161 161: def beginOfHour 162: t = @time.localtime.to_a 163: t[0, 2] = Array.new(2, 0) 164: t.slice!(6, 4) 165: t.reverse! 166: TjTime.new(Time.local(*t)) 167: end
Normalize time to the beginning of the current month.
# File lib/TjTime.rb, line 194 194: def beginOfMonth 195: t = @time.localtime.to_a 196: t[0, 3] = Array.new(3, 0) 197: t[3] = 1 198: t.slice!(6, 4) 199: t.reverse! 200: TjTime.new(Time.local(*t)) 201: end
Normalize time to the beginning of the current quarter.
# File lib/TjTime.rb, line 204 204: def beginOfQuarter 205: t = @time.localtime.to_a 206: t[0, 3] = Array.new(3, 0) 207: t[3] = 1 208: t[4] = ((t[4] - 1) % 3) + 1 209: t.slice!(6, 4) 210: t.reverse! 211: TjTime.new(Time.local(*t)) 212: end
Normalize time to the beginning of the current week. startMonday determines whether the week should start on Monday or Sunday.
# File lib/TjTime.rb, line 180 180: def beginOfWeek(startMonday) 181: t = @time.to_a 182: # Set time to noon, 12:00:00 183: t[0, 3] = [ 0, 0, 12 ] 184: weekday = t[6] 185: t.slice!(6, 4) 186: t.reverse! 187: # Substract the number of days determined by the weekday t[6] and set time 188: # to midnight of that day. 189: (TjTime.new(Time.local(*t)) - 190: (weekday - (startMonday ? 1 : 0)) * 60 * 60 * 24).midnight 191: end
Normalize time to the beginning of the current year.
# File lib/TjTime.rb, line 215 215: def beginOfYear 216: t = @time.localtime.to_a 217: t[0, 3] = Array.new(3, 0) 218: t[3, 2] = Array.new(2, 1) 219: t.slice!(6, 4) 220: t.reverse! 221: TjTime.new(Time.local(*t)) 222: end
Return the number of days between this time and date. The result is always rounded up.
# File lib/TjTime.rb, line 306 306: def daysTo(date) 307: countIntervals(date, :sameTimeNextDay) 308: end
Return a new time that is hours later than time.
# File lib/TjTime.rb, line 225 225: def hoursLater(hours) 226: TjTime.new(@time + hours * 3600) 227: end
Return the number of hours between this time and date. The result is always rounded up.
# File lib/TjTime.rb, line 299 299: def hoursTo(date) 300: t1, t2 = order(date) 301: ((t2 - t1) / 3600).ceil 302: end
Pass any unknown function directoy to the @time variable.
# File lib/TjTime.rb, line 395 395: def method_missing(func, *args) 396: @time.method(func).call(*args) 397: end
Normalize time to the beginning of the current day.
# File lib/TjTime.rb, line 170 170: def midnight 171: t = @time.localtime.to_a 172: t[0, 3] = Array.new(3, 0) 173: t.slice!(6, 4) 174: t.reverse! 175: TjTime.new(Time.local(*t)) 176: end
Return the abbreviated month name and the full year. E. g. ‘Feb 1972’.
# File lib/TjTime.rb, line 372 372: def monthAndYear 373: @time.strftime('%b %Y') 374: end
Return the number of months between this time and date. The result is always rounded up.
# File lib/TjTime.rb, line 318 318: def monthsTo(date) 319: countIntervals(date, :sameTimeNextMonth) 320: end
Return the start of the next dow day of week after date. dow must be 0 for Sundays, 1 for Mondays and 6 for Saturdays. If date is a Tuesday and dow is 5 (Friday) the date of next Friday 0:00 will be returned. If date is a Tuesday and dow is 2 (Tuesday) the date of the next Tuesday will be returned.
# File lib/TjTime.rb, line 386 386: def nextDayOfWeek(dow) 387: raise "Day of week must be 0 - 6." unless dow >= 0 && dow <= 6 388: d = midnight.sameTimeNextDay 389: currentDoW = d.strftime('%w').to_i 390: 1.upto((dow + 7 - currentDoW) % 7) { |i| d = d.sameTimeNextDay } 391: d 392: end
Return the number of the quarter prefixed by a ‘Q’.
# File lib/TjTime.rb, line 361 361: def quarterName 362: "Q#{(@time.mon / 3) + 1}" 363: end
Return the number of quarters between this time and date. The result is always rounded up.
# File lib/TjTime.rb, line 324 324: def quartersTo(date) 325: countIntervals(date, :sameTimeNextQuarter) 326: end
Return a new time that is 1 day later than time but at the same time of day.
# File lib/TjTime.rb, line 236 236: def sameTimeNextDay 237: delta = [ 0, 1, 1 ] 238: localT1 = @time.localtime.to_a 239: delta.each do |d| 240: t = @time + (24 + d) * 60 * 60 241: localT2 = t.localtime.to_a 242: return TjTime.new(t) if localT1[0, 3] == localT2[0, 3] 243: end 244: raise "Algorithm is broken for #{@time}" 245: end
Return a new time that is 1 hour later than time.
# File lib/TjTime.rb, line 230 230: def sameTimeNextHour 231: hoursLater(1) 232: end
Return a new time that is 1 month later than time but at the same time of day.
# File lib/TjTime.rb, line 262 262: def sameTimeNextMonth 263: sec, min, hour, day, month, year = @time.localtime.to_a 264: monMax = month == 2 && leapYear?(year) ? 29 : MON_MAX[month] 265: month += 1 266: if month > 12 267: month = 1 268: year += 1 269: end 270: day = monMax if day >= monMax 271: TjTime.new(Time.mktime(year, month, day, hour, min, sec, 0)) 272: end
Return a new time that is 1 quarter later than time but at the same time of day.
# File lib/TjTime.rb, line 276 276: def sameTimeNextQuarter 277: t = @time.localtime.to_a 278: if (t[4] += 3) > 12 279: t[4] -= 12 280: t[5] += 1 281: end 282: t.slice!(6, 4) 283: t.reverse! 284: TjTime.new(Time.local(*t)) 285: end
Return a new time that is 1 week later than time but at the same time of day.
# File lib/TjTime.rb, line 249 249: def sameTimeNextWeek 250: delta = [ 0, 1, 1 ] 251: localT1 = @time.localtime.to_a 252: delta.each do |d| 253: t = @time + (7 * 24 + d) * 60 * 60 254: localT2 = t.localtime.to_a 255: return TjTime.new(t) if localT1[0, 3] == localT2[0, 3] 256: end 257: raise "Algorithm is broken for #{@time}" 258: end
Return a new time that is 1 year later than time but at the same time of day.
# File lib/TjTime.rb, line 289 289: def sameTimeNextYear 290: t = @time.localtime.to_a 291: t[5] += 1 292: t.slice!(6, 4) 293: t.reverse! 294: TjTime.new(Time.local(*t)) 295: end
Returns the total number of seconds of the day. The time is assumed to be in the time zone specified by tz.
# File lib/TjTime.rb, line 94 94: def secondsOfDay(tz = nil) 95: # TODO: Add timezone support 96: (@time.to_i + @time.gmt_offset) % (60 * 60 * 24) 97: end
Return the abbreviated month name.
# File lib/TjTime.rb, line 356 356: def shortMonthName 357: @time.strftime('%b') 358: end
Return the seconds since Epoch.
# File lib/TjTime.rb, line 346 346: def to_i 347: @time.to_i 348: end
This function is just a wrapper around Time.strftime(). In case @time is nil, it returns ‘unkown’.
# File lib/TjTime.rb, line 336 336: def to_s(format = nil) 337: return 'unknown' if @time.nil? 338: if format.nil? 339: format = '%Y-%m-%d-%H:%M' + (@time.sec == 0 ? '' : ':%S') + '-%z' 340: end 341: # Always report values in local timezone 342: @time.dup.localtime.strftime(format) 343: end
Iterator that executes the block until time has reached endDate increasing time by step on each iteration.
# File lib/TjTime.rb, line 152 152: def upto(endDate, step = 1) 153: t = @time 154: while t < endDate.time 155: yield(TjTime.new(t)) 156: t += step 157: end 158: end
Return the week number. weekStartsMonday specifies wheter the counting should be for weeks starting Mondays or Sundays.
# File lib/TjTime.rb, line 367 367: def week(weekStartsMonday) 368: @time.strftime(weekStartsMonday ? '%W' : '%U') 369: end
Return the abbreviated weekday and the full date. E. g. ‘Sat 2007-11-03’.
# File lib/TjTime.rb, line 377 377: def weekdayAndDate 378: @time.strftime('%A %Y-%m-%d') 379: end
# File lib/TjTime.rb, line 412 412: def countIntervals(date, stepFunc) 413: i = 0 414: t1, t2 = order(date) 415: while t1 < t2 416: t1 = t1.send(stepFunc) 417: i += 1 418: end 419: i 420: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.