rubysl/rubysl-date — pull request #1 by felipec (2013-10-05 22:54:58+00:00)
Title: Improve DateTime strptime

Hi,

I start with a bit of cleanup, and then I attempt to fix strptime('%s %z').

The issue is very simple; DateTime.strptime('%s %z') doesn't work correctly:

---
require 'date'
date = DateTime.strptime('1377283786 -0700', '%s %z')
puts date.strftime('%s %z')
---
1377283786 +0000

Fortunately there's a patch available:

https://bugs.ruby-lang.org/attachments/3307/bug-7445.patch

After applying this patch, Ruby returns the right date ('1377283786 -0700').

Applying this patch wouldn't affect anybody negatively, because the people that
use '%s' and assume it's UTC, can continue to do so. So why not apply it?

This format is used exensively by Git. If you look at a git commit object (git
cat-file -p), you find this:

author Junio C Hamano pobox.com> 1377283786 -0700

Which correspondes exactly to '%s %z'.

Other languages also work correctly:

C:
---
#define _XOPEN_SOURCE
#include
#include

int main(int argc, char *argv[]) {
struct tm tm;
char buf[0x100];
strptime("1377283786 -0700", "%s %z", &tm);
strftime(buf, sizeof(buf), "%s %z", &tm);
printf("%s\n", buf);
return 0;
}
---
1377283786 -0700

Perl:
---
use DateTime::Format::Strptime;

my $format = DateTime::Format::Strptime->new(
pattern => '%s %z',
);

my $dt = $format->parse_datetime('1377283786 -0700');
print $format->format_datetime($dt), "\n";
---
1377283786 -0700