1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
nobu 2014-02-21 07:45:55 +00:00
parent 82d495134c
commit be5de49090
3 changed files with 49 additions and 11 deletions

View file

@ -1,4 +1,7 @@
Fri Feb 21 16:11:02 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
Fri Feb 21 16:45:54 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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

View file

@ -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

View file

@ -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)