diff --git a/lib/tzinfo/timezone.rb b/lib/tzinfo/timezone.rb index 518fe220..314ab6d3 100644 --- a/lib/tzinfo/timezone.rb +++ b/lib/tzinfo/timezone.rb @@ -573,15 +573,19 @@ def current_period_and_time # depending on the version of Ruby in use (for example, %:::z is only # supported by Time#strftime from MRI version 2.0.0 onwards.) def strftime(format, utc = Time.now.utc) + utc = TimeOrDateTime.wrap(utc) period = period_for_utc(utc) - local = period.to_local(utc) - local = Time.at(local).utc unless local.kind_of?(Time) || local.kind_of?(DateTime) + local_wrapped = period.to_local(utc) + local = local_wrapped.to_orig + local = local_wrapped.to_time unless local.kind_of?(Time) || local.kind_of?(DateTime) abbreviation = period.abbreviation.to_s.gsub(/%/, '%%') - format = format.gsub(/%(%*)(Z|:*z)/) do + format = format.gsub(/%(%*)([sZ]|:*z)/) do if $1.length.odd? # Escaped literal percent or series of percents. Pass on to strftime. "#$1%#$2" + elsif $2 == "s" + "#$1#{utc.to_i}" elsif $2 == "Z" "#$1#{abbreviation}" else diff --git a/test/tc_timezone.rb b/test/tc_timezone.rb index a4302bd2..e7f3cf4a 100644 --- a/test/tc_timezone.rb +++ b/test/tc_timezone.rb @@ -1260,6 +1260,7 @@ def test_strftime_datetime assert_equal('BST BST', tz.strftime('%Z %Z', dt)) assert_equal('BST %Z %BST %%Z %%BST', tz.strftime('%Z %%Z %%%Z %%%%Z %%%%%Z', dt)) assert_equal('+0100 +01:00 +01:00:00 +01 %::::z', tz.strftime('%z %:z %::z %:::z %::::z', dt)) + assert_equal('1153001522 %s %1153001522', tz.strftime('%s %%s %%%s', dt)) end def test_strftime_time @@ -1271,6 +1272,7 @@ def test_strftime_time assert_equal('BST BST', tz.strftime('%Z %Z', t)) assert_equal('BST %Z %BST %%Z %%BST', tz.strftime('%Z %%Z %%%Z %%%%Z %%%%%Z', t)) assert_equal('+0100 +01:00 +01:00:00 +01 %::::z', tz.strftime('%z %:z %::z %:::z %::::z', t)) + assert_equal('1153001522 %s %1153001522', tz.strftime('%s %%s %%%s', t)) end def test_strftime_int @@ -1282,6 +1284,7 @@ def test_strftime_int assert_equal('BST BST', tz.strftime('%Z %Z', i)) assert_equal('BST %Z %BST %%Z %%BST', tz.strftime('%Z %%Z %%%Z %%%%Z %%%%%Z', i)) assert_equal('+0100 +01:00 +01:00:00 +01 %::::z', tz.strftime('%z %:z %::z %:::z %::::z', i)) + assert_equal('1153001522 %s %1153001522', tz.strftime('%s %%s %%%s', i)) end def test_get_missing_data_source