* [sup-devel] Patch: Hookify Time#to_nice_s
@ 2011-01-24 23:24 Tero Tilus
0 siblings, 0 replies; only message in thread
From: Tero Tilus @ 2011-01-24 23:24 UTC (permalink / raw)
To: Sup developers
[-- Attachment #1: Type: text/plain, Size: 597 bytes --]
I noticed that my index-mode-date-widget hook patch (22c7a51675d8) was
not quite there. This is what it should have been from the beginning.
This renders index-mode-date-widget hook obsolete, since
Time#to_nice_s is what index-mode defaults to when there is no hook
defined. Unless of course you want "nice time" to be something else
on index-mode than everywhere else, which I don't think anybody wants.
Is this good as is or should I in this patch do something about the
now obsolete index-mode-date-widget hook? Deprecate somehow?
--
Tero Tilus ## 050 3635 235 ## http://tero.tilus.net/
[-- Attachment #2: 0001-Hookify-Time-to_nice_s.patch --]
[-- Type: application/octet-stream, Size: 4986 bytes --]
From 180e238c0069df058a5db28a03ddaf86a49efe56 Mon Sep 17 00:00:00 2001
From: Tero Tilus <tero@tilus.net>
Date: Wed, 22 Dec 2010 00:55:31 +0200
Subject: [PATCH] Hookify Time#to_nice_s
Signed-off-by: Tero Tilus <tero@tilus.net>
---
lib/sup.rb | 2 +
lib/sup/time.rb | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/sup/util.rb | 71 -----------------------------------------------
3 files changed, 85 insertions(+), 71 deletions(-)
create mode 100644 lib/sup/time.rb
diff --git a/lib/sup.rb b/lib/sup.rb
index 74eb950..520a072 100644
--- a/lib/sup.rb
+++ b/lib/sup.rb
@@ -256,6 +256,7 @@ try this instead:
You can also try `gem uninstall sup` and removing all Sup rubygems.
EOS
+#' duh!
abort
end
end
@@ -324,6 +325,7 @@ end
require "sup/util"
require "sup/hook"
+require "sup/time"
## everything we need to get logging working
require "sup/logger"
diff --git a/lib/sup/time.rb b/lib/sup/time.rb
new file mode 100644
index 0000000..c076e19
--- /dev/null
+++ b/lib/sup/time.rb
@@ -0,0 +1,83 @@
+class Time
+
+ Redwood::HookManager.register "time-to-nice-string", <<EOS
+Formats time nicely as string.
+Variables:
+ time: The Time instance to be formatted.
+ from: The Time instance providing the reference point (considered "now").
+EOS
+
+ def to_indexable_s
+ sprintf "%012d", self
+ end
+
+ def nearest_hour
+ if min < 30
+ self
+ else
+ self + (60 - min) * 60
+ end
+ end
+
+ def midnight # within a second
+ self - (hour * 60 * 60) - (min * 60) - sec
+ end
+
+ def is_the_same_day? other
+ (midnight - other.midnight).abs < 1
+ end
+
+ def is_the_day_before? other
+ other.midnight - midnight <= 24 * 60 * 60 + 1
+ end
+
+ 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_MAX_LEN = 9 # e.g. "Yest.10am"
+ def to_nice_s from=Time.now
+ Redwood::HookManager.run("time-to-nice-string", :time => self, :from => from) || default_to_nice_s(from)
+ end
+
+ 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
+end
+
diff --git a/lib/sup/util.rb b/lib/sup/util.rb
index 45dec3b..2d52871 100644
--- a/lib/sup/util.rb
+++ b/lib/sup/util.rb
@@ -515,77 +515,6 @@ class Array
def nonempty?; !empty? end
end
-class Time
- def to_indexable_s
- sprintf "%012d", self
- end
-
- def nearest_hour
- if min < 30
- self
- else
- self + (60 - min) * 60
- end
- end
-
- def midnight # within a second
- self - (hour * 60 * 60) - (min * 60) - sec
- end
-
- def is_the_same_day? other
- (midnight - other.midnight).abs < 1
- end
-
- def is_the_day_before? other
- other.midnight - midnight <= 24 * 60 * 60 + 1
- end
-
- 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_MAX_LEN = 9 # e.g. "Yest.10am"
- 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
-end
-
## simple singleton module. far less complete and insane than the ruby standard
## library one, but it automatically forwards methods calls and allows for
## constructors that take arguments.
--
1.5.6.5
[-- Attachment #3: Type: text/plain, Size: 143 bytes --]
_______________________________________________
Sup-devel mailing list
Sup-devel@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-devel
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2011-01-24 23:32 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-24 23:24 [sup-devel] Patch: Hookify Time#to_nice_s Tero Tilus
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox