FirebirdZonedTimeToDateTimeOffsetPropertyHandler

A property handler that maps the Firebird TIME WITH TIME ZONE type into a DateTimeOffset property on 1970-01-01.

A property handler that maps the Firebird TIME WITH TIME ZONE type into a DateTimeOffset property, as no single BCL type represents a time of day with an offset.

No single BCL type represents a time of day with an offset, hence the DateTimeOffset. Only the time of day and the UTC offset are meaningful: the date part is always 1970-01-01 when reading and is ignored when writing, and the name of the time zone is not kept.

Overview

Name Description
Class FirebirdZonedTimeToDateTimeOffsetPropertyHandler
Namespace RepoDb.PropertyHandlers.Firebird
Package RepoDb.Firebird
Implements IPropertyHandler<object, DateTimeOffset>
Column type The Firebird TIME WITH TIME ZONE type
Property type DateTimeOffset

Conversions

Method Direction Description
Get Database to property Converts the value returned by the driver (a DateTimeOffset or an FbZonedTime) into a DateTimeOffset on 1970-01-01, holding the local time of day and the UTC offset. null and DBNull are returned as default.
Set Property to database Converts the DateTimeOffset into an FbZonedTime, holding the UTC time of day with the offset as the time zone name (for example +03:00). The date part is ignored.

Exceptions

Exception Thrown When
InvalidOperationException The UTC offset of the time zone cannot be determined.
ArgumentException The type of the value returned by the driver is not supported.

Usability

Bind the handler to the property that maps to a TIME WITH TIME ZONE column.

Attribute

public class Shift
{
    public int Id { get; set; }

    [Map("opens_at"), PropertyHandler(typeof(FirebirdZonedTimeToDateTimeOffsetPropertyHandler))]
    public DateTimeOffset OpensAt { get; set; }
}

Once bound, the library invokes the handler automatically when the property is read or written.

using (var connection = new FbConnection(connectionString))
{
    connection.Insert(new Shift { Id = 1, OpensAt = new DateTimeOffset(1970, 1, 1, 9, 0, 0, TimeSpan.FromHours(3)) });
    var shift = connection.Query<Shift>(e => e.Id == 1).First();
    // shift.OpensAt is 09:00 +03:00 on 1970-01-01
}

Fluent Mapping

To configure via FluentMapper:

FluentMapper
    .Entity<Shift>()
    .PropertyHandler<FirebirdZonedTimeToDateTimeOffsetPropertyHandler>(e => e.OpensAt);

Property Level Mapping

To configure via PropertyHandlerMapper:

PropertyHandlerMapper.Add<Shift, FirebirdZonedTimeToDateTimeOffsetPropertyHandler>(e => e.OpensAt, new FirebirdZonedTimeToDateTimeOffsetPropertyHandler(), true);

Type Level Mapping

To apply the handler to every property of the type DateTimeOffset:

PropertyHandlerMapper.Add<DateTimeOffset, FirebirdZonedTimeToDateTimeOffsetPropertyHandler>(new FirebirdZonedTimeToDateTimeOffsetPropertyHandler(), true);

A type level mapping is process-wide: it is applied to every DateTimeOffset property of every entity and connection, not only to the intended column. Prefer the attribute, fluent or property level mapping unless every DateTimeOffset property maps to a TIME WITH TIME ZONE column.

See Also