CustomDateTimeOffset 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package CustomDateTimeOffset --version 1.0.1                
NuGet\Install-Package CustomDateTimeOffset -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CustomDateTimeOffset" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CustomDateTimeOffset --version 1.0.1                
#r "nuget: CustomDateTimeOffset, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CustomDateTimeOffset as a Cake Addin
#addin nuget:?package=CustomDateTimeOffset&version=1.0.1

// Install CustomDateTimeOffset as a Cake Tool
#tool nuget:?package=CustomDateTimeOffset&version=1.0.1                

CustomDateTime Library

CustomDateTime is a library for working with dates and time offsets in databases that do not natively support DateTimeOffset, such as MySQL and PostgreSQL. The library allows preserving and processing time offset information using a custom CustomDateTime class, which is designed to provide functionality similar to DateTimeOffset in databases without native support.

Features

  • CustomDateTime stores information about both the time (DateTime) and the time offset (Offset).
  • It provides methods for converting between DateTimeOffset and CustomDateTime.
  • Includes an Entity Framework Core ValueConverter to integrate CustomDateTime directly into your models.
  • Supports JSON serialization using a custom JsonConverter.

Installation

First, add the library to your project. If you're distributing the library via NuGet, you can install it as follows:

dotnet add package CustomDateTimeLibrary

If you're adding the library manually from GitHub, simply add it as a project reference.

Usage in Entity Framework Core

1. Defining Models

To use CustomDateTime in your Entity Framework Core models, simply replace DateTime or DateTimeOffset properties with CustomDateTime. For example:

using Core.Models.Shared;

public class Event
{
    public int Id { get; set; }
    
    // Using CustomDateTime instead of DateTimeOffset
    public CustomDateTime ValidFrom { get; set; }
}

2. Configuring OnModelCreating in DbContext

If you want to use CustomDateTime with Entity Framework Core, you'll need to add a custom ValueConverter in the OnModelCreating method in your DbContext:

using Microsoft.EntityFrameworkCore;
using Core.Converters;
using Core.Models.Shared;

public class ApplicationDbContext : DbContext
{
    public DbSet<Event> Events { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Event>()
            .Property(e => e.ValidFrom)
            .HasConversion(new CustomDateTimeConverter());
    }
}

3. Registering in Program.cs or Startup.cs

Depending on which version of ASP.NET Core you're using, you’ll need to register CustomDateTimeSerializer in Program.cs or Startup.cs to enable the correct JSON serialization and deserialization of date-time fields:

Program.cs (for .NET 6 and above):
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers()
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.Converters.Add(new CustomDateTimeSerializer());
    });

var app = builder.Build();

app.UseHttpsRedirection();
app.MapControllers();
app.Run();
Startup.cs (for older versions of .NET Core):
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers()
        .AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.Converters.Add(new CustomDateTimeSerializer());
        });
}

4. Example Usage in a Controller

Once everything is set up, you can start using CustomDateTime in your ASP.NET Core API controllers. For example:

[ApiController]
[Route("api/[controller]")]
public class EventsController : ControllerBase
{
    private readonly ApplicationDbContext _context;

    public EventsController(ApplicationDbContext context)
    {
        _context = context;
    }

    [HttpGet]
    public async Task<IActionResult> GetEvents()
    {
        var events = await _context.Events.ToListAsync();
        return Ok(events);
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 151 9/11/2024
1.0.1 105 9/11/2024
1.0.0 114 9/10/2024