Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Interval math with DateTime #180

Open
schveiguy opened this issue Jun 7, 2022 · 6 comments
Open

Add Interval math with DateTime #180

schveiguy opened this issue Jun 7, 2022 · 6 comments

Comments

@schveiguy
Copy link
Contributor

A postgres Interval type cannot be mapped to a Duration, because it has a "months" field.

But all time math on DateTime involves Duration. There should at least be a way to add an Interval to a DateTime.

@denizzzka
Copy link
Owner

Initially, I preferred not to add conversions or interop methods, which can suddenly lead to a decrease in accuracy

Now I think for some frequently used ypes we can add such conversion, with careful check that accuracy/range isn't lost (corresponding exception type already implemented)

@schveiguy
Copy link
Contributor Author

schveiguy commented Jun 8, 2022

Yeah, I have this function in my code now. Not sure if std.datetime can deal with the overflow:

@safe DateTime addInterval(DateTime date, dpq2.conv.time.Interval delta) {
    if(delta.months != 0) {
        date.add!"months"(delta.months);
    }
    // create a duration from the days/usecs
    import core.time : days, usecs;
    return date + (delta.days.days + delta.usecs.usecs);
}

I verified that postgres applies the months interval before the days/hours, so this should mimic that behavior.

@schveiguy
Copy link
Contributor Author

Note that std.datetime also has an Interval type making this a bit awkward...

@denizzzka
Copy link
Owner

Looks good. Can you make PR?

If it's not urgent, then I can do it myself, but a little later

@schveiguy
Copy link
Contributor Author

It's not urgent. I just wanted to note it as something that is needed. I can use our internal function. If it gets added, I will switch to using the official one.

@schveiguy
Copy link
Contributor Author

If I find some time, I'll make a PR.

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

No branches or pull requests

2 participants