Core Types
Detailed documentation for Pragmatic.Temporal types.
LocalDate
Section titled “LocalDate”Represents a calendar date without time or timezone. Use for dates that are meaningful regardless of timezone.
Creation
Section titled “Creation”// From componentsvar date = new LocalDate(2024, 6, 15);
// From DateTimevar fromDt = LocalDate.FromDateTime(DateTime.Now);
// From DateTimeOffsetvar fromDto = LocalDate.FromDateTimeOffset(DateTimeOffset.Now);
// Todayvar today = LocalDate.Today;Properties
Section titled “Properties”var date = new LocalDate(2024, 6, 15);
date.Year // 2024date.Month // 6date.Day // 15date.DayOfWeek // DayOfWeek.Saturdaydate.DayOfYear // 167date.IsWeekend // truedate.IsWeekday // falseArithmetic
Section titled “Arithmetic”var date = new LocalDate(2024, 6, 15);
date.AddDays(7) // 2024-06-22date.AddMonths(1) // 2024-07-15date.AddYears(1) // 2025-06-15
date.DaysBetween(other) // Days between datesNavigation
Section titled “Navigation”var date = new LocalDate(2024, 6, 15);
date.StartOfMonth() // 2024-06-01date.EndOfMonth() // 2024-06-30date.StartOfYear() // 2024-01-01date.EndOfYear() // 2024-12-31Combining with Time
Section titled “Combining with Time”var date = new LocalDate(2024, 6, 15);var time = new LocalTime(14, 30);
var dateTime = date.At(time); // LocalDateTimeLocalTime
Section titled “LocalTime”Represents a time of day without date or timezone.
Creation
Section titled “Creation”// From componentsvar time = new LocalTime(14, 30); // 14:30:00var withSeconds = new LocalTime(14, 30, 45); // 14:30:45
// From TimeOnlyvar fromTimeOnly = new LocalTime(TimeOnly.Now);
// From DateTime (extracts time part)var fromDt = LocalTime.FromDateTime(DateTime.Now);Properties
Section titled “Properties”var time = new LocalTime(14, 30, 45);
time.Hour // 14time.Minute // 30time.Second // 45Arithmetic
Section titled “Arithmetic”var time = new LocalTime(14, 30);
time.AddHours(2) // 16:30time.AddMinutes(30) // 15:00LocalDateTime
Section titled “LocalDateTime”Represents a date and time without timezone. Use for wall clock times.
Creation
Section titled “Creation”// From componentsvar dt = new LocalDateTime(2024, 6, 15, 14, 30);
// From LocalDate and LocalTimevar date = new LocalDate(2024, 6, 15);var time = new LocalTime(14, 30);var dt2 = date.At(time);
// From DateTimevar fromDt = LocalDateTime.FromDateTime(DateTime.Now);Properties
Section titled “Properties”var dt = new LocalDateTime(2024, 6, 15, 14, 30);
dt.Date // LocalDatedt.Time // LocalTimedt.Year, dt.Month, dt.Daydt.Hour, dt.Minute, dt.SecondConversion to ZonedDateTime
Section titled “Conversion to ZonedDateTime”var local = new LocalDateTime(2024, 6, 15, 14, 30);
// Interpret as a specific timezonevar zoned = local.InZone("Europe/Rome");ZonedDateTime
Section titled “ZonedDateTime”Represents a specific instant in time with full timezone awareness.
Creation
Section titled “Creation”// From UTCvar zoned = ZonedDateTime.FromUtc(DateTimeOffset.UtcNow, "Europe/Rome");
// From local datetime in a zonevar local = new LocalDateTime(2024, 6, 15, 14, 30);var zoned2 = ZonedDateTime.FromLocal(local, "America/New_York");
// Now in a timezonevar nowInRome = ZonedDateTime.NowIn("Europe/Rome");Properties
Section titled “Properties”var zoned = ZonedDateTime.FromUtc(DateTimeOffset.UtcNow, "Europe/Rome");
zoned.DateTime // DateTimeOffset (UTC)zoned.Zone // "Europe/Rome"zoned.Offset // Current UTC offset (e.g., +02:00)zoned.LocalDateTime // LocalDateTime in that zoneTimezone Conversion
Section titled “Timezone Conversion”var rome = ZonedDateTime.NowIn("Europe/Rome");var tokyo = rome.InZone("Asia/Tokyo");var utc = rome.ToUtc();Duration
Section titled “Duration”Represents a time span. Similar to TimeSpan but with better factory methods.
Creation
Section titled “Creation”var d1 = Duration.FromHours(2);var d2 = Duration.FromMinutes(30);var d3 = Duration.FromSeconds(45);var d4 = Duration.FromDays(7);
// Combinedvar d5 = Duration.FromHours(2) + Duration.FromMinutes(30);Properties
Section titled “Properties”var duration = Duration.FromHours(2.5);
duration.TotalHours // 2.5duration.TotalMinutes // 150duration.TotalSeconds // 9000Arithmetic
Section titled “Arithmetic”var d1 = Duration.FromHours(1);var d2 = Duration.FromMinutes(30);
var sum = d1 + d2; // 1:30:00var diff = d1 - d2; // 0:30:00var scaled = d1 * 2; // 2:00:00CronExpression
Section titled “CronExpression”Represents a cron schedule for recurring events.
Factory Methods
Section titled “Factory Methods”// Common patternsvar hourly = CronExpression.Hourly();var daily = CronExpression.Daily(new TimeOnly(9, 0));var weekly = CronExpression.Weekly(DayOfWeek.Monday, new TimeOnly(8, 0));var monthly = CronExpression.Monthly(1, new TimeOnly(0, 0)); // First of month
// Custom expressionvar custom = CronExpression.Parse("0 */15 9-17 * * 1-5");Getting Occurrences
Section titled “Getting Occurrences”var cron = CronExpression.Daily(new TimeOnly(9, 0));
// Next occurrencevar next = cron.GetNextOccurrence(DateTimeOffset.UtcNow);
// Multiple occurrencesvar nextFive = cron.GetNextOccurrences(DateTimeOffset.UtcNow, 5);Type Comparison
Section titled “Type Comparison”| Type | Has Date | Has Time | Has Timezone | Use Case |
|---|---|---|---|---|
LocalDate | ✅ | ❌ | ❌ | Birthdays, holidays |
LocalTime | ❌ | ✅ | ❌ | Store hours, schedules |
LocalDateTime | ✅ | ✅ | ❌ | Wall clock appointments |
ZonedDateTime | ✅ | ✅ | ✅ | Flight times, global events |
Duration | ❌ | ✅ | ❌ | Time intervals, durations |