ruby-changes:32993
From: nobu <ko1@a...>
Date: Fri, 21 Feb 2014 16:45:59 +0900 (JST)
Subject: [ruby-changes:32993] nobu:r45072 (trunk): logger.rb: DST
nobu 2014-02-21 16:45:55 +0900 (Fri, 21 Feb 2014) New Revision: 45072 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45072 Log: logger.rb: DST * lib/logger.rb (next_rotate_time, previous_period_end): consider DST change. Modified files: trunk/ChangeLog trunk/lib/logger.rb trunk/test/logger/test_logdevice.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 45071) +++ ChangeLog (revision 45072) @@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 -Fri Feb 21 16:11:02 2014 Nobuyoshi Nakada <nobu@r...> +Fri Feb 21 16:45:54 2014 Nobuyoshi Nakada <nobu@r...> + + * lib/logger.rb (next_rotate_time, previous_period_end): consider + DST change. * lib/logger.rb (Logger::LogDevice#check_shift_log): compare the current time with the time for the next rotation to fix rotation Index: lib/logger.rb =================================================================== --- lib/logger.rb (revision 45071) +++ lib/logger.rb (revision 45072) @@ -714,29 +714,26 @@ private https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L714 when /^monthly$/ t = Time.mktime(now.year, now.month, 1) + SiD * 31 mday = (1 if t.mday > 1) - if mday - t = Time.mktime(t.year, t.month, mday) - end else return now end + if mday or t.hour.nonzero? or t.min.nonzero? or t.sec.nonzero? + t = Time.mktime(t.year, t.month, mday || (t.mday + (t.hour > 12 ? 1 : 0))) + end t end def previous_period_end(now, shift_age) case shift_age when /^daily$/ - eod(now - 1 * SiD) + t = Time.mktime(now.year, now.month, now.mday) - SiD / 2 when /^weekly$/ - eod(now - ((now.wday + 1) * SiD)) + t = Time.mktime(now.year, now.month, now.mday) - (SiD * (now.wday + 1) + SiD / 2) when /^monthly$/ - eod(now - now.mday * SiD) + t = Time.mktime(now.year, now.month, 1) - SiD / 2 else - now + return now end - end - - def eod(t) Time.mktime(t.year, t.month, t.mday, 23, 59, 59) end end Index: test/logger/test_logdevice.rb =================================================================== --- test/logger/test_logdevice.rb (revision 45071) +++ test/logger/test_logdevice.rb (revision 45072) @@ -362,6 +362,44 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logdevice.rb#L362 end end + def test_shifting_dst_change + Dir.mktmpdir do |tmpdir| + system({"TZ"=>"Europe/London"}, EnvUtil.rubybin, *%W"--disable=gems -rlogger -C#{tmpdir} -e", <<-'end;') + begin + module FakeTime + attr_accessor :now + end + + class << Time + prepend FakeTime + end + + log = "log" + File.open(log, "w") {} + + Time.now = Time.mktime(2014, 3, 30, 0, 1, 1) + File.utime(Time.now, Time.now, log) + + dev = Logger::LogDevice.new(log, shift_age: 'daily') + dev.write("#{Time.now} hello-1\n") + File.utime(*[Time.mktime(2014, 3, 30, 0, 2, 3)]*2, log) + + Time.now = Time.mktime(2014, 3, 31, 0, 1, 1) + File.utime(Time.now, Time.now, log) + dev.write("#{Time.now} hello-2\n") + ensure + dev.close + end + end; + + log = File.join(tmpdir, "log") + cont = File.read(log) + assert_match(/hello-2/, cont) + assert_not_match(/hello-1/, cont) + assert_file.exist?(log+".20140330") + end + end + private def run_children(n, args, src) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/