From 2dc0e9bd872a59205e597a8c9a1e6f340f5c86a2 Mon Sep 17 00:00:00 2001 From: Benjamin Kellermann Date: Wed, 1 Jul 2009 22:47:10 +0200 Subject: first try to implement DateTime --- datepoll.rb | 150 ++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 121 insertions(+), 29 deletions(-) (limited to 'datepoll.rb') diff --git a/datepoll.rb b/datepoll.rb index 5d78cec..d16a225 100644 --- a/datepoll.rb +++ b/datepoll.rb @@ -6,57 +6,86 @@ require "date" require "poll" +load "time.rb" class DatePoll < Poll + def init + #FIXME: quick 'n' dirty hack, because Time <=> Date is not possible and yaml loads Time instead of DateTime! + #better solution would be to overwrite <=> + @head.each{|k,v| + if k.class == Time + @head.delete(k) + @head[DateTime.parse(k.to_s)]=v + end + } + end def sort_data fields datefields = fields.collect{|field| - field == "timestamp" || field == "name" ? field : Date.parse(field) + field == "timestamp" || field == "name" ? field : DateTime.parse(field) } super datefields end - def head_to_html(config = false) - ret = "\n" - monthhead = Hash.new(0) - @head.sort.each{|curdate,curdescription| - monthhead["#{curdate.year}-#{curdate.mon.to_s.rjust(2,"0")} "] += 1 + # returns a sorted array, containing the big units and how often each small is in the big one + # small and big must be formated for strftime + # ex: head_count("%Y-%m", "-%d") returns an array like [["2009-03",2],["2009-04",3]] + def head_count(big,small) + ret = Hash.new(0) + @head.keys.collect{|curdate| + DateTime.parse(curdate.strftime(big + small)) + }.uniq.each{|day| + ret[day.strftime(big)] += 1 } - monthhead.sort.each{|title,count| + ret.sort + end + + def head_to_html(config = false) + ret = "" + head_count("%Y-%m","-%d %H:%M%Z").each{|title,count| year, month = title.split("-").collect{|e| e.to_i} ret += "#{Date::ABBR_MONTHNAMES[month]} #{year}\n" } - ret += "Name\n" - @head.sort.each{|curdate,curdescription| - ret += "#{Date::ABBR_DAYNAMES[curdate.wday]}, #{curdate.day}\n" + ret += "" + head_count("%Y-%m-%d", " %H:%M%Z").each{|title,count| + curdate = Date.parse(title) + ret += "#{curdate.strftime("%a, %d")}\n" + } + ret += "Name" + @head.keys.sort.each{|curdate| + if curdate.class == Date + ret += "---\n" + else + ret += "#{curdate.strftime("%H:%M")}\n" + end } - ret += "Last Edit\n" - ret += "\n" - ret + ret += "Last Edit" + ret end def add_remove_column_htmlform if $cgi.include?("add_remove_column_month") begin - startdate = Date.parse("#{$cgi["add_remove_column_month"]}-1") + startdate = DateTime.parse("#{$cgi["add_remove_column_month"]}-1") rescue ArgumentError olddate = $cgi.params["add_remove_column_month"][1] case $cgi["add_remove_column_month"] when CGI.unescapeHTML(YEARBACK) - startdate = Date.parse("#{olddate}-1")-365 + startdate = DateTime.parse("#{olddate}-1")-365 when CGI.unescapeHTML(MONTHBACK) - startdate = Date.parse("#{olddate}-1")-1 + startdate = DateTime.parse("#{olddate}-1")-1 when CGI.unescapeHTML(MONTHFORWARD) - startdate = Date.parse("#{olddate}-1")+31 + startdate = DateTime.parse("#{olddate}-1")+31 when CGI.unescapeHTML(YEARFORWARD) - startdate = Date.parse("#{olddate}-1")+366 + startdate = DateTime.parse("#{olddate}-1")+366 else exit end - startdate = Date.parse("#{startdate.year}-#{startdate.month}-1") + startdate = DateTime.parse(startdate.strftime("%Y-%m-1")) end else - startdate = Date.parse("#{Date.today.year}-#{Date.today.month}-1") + startdate = DateTime.parse(Date.today.strftime("%Y-%m-1")) end ret = < +
+
END def navi val @@ -70,7 +99,7 @@ END ret += "\n" - 7.times{|i| ret += "" } + 7.times{|i| ret += "" } ret += "\n" ((startdate.wday+7-1)%7).times{ @@ -89,16 +118,78 @@ END
#{Date::ABBR_DAYNAMES[(i+1)%7]}#{Date::ABBR_DAYNAMES[(i+1)%7]}
+
+END + + ret += "
" + + head_count("%Y-%m", "-%d").each{|title,count| + year,month = title.split("-").collect{|e| e.to_i} + ret += "\n" + } + + ret += "" + + head_count("%Y-%m-%d","").each{|title,count| + curdate = Date.parse(title) + ret += "\n" + } + + ret += "" + + ["00:00", "10:00","13:00","14:00","20:00"].each{|time| + ret +="\n" + @head.sort.collect{|day,descr| + Date.parse(day.strftime("%Y-%m-%d")) + }.uniq.each{|date| + timestamp = DateTime.parse("#{date} #{time} #{Time.now.zone}") + klasse = "notchoosen" + klasse = "disabled" if timestamp < DateTime.now + klasse = "choosen" if @head.include?(timestamp) + ret += < +
+
+ + + + +
+ + +END + } + ret += "
\n" + } + ret += < +
+
+ + +
+ + END ret end - def add_remove_column name,description - begin - parsed_name = Date.parse("#{$cgi["add_remove_column_month"]}-#{name}") - rescue ArgumentError - return false + def add_remove_column col,description + if $cgi.include?("add_remove_column_day") + begin + parsed_date = YAML::load(DateTime.parse("#{$cgi["add_remove_column_month"]}-#{$cgi["add_remove_column_day"]} #{col} #{Time.now.zone}").to_yaml) + day = Date.parse(parsed_date.to_s) + @head.delete(day) if @head.include?(day) + rescue ArgumentError + return false + end + else + begin + parsed_date = YAML::load(Date.parse("#{$cgi["add_remove_column_month"]}-#{col}").to_yaml) + rescue ArgumentError + return false + end end - add_remove_parsed_column(parsed_name,CGI.escapeHTML(description)) + add_remove_parsed_column(parsed_date,CGI.escapeHTML(description)) end end @@ -119,8 +210,9 @@ $cgi = CGI.new class DatePollTest < Test::Unit::TestCase def setup - @poll = DatePoll.new(SITE, false) + @poll = DatePoll.new(SITE) end + #TODO def test_add_remove_column assert(!@poll.add_remove_column("foo", "bar")) assert(!@poll.add_remove_column("31", "31.02.2008 ;--)")) -- cgit v1.2.3
#{Date::ABBR_MONTHNAMES[month]} #{year}
#{curdate.strftime("%a, %d")}