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.

NodaTime YearMonth mapping

See original GitHub issue

It would be nice to have a NodaTime YearMonth mapping, despite the fact that PostgreSQL doesn’t have a specific type for it. It could be stored as a date (rounded to the first day of the month) or perhaps as an integer the way NodaTime internally stores it.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Shamus03commented, Jul 12, 2021

I revisited the project where I needed this today. I thought a ValueConverter wouldn’t result in proper SQL translation, but it does actually work very well:

modelBuilder.Entity<MyReport>()
    .Property(p => p.YearMonth)
    .IsRequired()
    .Hasconversion(
        v => v.OnDayOfMonth(1),
        v => v.ToYearMonth());

All parameters/comparisons/conversions just get treated like a PostgreSQL date. It will break down if I start to need more complicated translations, but this works great for now.

Thanks for the help.

0reactions
rojicommented, Jul 13, 2021

Happy it worked out and thanks for the code snippet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Noda Time | Patterns for YearMonth values
A mapping is provided between "year within era" and "absolute" year - where an absolute year uniquely identifies the date, and does not...
Read more >
Date/Time Mapping with NodaTime
NodaTime defines some types which are missing from the BCL, such as LocalDate , LocalTime , and OffsetTime . These cleanly correspond to...
Read more >
How do I correcly handle ZoneLocalMapping.ResultType. ...
throws an InvalidOperationException with message "EarlierMapping property should not be called on a result of type Ambiguous". I have no clue ...
Read more >
Noda Time | Jon Skeet's coding blog | Page 2
So, Noda Time exposes four ways of mapping a LocalDateTime and DateTimeZone to a ZonedDateTime: Exact: if there's a single mapping, return it....
Read more >
Noda Time: An Advanced Date/Time Library for .NET - InfoQ
The YearMonth subclass is useful for things like credit card expiration dates where defaulting to the first or last of the month may...
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