Class: Time
- Inherits:
-
Object
- Object
- Time
- Defined in:
- lib/texmailer/drafts.rb
Overview
Borrowed from sup
Constant Summary collapse
- TO_NICE_S_MAX_LEN =
e.g. “Yest.10am”
9
Instance Method Summary collapse
- #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
#is_the_day_before?(other) ⇒ Boolean
287 288 289 |
# File 'lib/texmailer/drafts.rb', line 287 def is_the_day_before? other other.midnight - midnight <= 24 * 60 * 60 + 1 end |
#is_the_same_day?(other) ⇒ Boolean
283 284 285 |
# File 'lib/texmailer/drafts.rb', line 283 def is_the_same_day? other (midnight - other.midnight).abs < 1 end |
#midnight ⇒ Object
within a second
279 280 281 |
# File 'lib/texmailer/drafts.rb', line 279 def midnight # within a second self - (hour * 60 * 60) - (min * 60) - sec end |
#nearest_hour ⇒ Object
271 272 273 274 275 276 277 |
# File 'lib/texmailer/drafts.rb', line 271 def nearest_hour if min < 30 self else self + (60 - min) * 60 end end |
#to_indexable_s ⇒ Object
267 268 269 |
# File 'lib/texmailer/drafts.rb', line 267 def to_indexable_s sprintf "%012d", self end |
#to_nice_distance_s(from = Time.now) ⇒ Object
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 |
# File 'lib/texmailer/drafts.rb', line 291 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
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/texmailer/drafts.rb', line 320 def 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 |