Skip to content

Conversation

@tfausak
Copy link
Collaborator

@tfausak tfausak commented Sep 8, 2025

I added negate in #21, but it can be generalized into arbitrary scaling. And then it's simply negate = scale (-1).

-- >>> scale 0.6 (MkInterval 0 0 1) -- rounds up
-- Just (MkInterval {months = 0, days = 0, microseconds = 1})
--
-- Fractional days are converted into microseconds, assuming 24 hours per day.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior is a bit strange, but it's what PostgreSQL does:

postgres=# select interval '1 day' * 0.5;
 ?column?
----------
 12:00:00
(1 row)

--
-- Fractional months are converted into days, assuming 30 days per month. If
-- this conversion produces fractional days, those are converted into
-- microseconds.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly:

postgres=# select interval '1 month' * 0.05;
    ?column?
----------------
 1 day 12:00:00
(1 row)

-- Just (MkInterval {months = 0, days = 1, microseconds = 43200000000})
--
-- Returns 'Nothing' if any component would overflow. See 'scaleSaturating' for
-- a version that uses saturating arithmetic instead.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostgreSQL errors out if an interval would overflow:

postgres=# select interval '46341 days' * 46341;
ERROR:  interval out of range

@tfausak tfausak enabled auto-merge (squash) September 8, 2025 15:19
@tfausak tfausak merged commit 56b183a into main Sep 8, 2025
19 checks passed
@tfausak tfausak deleted the tfausak/2025-09-09-scale branch September 8, 2025 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants