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

Have from_epoch() support just getting seconds passed #119

Closed
neilb opened this issue Mar 20, 2021 · 6 comments
Closed

Have from_epoch() support just getting seconds passed #119

neilb opened this issue Mar 20, 2021 · 6 comments

Comments

@neilb
Copy link

neilb commented Mar 20, 2021

It feels like more often than not, when I have epoch seconds and want a DateTime,
I write something like this:

$dt = DateTime->from_epoch( time() );

Then I get the error message, say "D'oh!", and add the epoch =>

The "epoch" parameter feels redundant, and I can't be the only one who does this? How about supporting both styles?

@autarch
Copy link
Member

autarch commented Mar 26, 2021

Yes, I think this would be possible. We'd need to check that @_ == 1 and that $_[0] is a valid Num. Are you up for writing a PR?

@neilb
Copy link
Author

neilb commented Apr 3, 2021

Are you up for writing a PR?

My first thought was "yeah", that should be easy — as you say,we just need to check @_ and valid number. How hard can that be? Then I looked at the code for from_epoch with its validation etc, and remembering there are other options allowed, so it's not such a quick fix, for me at least.

I've put it on my perl todo list, but TBH there are a lot of things ahead of it, so realistically not likely to happen any time soon, sadly.

@danstewart
Copy link

@autarch I've had a look at this but can't figure out how to validate with Params::ValidationCompiler, any pointers?

This seems to work:

my %p     = $_[0] =~ /^\d+$/ ? ( epoch => $_[0] ) : $validator->(@_);

Is there a smart way to work this into the current validator that I'm missing, or is it possible to have the validator not die, then I could use two separate validators?

@autarch
Copy link
Member

autarch commented Jun 6, 2021

#123

@autarch
Copy link
Member

autarch commented Mar 2, 2022

Added in 1.56

@autarch autarch closed this as completed Mar 2, 2022
@neilb
Copy link
Author

neilb commented Mar 3, 2022

Yay! Thanks.

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

3 participants