From be5de490906e1b2b542ba6e0160672f92a24ab60 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 21 Feb 2014 07:45:55 +0000 Subject: logger.rb: DST * lib/logger.rb (next_rotate_time, previous_period_end): consider DST change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45072 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 ++++- lib/logger.rb | 17 +++++++---------- test/logger/test_logdevice.rb | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9af231e766..9dd67f27e8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,7 @@ -Fri Feb 21 16:11:02 2014 Nobuyoshi Nakada +Fri Feb 21 16:45:54 2014 Nobuyoshi Nakada + + * 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 diff --git a/lib/logger.rb b/lib/logger.rb index ef9c75d068..ca667f48b4 100644 --- a/lib/logger.rb +++ b/lib/logger.rb @@ -714,29 +714,26 @@ private 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 diff --git a/test/logger/test_logdevice.rb b/test/logger/test_logdevice.rb index 3db3a5da45..0245d03eba 100644 --- a/test/logger/test_logdevice.rb +++ b/test/logger/test_logdevice.rb @@ -362,6 +362,44 @@ class TestLogDevice < Test::Unit::TestCase 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) -- cgit v1.2.3