Class: Time
Constant Summary collapse
- TO_NICE_S_MAX_LEN =
e.g. “Yest.10am”
9
Instance Method Summary collapse
- #default_to_nice_s(from = Time.now) ⇒ Object
- #is_the_day_before?(other) ⇒ Boolean
- #is_the_same_day?(other) ⇒ Boolean
-
#midnight ⇒ Object
within a second.
- #nearest_hour ⇒ Object
- #to_indexable_s ⇒ Object
- #to_nice_distance_s(from = Time.now) ⇒ Object
- #to_nice_s(from = Time.now) ⇒ Object
Instance Method Details
#default_to_nice_s(from = Time.now) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sup/time.rb', line 67 def default_to_nice_s from=Time.now if year != from.year strftime "%b %Y" elsif month != from.month strftime "%b %e" else if is_the_same_day? from strftime("%l:%M%p").downcase # emulate %P (missing on ruby 1.8 darwin) elsif is_the_day_before? from "Yest." + nearest_hour.strftime("%l%p").downcase # emulate %P else strftime "%b %e" end end end |
#is_the_day_before?(other) ⇒ Boolean
30 31 32 |
# File 'lib/sup/time.rb', line 30 def is_the_day_before? other other.midnight - midnight <= 24 * 60 * 60 + 1 end |
#is_the_same_day?(other) ⇒ Boolean
26 27 28 |
# File 'lib/sup/time.rb', line 26 def is_the_same_day? other (midnight - other.midnight).abs < 1 end |
#midnight ⇒ Object
within a second
22 23 24 |
# File 'lib/sup/time.rb', line 22 def midnight # within a second self - (hour * 60 * 60) - (min * 60) - sec end |
#nearest_hour ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/sup/time.rb', line 14 def nearest_hour if min < 30 self else self + (60 - min) * 60 end end |
#to_indexable_s ⇒ Object
10 11 12 |
# File 'lib/sup/time.rb', line 10 def to_indexable_s sprintf "%012d", self end |
#to_nice_distance_s(from = Time.now) ⇒ Object
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 |
# File 'lib/sup/time.rb', line 34 def to_nice_distance_s from=Time.now later_than = (self < from) diff = (self.to_i - from.to_i).abs.to_f text = [ ["second", 60], ["minute", 60], ["hour", 24], ["day", 7], ["week", 4.345], # heh heh ["month", 12], ["year", nil], ].argfind do |unit, size| if diff.round <= 1 "one #{unit}" elsif size.nil? || diff.round < size "#{diff.round} #{unit}s" else diff /= size.to_f false end end if later_than text + " ago" else "in " + text end end |
#to_nice_s(from = Time.now) ⇒ Object
63 64 65 |
# File 'lib/sup/time.rb', line 63 def to_nice_s from=Time.now Redwood::HookManager.run("time-to-nice-string", :time => self, :from => from) || default_to_nice_s(from) end |