sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/

lib/sup/time.rb (2232B) - raw

      1 class Time
      2 
      3   Redwood::HookManager.register "time-to-nice-string", <<EOS
      4 Formats time nicely as string.
      5 Variables:
      6   time: The Time instance to be formatted.
      7   from: The Time instance providing the reference point (considered "now").
      8 EOS
      9 
     10   def to_indexable_s
     11     sprintf "%012d", self
     12   end
     13 
     14   def nearest_hour
     15     if min < 30
     16       self
     17     else
     18       self + (60 - min) * 60
     19     end
     20   end
     21 
     22   def midnight # within a second
     23     self - (hour * 60 * 60) - (min * 60) - sec
     24   end
     25 
     26   def is_the_same_day? other
     27     (midnight - other.midnight).abs < 1
     28   end
     29 
     30   def is_the_day_before? other
     31     other.midnight - midnight <=  24 * 60 * 60 + 1
     32   end
     33 
     34   def to_nice_distance_s from=Time.now
     35     later_than = (self < from)
     36     diff = (self.to_i - from.to_i).abs.to_f
     37     text =
     38       [ ["second", 60],
     39         ["minute", 60],
     40         ["hour", 24],
     41         ["day", 7],
     42         ["week", 4.345], # heh heh
     43         ["month", 12],
     44         ["year", nil],
     45       ].argfind do |unit, size|
     46         if diff.round <= 1
     47           "one #{unit}"
     48         elsif size.nil? || diff.round < size
     49           "#{diff.round} #{unit}s"
     50         else
     51           diff /= size.to_f
     52           false
     53         end
     54       end
     55     if later_than
     56       text + " ago"
     57     else
     58       "in " + text
     59     end
     60   end
     61 
     62   TO_NICE_S_MAX_LEN = 9 # e.g. "Yest.10am"
     63 
     64   ## This is how a thread date is displayed in thread-index-mode
     65   def to_nice_s from=Time.now
     66     Redwood::HookManager.run("time-to-nice-string", :time => self, :from => from) || default_to_nice_s(from)
     67   end
     68 
     69   def default_to_nice_s from=Time.now
     70     if year != from.year
     71       strftime "%b %Y"
     72     elsif month != from.month
     73       strftime "%b %e"
     74     else
     75       if is_the_same_day? from
     76         format = $config[:time_mode] == "24h" ? "%k:%M" : "%l:%M%p"
     77         strftime(format).downcase
     78       elsif is_the_day_before? from
     79         format = $config[:time_mode] == "24h" ? "%kh" : "%l%p"
     80         "Yest." + nearest_hour.strftime(format).downcase
     81       else
     82         strftime "%b %e"
     83       end
     84     end
     85   end
     86 
     87   ## This is how a message date is displayed in thread-view-mode
     88   def to_message_nice_s from=Time.now
     89     format = $config[:time_mode] == "24h" ? "%B %e %Y %k:%M" : "%B %e %Y %l:%M%p"
     90     strftime format
     91   end
     92 end
     93