Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support %- variant for strftime #87

Open
tbm opened this issue Jan 17, 2019 · 4 comments
Open

Support %- variant for strftime #87

tbm opened this issue Jan 17, 2019 · 4 comments

Comments

@tbm
Copy link

tbm commented Jan 17, 2019

I just found out that you can use %- to strip leading zeros, e.g.

$ date +%Y/%-m/%d
2019/1/16

Unfortunately, this is not supported by DateTime::Format::Strptime:

use DateTime::Format::Strptime qw(strftime);

my $date = DateTime::Format::Strptime->new(
    pattern  =>  "%Y/%-m/%d",
    on_error => "croak",
);

my $p = $date->parse_datetime("2015/5/30");
print strftime("%F", $p), "\n" if $p;
@autarch
Copy link
Member

autarch commented Jan 17, 2019

Hi Martin,

I've not seen this variation before and my system's strptime man page doesn't mention it. The strftime man page does document this as a glibc extension, saying:

       -      (dash) Do not pad a numeric result string.

There's really no need for this on the parsing side. The %m specifier will parse a value with or without a leading zero (as will similar specifier for days, hours, etc.).

The strftime exported by this package is just a small wrapper around DateTime's built-in strftime method. A patch to add support for this (ideally with the other glibc extensions as well) would be welcome.

I'm going to move this issue to the https://github.com/houseabsolute/DateTime.pm repo.

@autarch autarch transferred this issue from houseabsolute/DateTime-Format-Strptime Jan 17, 2019
@autarch autarch changed the title Support %- variant Support %- variant for strftime Jan 17, 2019
@tbm
Copy link
Author

tbm commented Jan 17, 2019

There's really no need for this on the parsing side. The %m specifier will parse a value with or without a leading zero

That's good to know. I didn't realize that but can confirm that it works.

However, if I use %-m the parser doesn't parse it, probably because it doesn't recognize %-m, so I'd say the parser still needs to be taught about the minus variant.

@autarch
Copy link
Member

autarch commented Jan 17, 2019

However, if I use %-m the parser doesn't parse it, probably because it doesn't recognize %-m, so I'd say the parser still needs to be taught about the minus variant.

But as I said, this is not a thing for the strptime function in glibc. I don't think it makes sense to deviate from that.

@tbm
Copy link
Author

tbm commented Jan 18, 2019

But as I said, this is not a thing for the strptime function in glibc. I don't think it makes sense to deviate from that.

Sorry, I didn't get what you were saying. I understand now. Thanks for the clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants