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

Feature request: Take base object as third argument #49

Open
Grinnz opened this issue May 11, 2020 · 4 comments
Open

Feature request: Take base object as third argument #49

Grinnz opened this issue May 11, 2020 · 4 comments

Comments

@Grinnz
Copy link
Contributor

Grinnz commented May 11, 2020

This idea came about in IRC discussion. Since the underlying OS strptime function allows passing a tm struct and (except on Solaris) will only overwrite the parts of the struct it parses out of the string, it would be nice to provide this capability with Time::Piece. This would provide an alternate solution for both #44 and #48.

my $tm = Time::Piece->strptime($str, $format, Time::Piece::localtime); # parse string as localtime, and use current date as default for any missing components
my $tm = Time::Piece->strptime($str, $format, Time::Piece::gmtime(0)); # current behavior-ish, using UTC and epoch 0 as base
@Grinnz
Copy link
Contributor Author

Grinnz commented May 11, 2020

There's the question whether this should modify the passed "base" object or just copy its components. I could see either case, in the underlying function it is modified directly, but it's also not really necessary at this level of API and it's more polite to leave inputs untouched. As long as it's documented whether it's modified or not.

@scottchiefbaker
Copy link

This is more wordy than #44. Both are decent solutions, I think a simple boolean solution like in #44 would be my preference.

@Grinnz
Copy link
Contributor Author

Grinnz commented May 11, 2020

The reasons I prefer this solution over the idea in #44 are: It's self documenting - it's not clear what a random boolean argument pertains to; it's more flexible in that it can solve the problem of #48 in a bugwards compatible way, and allow for potentially any other "starting point" object; and it's closer to the underlying strptime API that some may be used to.

@Aearsis
Copy link

Aearsis commented May 13, 2020

I would prefer generalizing the behavior for GMT flag (copy unspecified fields from base in $base->strptime(...)). But as this syntax is commonly used to parse localtimes, that would change behavior for a lot of existing code. This solution is a compromise, but an acceptable one.

I would certainly not modify the passed object.

For the starting point, I would suggest inheriting only more significant parts. I would not expect the result of strptime('13.5.', '%d.%m.') to have current time inside. The more when there is no easy way to get localtime with zeroed time... As the behavior of POSIX strptime is kinda undefined wrt initialization of missing parts, I think that this difference is acceptable.

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

No branches or pull requests

3 participants