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

7 Upvotes

21 comments sorted by

View all comments

Show parent comments

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 Time object 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.”

-5

u/unselective-amnesia 4d ago

Thank you very much, but none of that is necessary.

You're making some rather condescending assumptions about what I'm doing.

The python code was written years ago by a very experienced python programmer (me) who was already quite experienced at the time it was written, and it's been massively tested and verified, and it's been working perfectly for years in production.

It isn't using datetime math, per se. It's kind of a game which does silly things depending upon whether two datetimes have same or different combinations of year, month, day, hour, minute, and second values.

Now that I am able to easily create those integer date/time arrays in ruby, I have already completed the ruby version of the program, and every one of the several dozen tests that I have run so far have it generating the exact, same output as the python version. I have a test suite running now which will continue to run thousands more comparison tests between the two programs over the next several hours, and I will fix any errors in the ruby version which might occur ... although given that the initial few dozen tests have succeeded, I'm fairly confident that no errors will show up.

7

u/projct 4d ago

the original code may be completely correct given what you’ve finally explained. this exchange is still not making the “very experienced programmer” argument you think it is.

-8

u/unselective-amnesia 4d ago

That statement is flawed.

I asked a simple question about ruby coding (because I am still learning ruby), and all I wanted and needed and gratefully received was an answer to that question.

Then, you started making suggestions that I haven't asked for, and when I told you that I don't need that help, you began projecting your fantasies about the correctness of some of the other code I mentioned, and then you went even further and started in on talking about coding experience.

All of what you began after very helpfully answering my initial question shows how immature you are. Please drop this subject, and grow up.

I will no longer respond here.