# \[PATCH 0/2\] Fix strptime '%s'

**URL:** <https://rubytalk.org/t/patch-0-2-fix-strptime-s/68558>\
**Category:** ruby-talk\
**Created:** [7 October 2013 01:13 UTC](https://rubytalk.org/t/patch-0-2-fix-strptime-s/68558 "2013-10-07T01:13:40Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Felipe\_Contreras](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/felipe_contreras/32/2355_2.png) [@Felipe\_Contreras](https://rubytalk.org/u/Felipe_Contreras)\
**Post date:** [7 October 2013 01:13 UTC](https://rubytalk.org/t/patch-0-2-fix-strptime-s/68558/2 "2013-10-07T01:13:41Z")

</div>

'%s' is meant to imply UTC, however:

&nbsp;&nbsp;Time.strptime('0', '%s')  
&nbsp;&nbsp;=\> 1969-12-31 18:00:00 -0600

After this patch:

&nbsp;&nbsp;Time.strptime('0', '%s')  
&nbsp;&nbsp;=\> 1970-01-01 00:00:00 +0000

In addition, '%s %z' is parsed correctly:

&nbsp;&nbsp;Time.strptime('0 +0100', '%s %z').strftime('%s %z')  
&nbsp;&nbsp;=\> "0 -0600"

Now:

&nbsp;&nbsp;Time.strptime('0 +0100', '%s %z').strftime('%s %z')  
&nbsp;&nbsp;=\> "0 +0100"

Signed-off-by: Felipe Contreras \<[felipe.contreras@gmail.com](mailto:felipe.contreras@gmail.com)\>

> **···**
>
> ---  
> lib/time.rb | 3 ++-  
> test/test\_time.rb | 2 ++  
> 2 files changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/time.rb b/lib/time.rb  
> index 91b5b54..4474f27 100644  
> --- a/lib/time.rb  
> +++ b/lib/time.rb  
> @@ -393,7 +393,8 @@ class Time  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d = Date.\_strptime(date, format)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise ArgumentError, "invalid strptime format - `#{format}'" unless d  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if seconds = d[:seconds]  
> - [Time.at](http://Time.at)(seconds)  
> + offset = d[:offset] || 0  
> + [Time.at](http://Time.at)(seconds).localtime(offset)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;year = d[:year]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;year = yield(year) if year && block\_given?  
> diff --git a/test/test\_time.rb b/test/test\_time.rb  
> index 0a4e8a7..1c95370 100644  
> --- a/test/test\_time.rb  
> +++ b/test/test\_time.rb  
> @@ -400,6 +400,8 @@ class TestTimeExtension \< Test::Unit::TestCase # :nodoc:  
> &nbsp;&nbsp;&nbsp;def test\_strptime  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(Time.utc(2005, 8, 28, 06, 54, 20), Time.strptime("28/Aug/2005:06:54:20 +0000", "%d/%b/%Y:%T %z"))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal([Time.at](http://Time.at)(1).localtime, Time.strptime("1", "%s"))  
> + assert\_equal(0, Time.strptime('0', '%s').utc\_offset)  
> + assert\_equal(3600, Time.strptime('0 +0100', '%s %z').utc\_offset)  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;def test\_nsec  
> --  
> 1.8.4-fc

---

_[View the full topic](https://rubytalk.org/t/patch-0-2-fix-strptime-s/68558)._
