< Summary

Information
Class: MyNet.Temporal.Intervals.DateTimePeriod
Assembly: MyNet.Temporal
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Intervals/DateTimePeriod.cs
Tag: 342_31701152804
Line coverage
62%
Covered lines: 15
Uncovered lines: 9
Coverable lines: 24
Total lines: 78
Line coverage: 62.5%
Branch coverage
66%
Covered branches: 8
Total branches: 12
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
FromDuration(...)100%11100%
Shift(...)100%11100%
Extend(...)100%11100%
ToOffsetPeriod(...)100%11100%
ToOffsetPeriod(...)100%210%
Create(...)75%88100%
ToOffsetBound(...)100%22100%
ToOffsetBound(...)0%620%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Intervals/DateTimePeriod.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DateTimePeriod.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8
 9using MyNet.Primitives.Intervals;
 10
 11namespace MyNet.Temporal.Intervals;
 12
 13/// <summary>
 14/// Half-open civil <see cref="DateTime"/> interval <c>[start, end)</c>: start inclusive, end exclusive.
 15/// Prefer this type for UI / desktop occupation spans. For absolute instants (APIs, multi-offset), use
 16/// <see cref="DateTimeOffsetPeriod"/> instead. <see cref="DateTime.Kind"/> is not normalized by constructors.
 17/// </summary>
 18/// <param name="start">Inclusive start.</param>
 19/// <param name="end">Exclusive end.</param>
 9620public sealed class DateTimePeriod(DateTime start, DateTime end) : DateTimeInterval<DateTimePeriod>(start, end, inclusiv
 21{
 22    /// <summary>
 23    /// Creates a period from a start and a fixed <see cref="TimeSpan"/> duration.
 24    /// </summary>
 325    public static DateTimePeriod FromDuration(DateTime start, TimeSpan duration) => new(start, start + duration);
 26
 27    /// <summary>
 28    /// Returns a new DateTimePeriod shifted by <paramref name="offset"/>.
 29    /// </summary>
 930    public DateTimePeriod Shift(TimeSpan offset) => new(Start!.Value.Value + offset, End!.Value.Value + offset);
 31
 32    /// <summary>
 33    /// Returns a new DateTimePeriod with the end extended by <paramref name="offset"/>.
 34    /// </summary>
 635    public DateTimePeriod Extend(TimeSpan offset) => new(Start!.Value.Value, End!.Value.Value + offset);
 36
 37    /// <summary>
 38    /// Converts bounds to <see cref="DateTimeOffset"/> with the given fixed offset.
 39    /// <see cref="DateTimeKind.Utc"/> / <see cref="DateTimeKind.Local"/> preserve the absolute instant then project to 
 40    /// <see cref="DateTimeKind.Unspecified"/> is treated as civil wall time with that offset attached.
 41    /// </summary>
 42    public DateTimeOffsetPeriod ToOffsetPeriod(TimeSpan offset) =>
 643        new(ToOffsetBound(Start!.Value.Value, offset), ToOffsetBound(End!.Value.Value, offset));
 44
 45    /// <summary>
 46    /// Converts bounds into <paramref name="timeZone"/> (DST-aware).
 47    /// Utc/Local preserve the absolute instant; Unspecified is treated as wall time in that zone.
 48    /// </summary>
 49    public DateTimeOffsetPeriod ToOffsetPeriod(TimeZoneInfo timeZone)
 50    {
 051        ArgumentNullException.ThrowIfNull(timeZone);
 52
 053        return new DateTimeOffsetPeriod(ToOffsetBound(Start!.Value.Value, timeZone), ToOffsetBound(End!.Value.Value, tim
 54    }
 55
 56    /// <inheritdoc />
 57    protected override DateTimePeriod Create(IntervalBoundary<DateTime>? start, IntervalBoundary<DateTime>? end) =>
 658        !start.HasValue || !end.HasValue
 659            ? throw new InvalidOperationException("DateTimePeriod must be bounded.")
 660            : !start.Value.IsInclusive || end.Value.IsInclusive
 661                ? throw new InvalidOperationException("DateTimePeriod only supports [start, end) boundaries.")
 662                : new DateTimePeriod(start.Value.Value, end.Value.Value);
 63
 1264    private static DateTimeOffset ToOffsetBound(DateTime value, TimeSpan offset) => value.Kind switch
 1265    {
 666        DateTimeKind.Utc or DateTimeKind.Local => new DateTimeOffset(value).ToOffset(offset),
 667        _ => new DateTimeOffset(DateTime.SpecifyKind(value, DateTimeKind.Unspecified), offset)
 1268    };
 69
 070    private static DateTimeOffset ToOffsetBound(DateTime value, TimeZoneInfo timeZone) => value.Kind switch
 071    {
 072        DateTimeKind.Utc or DateTimeKind.Local => TimeZoneInfo.ConvertTime(new DateTimeOffset(value), timeZone),
 073        _ => new DateTimeOffset(
 074            DateTime.SpecifyKind(value, DateTimeKind.Unspecified),
 075            timeZone.GetUtcOffset(DateTime.SpecifyKind(value, DateTimeKind.Unspecified)))
 076    };
 77}
 78