News for hackers. Stuff that matters.
Re:Please put units in names by mfkp on Sunday March 20, 2022 @11:18PM (#30749830)
Technically Ruby only accepts seconds. You're thinking of ActiveSupport from Rails.
Re:Please put units in names by inopinatus on Sunday March 20, 2022 @11:41PM (#30749956)
Here's a fun way to annoy a Rails developer.

    (Time.now + 1.month).to_i == Time.now.to_i + 1.month.to_i
    #=> false
Re:Please put units in names by irjustin on Monday March 21, 2022 @01:17AM (#30750571)
Not sure why that's annoying? The right side doesn't really make sense.

Though it does seem pretty easy for a novice to do thinking it's the same, but what does 1.month.to_i even mean!?

Re:Please put units in names by lloeki on Monday March 21, 2022 @06:32AM (#30752386)
> 1.month.to_i

Duration of a month in seconds? Before you balk at the idea, there exists a definition of a constant month duration for accounting stuff. I you hate dates - and yourself - try accounting, there's mind boggling stuff that makes the engineer mind recoil in absolute terror.

Re:Please put units in names by inopinatus on Monday March 21, 2022 @01:50AM (#30750727)
Actually taken almost verbatim from the report summarizing a real and subtle bug (distributed across multiple files) in code written by definitely-not-novices.
Re:Please put units in names by neurostimulant on Monday March 21, 2022 @02:24AM (#30750878)
I'm not really familiar to RoR. Is is due to Time.now getting called twice and each returns slightly different value?
Re:Please put units in names by inopinatus on Monday March 21, 2022 @02:50AM (#30751014)
Alas, no. It's because ActiveSupport's duration arithmetic is not distributive under conversion.

The expression on the left hand side advances time (as a domain object) by exactly a month, then converts the result to integer unix time. The expression on the right adds 2629746¹ to the current unix time.

The conversion becomes dangerously magical in the presence of shared code that accepts both object and integer representations of time & duration. A consumer from one part of a system can inadvertently obtain different results to another unless they use identical calling conventions.

[1] this is 1/12 of the mean length of a gregorian year²

[2] 365.2425 days i.e. 31,556,952 seconds

Re:Please put units in names by neurostimulant on Monday March 21, 2022 @05:33PM (#30759985)
Oh wow. This is totally make sense when you think about it, but something that'll never cross my mind when casually checking the code. I guess this is why python's timedelta doesn't have month unit as the length of a month is highly context dependent.
Re:Please put units in names by ativzzz on Monday March 21, 2022 @11:35AM (#30755265)
I would just use `1.month.from_now` instead of the additions anyway
Re:Please put units in names by inopinatus on Monday March 21, 2022 @04:46PM (#30759499)
That won’t save you; 1.month.from_now is implemented by addition.