| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NumberToTimeSpanExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Primitives.Intervals; |
| | | 9 | | using MyNet.Primitives.Temporal; |
| | | 10 | | |
| | | 11 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 12 | | namespace MyNet.Primitives; |
| | | 13 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 14 | | |
| | | 15 | | public static class NumberToTimeSpanExtensions |
| | | 16 | | { |
| | 33 | 17 | | private static int GetDecadeStart(int year) => year / 10 * 10; |
| | | 18 | | |
| | 27 | 19 | | private static int GetCenturyStart(int year) => year / 100 * 100; |
| | | 20 | | |
| | | 21 | | extension(int value) |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Generates <see cref="TimeSpan"/> value for given number of Years. |
| | | 25 | | /// </summary> |
| | 78 | 26 | | public FluentTimeSpan Years() => new() { Years = value }; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Generates <see cref="TimeSpan"/> value for given number of Quarters. |
| | | 30 | | /// </summary> |
| | 12 | 31 | | public FluentTimeSpan Quarters() => new() { Months = value * 3 }; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Returns <see cref="TimeSpan"/> value for given number of Months. |
| | | 35 | | /// </summary> |
| | 78 | 36 | | public FluentTimeSpan Months() => new() { Months = value }; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Returns <see cref="TimeSpan"/> for given number of Weeks (number of weeks * 7). |
| | | 40 | | /// </summary> |
| | 75 | 41 | | public FluentTimeSpan Weeks() => new() { TimeSpan = TimeSpan.FromDays(value * DateTimeHelper.DaysPerWeek) }; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Returns <see cref="TimeSpan"/> for given number of Days. |
| | | 45 | | /// </summary> |
| | 288 | 46 | | public FluentTimeSpan Days() => new() { TimeSpan = TimeSpan.FromDays(value) }; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Returns <see cref="TimeSpan"/> for given number of Hours. |
| | | 50 | | /// </summary> |
| | 78 | 51 | | public FluentTimeSpan Hours() => new() { TimeSpan = TimeSpan.FromHours(value) }; |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Returns <see cref="TimeSpan"/> for given number of Minutes. |
| | | 55 | | /// </summary> |
| | 81 | 56 | | public FluentTimeSpan Minutes() => new() { TimeSpan = TimeSpan.FromMinutes(value) }; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Returns <see cref="TimeSpan"/> for given number of Seconds. |
| | | 60 | | /// </summary> |
| | 273 | 61 | | public FluentTimeSpan Seconds() => new() { TimeSpan = TimeSpan.FromSeconds(value) }; |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Returns <see cref="TimeSpan"/> for given number of Milliseconds. |
| | | 65 | | /// </summary> |
| | 90 | 66 | | public FluentTimeSpan Milliseconds() => new() { TimeSpan = TimeSpan.FromMilliseconds(value) }; |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Returns <see cref="TimeSpan"/> for given number of ticks. |
| | | 70 | | /// </summary> |
| | 66 | 71 | | public FluentTimeSpan Ticks() => new() { TimeSpan = TimeSpan.FromTicks(value) }; |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Returns <see cref="TimeSpan"/> for given number of units. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="unit">The unit of time.</param> |
| | | 77 | | /// <returns>A <see cref="FluentTimeSpan"/> representing the specified number of units.</returns> |
| | 0 | 78 | | public FluentTimeSpan Unit(TimeUnit unit) => unit switch |
| | 0 | 79 | | { |
| | 0 | 80 | | TimeUnit.Millisecond => new() { TimeSpan = TimeSpan.FromMilliseconds(value) }, |
| | 0 | 81 | | TimeUnit.Second => new() { TimeSpan = TimeSpan.FromSeconds(value) }, |
| | 0 | 82 | | TimeUnit.Minute => new() { TimeSpan = TimeSpan.FromMinutes(value) }, |
| | 0 | 83 | | TimeUnit.Hour => new() { TimeSpan = TimeSpan.FromHours(value) }, |
| | 0 | 84 | | TimeUnit.Day => new() { TimeSpan = TimeSpan.FromDays(value) }, |
| | 0 | 85 | | TimeUnit.Week => new() { TimeSpan = TimeSpan.FromDays(value * DateTimeHelper.DaysPerWeek) }, |
| | 0 | 86 | | TimeUnit.Month => new() { Months = value }, |
| | 0 | 87 | | TimeUnit.Year => new() { Years = value }, |
| | 0 | 88 | | _ => default |
| | 0 | 89 | | }; |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Converts an integer value to a <see cref="TimeSpan"/> based on the specified time unit. For example, if you |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="timeUnitToGet">The time unit to convert the integer value to.</param> |
| | | 95 | | /// <returns>A <see cref="TimeSpan"/> representing the specified value in the specified time unit.</returns> |
| | 273 | 96 | | public TimeSpan ToTimeSpan(TimeUnit timeUnitToGet) => timeUnitToGet switch |
| | 273 | 97 | | { |
| | 0 | 98 | | TimeUnit.Millisecond => new(0, 0, 0, 0, value), |
| | 45 | 99 | | TimeUnit.Second => new(0, 0, 0, value, 0), |
| | 63 | 100 | | TimeUnit.Minute => new(0, 0, value, 0, 0), |
| | 42 | 101 | | TimeUnit.Hour => new(0, value, 0, 0, 0), |
| | 48 | 102 | | TimeUnit.Day => new(value, 0, 0, 0, 0), |
| | 0 | 103 | | TimeUnit.Week => new(value * DateTimeHelper.DaysPerWeek, 0, 0, 0, 0), |
| | 42 | 104 | | TimeUnit.Month => new((int)Math.Round(value * DateTimeHelper.DaysPerMonth), 0, 0, 0, 0), |
| | 33 | 105 | | TimeUnit.Year => new((int)Math.Round(value * DateTimeHelper.DaysPerYear), 0, 0, 0, 0), |
| | 0 | 106 | | _ => TimeSpan.Zero |
| | 273 | 107 | | }; |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Returns the first year of the decade containing the given year. For example, if the input year is 1995, this |
| | | 111 | | /// </summary> |
| | | 112 | | /// <returns>The first year of the decade containing the given year.</returns> |
| | 15 | 113 | | public int DecadeStart() => GetDecadeStart(value); |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Returns the last year of the decade containing the given year. For example, if the input year is 1995, this |
| | | 117 | | /// </summary> |
| | | 118 | | /// <returns>The last year of the decade containing the given year.</returns> |
| | 12 | 119 | | public int DecadeEnd() => GetDecadeStart(value) + 9; |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Returns a ClosedInterval representing the decade of the given year. The start of the interval is calculated |
| | | 123 | | /// </summary> |
| | | 124 | | /// <returns>A ClosedInterval representing the decade of the given year.</returns> |
| | | 125 | | public ClosedInterval<int> Decade() |
| | | 126 | | { |
| | 6 | 127 | | var start = GetDecadeStart(value); |
| | 6 | 128 | | return new(start, start + 9); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Returns the first year of the century containing the given year. For example, if the input year is 1995, thi |
| | | 133 | | /// </summary> |
| | | 134 | | /// <returns>The first year of the century containing the given year.</returns> |
| | 12 | 135 | | public int CenturyStart() => GetCenturyStart(value); |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Returns the last year of the century containing the given year. For example, if the input year is 1995, this |
| | | 139 | | /// </summary> |
| | | 140 | | /// <returns>The last year of the century containing the given year.</returns> |
| | 9 | 141 | | public int CenturyEnd() => GetCenturyStart(value) + 99; |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Returns a ClosedInterval representing the century of the given year. The start of the interval is calculated |
| | | 145 | | /// </summary> |
| | | 146 | | /// <returns>A ClosedInterval representing the century of the given year.</returns> |
| | | 147 | | public ClosedInterval<int> Century() |
| | | 148 | | { |
| | 6 | 149 | | var start = GetCenturyStart(value); |
| | 6 | 150 | | return new(start, start + 99); |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | extension(double value) |
| | | 155 | | { |
| | | 156 | | /// <summary> |
| | | 157 | | /// Returns <see cref="TimeSpan"/> for given number of Weeks (number of weeks * 7). |
| | | 158 | | /// </summary> |
| | 0 | 159 | | public FluentTimeSpan Weeks() => new() { TimeSpan = TimeSpan.FromDays((int)(value * DateTimeHelper.DaysPerWeek)) |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Returns <see cref="TimeSpan"/> for given number of Days. |
| | | 163 | | /// </summary> |
| | 18 | 164 | | public FluentTimeSpan Days() => new() { TimeSpan = TimeSpan.FromDays(value) }; |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Returns <see cref="TimeSpan"/> for given number of Hours. |
| | | 168 | | /// </summary> |
| | 0 | 169 | | public FluentTimeSpan Hours() => new() { TimeSpan = TimeSpan.FromHours(value) }; |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Returns <see cref="TimeSpan"/> for given number of Minutes. |
| | | 173 | | /// </summary> |
| | 0 | 174 | | public FluentTimeSpan Minutes() => new() { TimeSpan = TimeSpan.FromMinutes(value) }; |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Returns <see cref="TimeSpan"/> for given number of Seconds. |
| | | 178 | | /// </summary> |
| | 0 | 179 | | public FluentTimeSpan Seconds() => new() { TimeSpan = TimeSpan.FromSeconds(value) }; |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Returns <see cref="TimeSpan"/> for given number of Milliseconds. |
| | | 183 | | /// </summary> |
| | 0 | 184 | | public FluentTimeSpan Milliseconds() => new() { TimeSpan = TimeSpan.FromMilliseconds(value) }; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Returns <see cref="TimeSpan"/> for given number of ticks. |
| | | 189 | | /// </summary> |
| | 0 | 190 | | public static FluentTimeSpan Ticks(this long ticks) => new() { TimeSpan = TimeSpan.FromTicks(ticks) }; |
| | | 191 | | } |
| | | 192 | | |