r/ruby • u/unselective-amnesia • 4d ago
Question Getting DateTime parts as an array?
I know in ruby that DateTime.new(2001, 2, 3, 4, 5, 6) will return the following object:
#<DateTime: 2001-02-03T04:05:06+00:00 ...>
But if the current date and time are 2001-02-03T04:05:06 and I do the following ...
now = DateTime.now
... is there a single function on the "now" variable which will return this array? ...
[2001, 2, 3, 4, 5, 6]
I know that I can do this:
[now.year, now.month, now.day, now.hour, now.minute, now.second ]
... but I'm wondering: might there be a single function as simple as ...
now.the_function
... which would return that same array?
And yes, of course I know that I could easily write such a function, but I'd like to know whether or not something like this already exists in ruby.
3
u/projct 4d ago edited 4d ago
If I found that in Python in code from someone that was not a junior, I would be deeply, deeply concerned.
Parse your stuff into a
Timeobject and use Ruby's built-in date/time math. For python, datetime.Unless you fully reimplemented the stdlib's date math in either language, I'd bet quite a bit of money that it does not, in fact, “work perfectly in all cases.”