Skip to content

Commit ad4ca4c

Browse files
committed
Fix time() on 1.8.7
The time() function takes an argument of a timezone, and always returns time in epoch format. The epoch format is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds. This means that it is universally the same regardless of timezones. I don't know what the timezone argument is supposed to do, and it is not documented. So lets just make 1.8.7 work like > 1.8.7
1 parent f5f72f4 commit ad4ca4c

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

lib/puppet/parser/functions/time.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ module Puppet::Parser::Functions
3333

3434
ENV['TZ'] = time_zone
3535

36-
time = local_time.localtime
36+
result = local_time.localtime.strftime('%s')
3737

3838
ENV['TZ'] = original_zone
39+
else
40+
result = time.localtime.strftime('%s')
3941
end
4042

4143
# Calling Time#to_i on a receiver changes it. Trust me I am the Doctor.
42-
result = time.strftime('%s')
4344
result = result.to_i
4445

4546
return result

spec/functions/time_spec.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
context 'when running at a specific time' do
88
before(:each) {
99
# get a value before stubbing the function
10-
test_time = Time.utc(2006, 10, 13, 8, 15, 11, '+01:00')
10+
test_time = Time.utc(2006, 10, 13, 8, 15, 11)
1111
Time.expects(:new).with().returns(test_time).once
1212
}
1313
it { is_expected.to run.with_params().and_return(1160727311) }
@@ -16,13 +16,6 @@
1616
it { is_expected.to run.with_params({}).and_return(1160727311) }
1717
it { is_expected.to run.with_params('foo').and_return(1160727311) }
1818
it { is_expected.to run.with_params('UTC').and_return(1160727311) }
19-
20-
context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
21-
it { is_expected.to run.with_params('America/Los_Angeles').and_return(1160727311) }
22-
end
23-
24-
context 'when running on ruby 1.8.7, which garbles the TZ', :if => RUBY_VERSION == '1.8.7' do
25-
it { is_expected.to run.with_params('America/Los_Angeles').and_return(1160702111) }
26-
end
19+
it { is_expected.to run.with_params('America/New_York').and_return(1160727311) }
2720
end
2821
end

0 commit comments

Comments
 (0)