question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Map NodaTime Interval to tstzrange

See original GitHub issue

I somehow managed to miss NodaTime’s Interval type. This is a perfect match for PostgreSQL tstzrange, similarly to how we map DateInterval to PostgreSQL daterange.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
madmoxcommented, Dec 22, 2021

I can easily get around the issue because I use Dapper on top of npgsql and already have type handlers so that Dapper correctly maps properties with non default types, so the fix is not top-priority. I even updated the handler so that it accepts both Interval[] and NpgsqlRange<Instant>[], in case the fix is integrated later and I forget about the issue when upgrading my nugets.

public class IntervalListHandler : SqlMapper.TypeHandler<List<Interval>>
{
    public override List<Interval> Parse(object value)
    {
        // Looks like npgsql incorrectly maps tstzrange[] to NpgsqlRange<Instant>[]
        // although it correctly maps tstzrange to NodaTime.Interval.
        // See https://github.com/npgsql/npgsql/issues/4070 for details.

        if (value is NpgsqlRange<Instant>[])
        {
            var typedValue = (NpgsqlRange<Instant>[])value;
            return typedValue?.Select(x => new Interval(x.LowerBound, x.UpperBound)).ToList();
        }
        else
        {
            var typedValue = (Interval[])value;
            return typedValue?.ToList();
        }
    }

    public override void SetValue(IDbDataParameter parameter, List<Interval> value)
    {
        parameter.Value = value;
        ((NpgsqlParameter)parameter).NpgsqlDbType = NpgsqlDbType.Array | NpgsqlDbType.Range | NpgsqlDbType.TimestampTz;
    }
}

But I opened the issue 😃

0reactions
rojicommented, Dec 22, 2021

@madmox the non-contiguous aspect of multiranges may indeed be a reason to continue using arrays, true.

If using NodaTime Interval[] is important for you, please open a new issue - I can’t promise I’ll get around to it immediately though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodaTime Type Plugin
Npgsql provides a plugin that allows mapping the NodaTime date/time library; this is the recommended way to interact with PostgreSQL date/time types, ...
Read more >
Developers - Map NodaTime Interval to tstzrange -
Map NodaTime Interval to tstzrange. ... This is a perfect match for PostgreSQL tstzrange , similarly to how we map DateInterval to PostgreSQL...
Read more >
Struct Interval | Noda Time
Initializes a new instance of the Interval struct. The interval includes the start instant and excludes the end instant. The end may equal...
Read more >
Can i use daterange column and work with EF for ...
Generally i have report data for some date range and i want to insert new, and delete old one for the same period....
Read more >
Postgres Infinity Timestamp
Postgres Infinity Timestamp The timestamptz is defined as timestamp with timezone. Hence the timestamp holds finite value.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found