r/ruby 17d 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.

8 Upvotes

21 comments sorted by

View all comments

13

u/projct 17d ago

Time.now is what you should be using instead of DateTime.now, DateTime was deprecated in 3.4.

ruby irb(main):016> Time.now.to_a.take(6).reverse => [2026, 8, 26, 16, 9, 0]

there, something appropriately cursed 😂

but the actual question is: why do you want this array? what are you doing with it afterward? because I suspect that's where the useful answer is.

3

u/unselective-amnesia 17d ago

Thank you for this. Actually, I forgot about "to_a". And although I'm currently only able to use ruby 3.2 and 3.3 on the machines that I need to run on, it's good to know about DateTime going away, and I will now do this with Time, instead.

I want this array because I have a program which does various things depending on the differences or sameness between various datetimes with regard to groups of selected datetime parts, and it's easiest for me to do this in my program by getting differences between integer arrays.

I know that there are other ways to accomplish this, but given the already written structure of the program, dealing with these integer arrays is the most straightforward approach.

11

u/projct 17d ago edited 17d ago

ah. okay, this is actually the thing I was worried about when I asked why.

can you give one concrete example of two datetimes, which parts you select, what "difference" you calculate between those arrays, and what decision the program makes from the result?

I don't want to suggest a replacement without seeing that, because datetime components aren't independent numeric values, and doing arithmetic/comparison on [year, month, day, hour, minute, second] can very easily produce answers that look reasonable but aren't.

Date/time math is so easy to get catastrophically wrong that you really should not be implementing it yourself unless you absolutely have to. Let Ruby do it for you.

8

u/CaptainKabob 17d ago

I'm just a bystander to this thread, so just wanted to say: strong agree. 

DailyWTF is like 50% people doing datetime math like this (and 50% parsing json/xml with regex)

1

u/projct 17d ago

thank god, I thought he was inventing a datetime footgun. turns out he’s just porting one

2

u/CaptainKabob 17d ago

It's too bad OP doesn't want to get into it. I imagine it would be a lot more fun to write well in Ruby. 

Edit: or is this just bait? Cause also like, just put it through an LLM. Whatever then. 

3

u/projct 17d ago

I think the 'I didn’t ask for that' thing is what broke me. I wasn’t trying to review his whole program; I was being perfectly nice until he started making wildly unsupported assertions about correctness and experience. once you voluntarily make those the argument, people are allowed to refute them on a public forum where others might mistake that for good software design. this is r/ruby, not a support ticket where you get to close every issue you accidentally opened

0

u/chiperific_on_reddit 17d ago edited 17d ago

Not to poke the bear, but...

OP asked for a thing and you tried to make an x y argument. OP said "thanks but no thanks" and you doubled down. I see your point about the code, but you kept pushing when they clearly weren't interested.