]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
Fix time() on 1.8.7
authorHunter Haugen <hunter@puppetlabs.com>
Thu, 4 Jun 2015 16:40:52 +0000 (09:40 -0700)
committerHunter Haugen <hunter@puppetlabs.com>
Thu, 4 Jun 2015 16:40:52 +0000 (09:40 -0700)
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

lib/puppet/parser/functions/time.rb
spec/functions/time_spec.rb

index 0cddaf86b9341cd4b2ed21fd68bafdce83914ce2..c5747474f5fd3e5ac26eefde4f0a3a98d928bf1a 100644 (file)
@@ -33,13 +33,14 @@ Will return something like: 1311972653
 
       ENV['TZ'] = time_zone
 
-      time = local_time.localtime
+      result = local_time.localtime.strftime('%s')
 
       ENV['TZ'] = original_zone
+    else
+      result = time.localtime.strftime('%s')
     end
 
     # Calling Time#to_i on a receiver changes it.  Trust me I am the Doctor.
-    result = time.strftime('%s')
     result = result.to_i
 
     return result
index 2875e25ac7a84ff463f49f589d755ab01a804fad..d157939e9ea2e02fdfb51bdfdc3958f98c15b17f 100755 (executable)
@@ -7,7 +7,7 @@ describe 'time' do
   context 'when running at a specific time' do
     before(:each) {
       # get a value before stubbing the function
-      test_time = Time.utc(2006, 10, 13, 8, 15, 11, '+01:00')
+      test_time = Time.utc(2006, 10, 13, 8, 15, 11)
       Time.expects(:new).with().returns(test_time).once
     }
     it { is_expected.to run.with_params().and_return(1160727311) }
@@ -16,13 +16,6 @@ describe 'time' do
     it { is_expected.to run.with_params({}).and_return(1160727311) }
     it { is_expected.to run.with_params('foo').and_return(1160727311) }
     it { is_expected.to run.with_params('UTC').and_return(1160727311) }
-
-    context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
-      it { is_expected.to run.with_params('America/Los_Angeles').and_return(1160727311) }
-    end
-
-    context 'when running on ruby 1.8.7, which garbles the TZ', :if => RUBY_VERSION == '1.8.7' do
-      it { is_expected.to run.with_params('America/Los_Angeles').and_return(1160702111) }
-    end
+    it { is_expected.to run.with_params('America/New_York').and_return(1160727311) }
   end
 end