Bug with strptime('%s %z') and patch fix available

Hi,

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:

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 [email protected] 1377283786 -0700

Which correspondes exactly to ‘%s %z’.

Other languages also work correctly:

C:

#define _XOPEN_SOURCE
#include <stdio.h>
#include <time.h>

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

Python doesn’t allow the ‘%s’ format at all, but at least they warn the
user
about that, Ruby just fails silently with ‘%s %z’.

Unfortunately the discussion in bug #7445 didn’t lead anywhere.

I’m hoping you can take a look at this with fresh eyes and realize it’s
in the
best interest of everyone to apply this patch.

Can we just apply it?

Cheers.