|
| 1 | +--- |
| 2 | +description: 'NodaTime usage guidelines - always use NodaTime types instead of .NET DateTime/DateTimeOffset' |
| 3 | +applyTo: '**/*.cs' |
| 4 | +--- |
| 5 | + |
| 6 | +# NodaTime Usage Guidelines |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +This project uses **NodaTime** for all date/time handling. NodaTime provides a cleaner, more explicit API for working with dates and times compared to the built-in .NET types. |
| 11 | + |
| 12 | +## Required Types |
| 13 | + |
| 14 | +Always use NodaTime types instead of .NET built-in types: |
| 15 | + |
| 16 | +| .NET Type | NodaTime Replacement | Use Case | |
| 17 | +|-----------|---------------------|----------| |
| 18 | +| `DateTime` | `Instant` | Points in time (timestamps, created/updated dates) | |
| 19 | +| `DateTime` | `LocalDateTime` | Date and time without timezone | |
| 20 | +| `DateTimeOffset` | `Instant` | Timestamps with timezone awareness | |
| 21 | +| `DateOnly` | `LocalDate` | Dates without time component | |
| 22 | +| `TimeOnly` | `LocalTime` | Times without date component | |
| 23 | +| `TimeSpan` | `Duration` | Elapsed time / intervals | |
| 24 | +| `TimeSpan` | `Period` | Calendar-based intervals (months, years) | |
| 25 | + |
| 26 | +## Entity Models |
| 27 | + |
| 28 | +All entity date/time properties MUST use `Instant`: |
| 29 | + |
| 30 | +```csharp |
| 31 | +// CORRECT |
| 32 | +using NodaTime; |
| 33 | + |
| 34 | +public class MyEntity : DataModelBase |
| 35 | +{ |
| 36 | + public Instant? LastPlayedAt { get; set; } |
| 37 | + public Instant? ExpiresAt { get; set; } |
| 38 | + public Instant PublishedAt { get; set; } |
| 39 | +} |
| 40 | + |
| 41 | +// WRONG - Never use these in entity models |
| 42 | +public class MyEntity |
| 43 | +{ |
| 44 | + public DateTime? LastPlayedAt { get; set; } // NO |
| 45 | + public DateTimeOffset? ExpiresAt { get; set; } // NO |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +## Why This Matters |
| 50 | + |
| 51 | +1. **SQLite Compatibility**: SQLite EF Core provider doesn't support `DateTimeOffset` in ORDER BY clauses. NodaTime's `Instant` type works correctly with SQLite when using `UseNodaTime()`. |
| 52 | + |
| 53 | +2. **Consistency**: The entire codebase uses NodaTime. Mixing types causes conversion issues and maintenance burden. |
| 54 | + |
| 55 | +3. **Clarity**: NodaTime types make the code's intent explicit (e.g., `Instant` is unambiguously a point in time in UTC). |
| 56 | + |
| 57 | +## Database Configuration |
| 58 | + |
| 59 | +The DbContext is configured to use NodaTime: |
| 60 | + |
| 61 | +```csharp |
| 62 | +optionsBuilder.UseSqlite(connectionString, x => x.UseNodaTime()); |
| 63 | +// or |
| 64 | +optionsBuilder.UseNpgsql(connectionString, x => x.UseNodaTime()); |
| 65 | +``` |
| 66 | + |
| 67 | +## Common Patterns |
| 68 | + |
| 69 | +### Getting Current Time |
| 70 | +```csharp |
| 71 | +using NodaTime; |
| 72 | + |
| 73 | +var now = SystemClock.Instance.GetCurrentInstant(); |
| 74 | +``` |
| 75 | + |
| 76 | +### Converting from External Sources |
| 77 | +When receiving `DateTimeOffset` from external APIs (RSS feeds, HTTP headers, etc.): |
| 78 | + |
| 79 | +```csharp |
| 80 | +// Convert DateTimeOffset to Instant |
| 81 | +DateTimeOffset externalDate = GetFromExternalSource(); |
| 82 | +Instant instant = Instant.FromDateTimeOffset(externalDate); |
| 83 | + |
| 84 | +// Handle potential MinValue (represents "no date") |
| 85 | +episode.PublishDate = externalDate != DateTimeOffset.MinValue |
| 86 | + ? Instant.FromDateTimeOffset(externalDate) |
| 87 | + : null; |
| 88 | +``` |
| 89 | + |
| 90 | +### Extracting Date Components |
| 91 | +```csharp |
| 92 | +Instant instant = SystemClock.Instance.GetCurrentInstant(); |
| 93 | + |
| 94 | +// Get year, month, day (requires converting to ZonedDateTime) |
| 95 | +int year = instant.InUtc().Year; |
| 96 | +int month = instant.InUtc().Month; |
| 97 | +int day = instant.InUtc().Day; |
| 98 | +``` |
| 99 | + |
| 100 | +### Formatting for Display |
| 101 | +```csharp |
| 102 | +Instant instant = entity.CreatedAt; |
| 103 | + |
| 104 | +// Convert to DateTime for standard formatting |
| 105 | +DateTime dateTime = instant.ToDateTimeUtc(); |
| 106 | +string formatted = dateTime.ToString("yyyy-MM-dd"); |
| 107 | + |
| 108 | +// Or use NodaTime patterns |
| 109 | +string formatted = instant.InUtc().LocalDateTime.ToString("yyyy-MM-dd HH:mm:ss", null); |
| 110 | +``` |
| 111 | + |
| 112 | +### Duration and Arithmetic |
| 113 | +```csharp |
| 114 | +var now = SystemClock.Instance.GetCurrentInstant(); |
| 115 | +var duration = Duration.FromHours(2); |
| 116 | +var twoHoursAgo = now.Minus(duration); |
| 117 | +var twoHoursFromNow = now.Plus(duration); |
| 118 | +``` |
| 119 | + |
| 120 | +## DTOs and API Models |
| 121 | + |
| 122 | +For DTOs that cross API boundaries, prefer `Instant` when possible. If the external API requires `DateTimeOffset`, convert at the boundary: |
| 123 | + |
| 124 | +```csharp |
| 125 | +// Internal DTO |
| 126 | +public record MyDataInfo( |
| 127 | + int Id, |
| 128 | + Instant CreatedAt, |
| 129 | + Instant? LastModified |
| 130 | +); |
| 131 | + |
| 132 | +// API response mapping (if needed) |
| 133 | +public DateTimeOffset CreatedAtDto => CreatedAt.ToDateTimeOffset(); |
| 134 | +``` |
| 135 | + |
| 136 | +## Test Data |
| 137 | + |
| 138 | +When creating test data, use NodaTime: |
| 139 | + |
| 140 | +```csharp |
| 141 | +var entity = new MyEntity |
| 142 | +{ |
| 143 | + CreatedAt = SystemClock.Instance.GetCurrentInstant(), |
| 144 | + PublishDate = Instant.FromUtc(2025, 1, 15, 10, 30), |
| 145 | + ExpiresAt = SystemClock.Instance.GetCurrentInstant().Plus(Duration.FromDays(30)) |
| 146 | +}; |
| 147 | +``` |
| 148 | + |
| 149 | +## Exceptions |
| 150 | + |
| 151 | +The only acceptable use of `DateTimeOffset` is when storing HTTP header values that are naturally `DateTimeOffset` and are NOT used in database queries (e.g., `Last-Modified` header for caching): |
| 152 | + |
| 153 | +```csharp |
| 154 | +public class PodcastChannel |
| 155 | +{ |
| 156 | + // OK - This comes from HTTP Last-Modified header and is not queried/sorted |
| 157 | + public DateTimeOffset? LastModified { get; set; } |
| 158 | + |
| 159 | + // These MUST be Instant - they are used in queries |
| 160 | + public Instant? LastSyncAt { get; set; } |
| 161 | + public Instant CreatedAt { get; set; } |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +## References |
| 166 | + |
| 167 | +- [NodaTime Documentation](https://nodatime.org/3.1.x/userguide) |
| 168 | +- [EF Core NodaTime Provider](https://www.npgsql.org/efcore/mapping/nodatime.html) |
0 commit comments