Runtime

One day, 25 hours: DateTime, DateTimeOffset and time zones in .NET

DEV Community: #csharp AdrianStrugala
Runtime

The same row showed Sunday to one user and Monday to another: SQL Server's datetime2 drops the offset, and 25 October lasts 25 hours in Berlin. The author explains how a small struct that keeps the zone next to the moment fixes the filter.

Key points

  • SQL Server's datetime2 drops the offset, so 01:30+02:00 comes back as a bare 01:30 in every time zone.
  • 25 October 2026 lasts 25 hours in Berlin but 24 in Tokyo, so fixed 24-hour day filters break.
  • DateTime's Kind.Utc survives a database round trip, but Kind.Unspecified yields a different moment on each machine.
  • The SolTechnology.DateTimeZone package wraps a DateTimeOffset with a TimeZoneInfo and serializes as RFC 9557 text.
  • AddHours moves the moment; AddDays, AddMonths and AddYears move the date on the clock.
  • ClockChange (NewOffset or Throw) resolves ambiguous and invalid local times such as 02:30 at a DST switch.

Real-world use

  • A list screen where users in Berlin and Tokyo filter records by day and each needs their own local day.
  • A SQL Server table whose datetime2 CreatedAt column is queried with a user-supplied date range.
  • A weekly meeting scheduled in a named zone that must keep its local clock time across a DST change.
  • Avro, Iceberg or Parquet timestamps that carry no zone and need an explicit InZone call on import.

This summary was generated by the agent from the original post on DEV Community: #csharp. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message