FirebirdZonedTimeToNullableDateTimeOffsetPropertyHandler
A property handler that maps the Firebird TIME WITH TIME ZONE type into a nullable DateTimeOffset property on 1970-01-01.
A property handler that maps the Firebird TIME WITH TIME ZONE type into a Nullable<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 | FirebirdZonedTimeToNullableDateTimeOffsetPropertyHandler |
| 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 null. |
| 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. A null value is written as null. |
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 nullable TIME WITH TIME ZONE column.
Attribute
public class Shift
{
public int Id { get; set; }
[Map("closes_at"), PropertyHandler(typeof(FirebirdZonedTimeToNullableDateTimeOffsetPropertyHandler))]
public DateTimeOffset? ClosesAt { 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, ClosesAt = null });
var shift = connection.Query<Shift>(e => e.Id == 1).First();
// shift.ClosesAt is null
}
Fluent Mapping
To configure via FluentMapper:
FluentMapper
.Entity<Shift>()
.PropertyHandler<FirebirdZonedTimeToNullableDateTimeOffsetPropertyHandler>(e => e.ClosesAt);
Property Level Mapping
To configure via PropertyHandlerMapper:
PropertyHandlerMapper.Add<Shift, FirebirdZonedTimeToNullableDateTimeOffsetPropertyHandler>(e => e.ClosesAt, new FirebirdZonedTimeToNullableDateTimeOffsetPropertyHandler(), true);
Type Level Mapping
To apply the handler to every property of the type DateTimeOffset?:
PropertyHandlerMapper.Add<DateTimeOffset?, FirebirdZonedTimeToNullableDateTimeOffsetPropertyHandler>(new FirebirdZonedTimeToNullableDateTimeOffsetPropertyHandler(), 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 everyDateTimeOffset?property maps to aTIME WITH TIME ZONEcolumn.