| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateTimeOffsetExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 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 DateTimeOffsetExtensions |
| | | 16 | | { |
| | | 17 | | extension(DateTimeOffset dateTimeOffset) |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Returns a new <see cref="DateTime"/> that adds the value of the specified <see cref="FluentTimeSpan"/> to th |
| | | 21 | | /// </summary> |
| | 3 | 22 | | public DateTimeOffset AddFluentTimeSpan(FluentTimeSpan timeSpan) => dateTimeOffset.AddMonths(timeSpan.Months) |
| | 3 | 23 | | .AddYears(timeSpan.Years) |
| | 3 | 24 | | .Add(timeSpan.TimeSpan); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Returns a new <see cref="DateTime"/> that subtracts the value of the specified <see cref="FluentTimeSpan"/> |
| | | 28 | | /// </summary> |
| | 0 | 29 | | public DateTimeOffset SubtractFluentTimeSpan(FluentTimeSpan timeSpan) => dateTimeOffset.AddMonths(-timeSpan.Mont |
| | 0 | 30 | | .AddYears(-timeSpan.Years) |
| | 0 | 31 | | .Subtract(timeSpan.TimeSpan); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Returns the very end of the given day (the last millisecond of the last hour for the given <see cref="DateTi |
| | | 35 | | /// </summary> |
| | 3 | 36 | | public DateTimeOffset EndOfDay() => new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day, 23, 59, 5 |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Returns the Start of the given day (the first millisecond of the given <see cref="DateTimeOffset"/>). |
| | | 40 | | /// </summary> |
| | 3 | 41 | | public DateTimeOffset BeginningOfDay() => new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day, 0, |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Returns the same date (same Day, Month, Hour, Minute, Second etc.) in the next calendar year. |
| | | 45 | | /// If that day does not exist in next year in same month, number of missing days is added to the last day in sa |
| | | 46 | | /// </summary> |
| | | 47 | | public DateTimeOffset NextYear() |
| | | 48 | | { |
| | 3 | 49 | | var nextYear = dateTimeOffset.Year + 1; |
| | 3 | 50 | | var numberOfDaysInSameMonthNextYear = DateTime.DaysInMonth(nextYear, dateTimeOffset.Month); |
| | | 51 | | |
| | 3 | 52 | | if (numberOfDaysInSameMonthNextYear >= dateTimeOffset.Day) |
| | 3 | 53 | | return new(nextYear, dateTimeOffset.Month, dateTimeOffset.Day, dateTimeOffset.Hour, dateTimeOffset.Minut |
| | 0 | 54 | | var differenceInDays = dateTimeOffset.Day - numberOfDaysInSameMonthNextYear; |
| | 0 | 55 | | var dateTimeOffset1 = new DateTimeOffset(nextYear, dateTimeOffset.Month, numberOfDaysInSameMonthNextYear, da |
| | 0 | 56 | | return dateTimeOffset1 + differenceInDays.Days(); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Returns the same date (same Day, Month, Hour, Minute, Second etc.) in the previous calendar year. |
| | | 61 | | /// If that day does not exist in previous year in same month, number of missing days is added to the last day i |
| | | 62 | | /// </summary> |
| | | 63 | | public DateTimeOffset PreviousYear() |
| | | 64 | | { |
| | 3 | 65 | | var previousYear = dateTimeOffset.Year - 1; |
| | 3 | 66 | | var numberOfDaysInSameMonthPreviousYear = DateTime.DaysInMonth(previousYear, dateTimeOffset.Month); |
| | | 67 | | |
| | 3 | 68 | | if (numberOfDaysInSameMonthPreviousYear >= dateTimeOffset.Day) |
| | 3 | 69 | | return new(previousYear, dateTimeOffset.Month, dateTimeOffset.Day, dateTimeOffset.Hour, dateTimeOffset.M |
| | 0 | 70 | | var differenceInDays = dateTimeOffset.Day - numberOfDaysInSameMonthPreviousYear; |
| | 0 | 71 | | var dateTime = new DateTimeOffset(previousYear, dateTimeOffset.Month, numberOfDaysInSameMonthPreviousYear, d |
| | 0 | 72 | | return dateTime + differenceInDays.Days(); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Returns <see cref="DateTimeOffset"/> increased by 24 hours ie Next Day. |
| | | 77 | | /// </summary> |
| | 6 | 78 | | public DateTimeOffset NextDay() => dateTimeOffset + 1.Days(); |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Returns <see cref="DateTimeOffset"/> decreased by 24h period ie Previous Day. |
| | | 82 | | /// </summary> |
| | 6 | 83 | | public DateTimeOffset PreviousDay() => dateTimeOffset - 1.Days(); |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Returns first next occurrence of specified <see cref="DayOfWeek"/>. |
| | | 87 | | /// </summary> |
| | | 88 | | public DateTimeOffset Next(DayOfWeek day) |
| | | 89 | | { |
| | | 90 | | do |
| | 3 | 91 | | dateTimeOffset = dateTimeOffset.NextDay(); |
| | 3 | 92 | | while (dateTimeOffset.DayOfWeek != day); |
| | | 93 | | |
| | 3 | 94 | | return dateTimeOffset; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Returns first next occurrence of specified <see cref="DayOfWeek"/>. |
| | | 99 | | /// </summary> |
| | | 100 | | public DateTimeOffset Previous(DayOfWeek day) |
| | | 101 | | { |
| | | 102 | | do |
| | 3 | 103 | | dateTimeOffset = dateTimeOffset.PreviousDay(); |
| | 3 | 104 | | while (dateTimeOffset.DayOfWeek != day); |
| | | 105 | | |
| | 3 | 106 | | return dateTimeOffset; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Increases supplied <see cref="DateTimeOffset"/> for 7 days ie returns the Next Week. |
| | | 111 | | /// </summary> |
| | 0 | 112 | | public DateTimeOffset WeekAfter() => dateTimeOffset + 1.Weeks(); |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Decreases supplied <see cref="DateTimeOffset"/> for 7 days ie returns the Previous Week. |
| | | 116 | | /// </summary> |
| | 0 | 117 | | public DateTimeOffset WeekEarlier() => dateTimeOffset - 1.Weeks(); |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Increases the <see cref="DateTimeOffset"/> object with given <see cref="TimeSpan"/> value. |
| | | 121 | | /// </summary> |
| | 0 | 122 | | public DateTimeOffset IncreaseTime(TimeSpan toAdd) => dateTimeOffset + toAdd; |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Decreases the <see cref="DateTimeOffset"/> object with given <see cref="TimeSpan"/> value. |
| | | 126 | | /// </summary> |
| | 0 | 127 | | public DateTimeOffset DecreaseTime(TimeSpan toSubtract) => dateTimeOffset - toSubtract; |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Returns the original <see cref="DateTimeOffset"/> with Hour part changed to supplied hour parameter. |
| | | 131 | | /// </summary> |
| | 0 | 132 | | public DateTimeOffset SetTime(int hour) => new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day, ho |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Returns the original <see cref="DateTimeOffset"/> with Hour and Minute parts changed to supplied hour and mi |
| | | 136 | | /// </summary> |
| | 0 | 137 | | public DateTimeOffset SetTime(int hour, int minute) => new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOf |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Returns the original <see cref="DateTimeOffset"/> with Hour, Minute and Second parts changed to supplied hou |
| | | 141 | | /// </summary> |
| | 0 | 142 | | public DateTimeOffset SetTime(int hour, int minute, int second) => new(dateTimeOffset.Year, dateTimeOffset.Month |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Returns the original <see cref="DateTimeOffset"/> with Hour, Minute, Second and Millisecond parts changed to |
| | | 146 | | /// </summary> |
| | 6 | 147 | | public DateTimeOffset SetTime(int hour, int minute, int second, int millisecond) => new(dateTimeOffset.Year, dat |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Returns <see cref="DateTimeOffset"/> with changed Hour part. |
| | | 151 | | /// </summary> |
| | 0 | 152 | | public DateTimeOffset SetHour(int hour) => new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day, ho |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Returns <see cref="DateTimeOffset"/> with changed Minute part. |
| | | 156 | | /// </summary> |
| | 0 | 157 | | public DateTimeOffset SetMinute(int minute) => new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Returns <see cref="DateTimeOffset"/> with changed Second part. |
| | | 161 | | /// </summary> |
| | 0 | 162 | | public DateTimeOffset SetSecond(int second) => new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Returns <see cref="DateTimeOffset"/> with changed Millisecond part. |
| | | 166 | | /// </summary> |
| | 0 | 167 | | public DateTimeOffset SetMillisecond(int millisecond) => new(dateTimeOffset.Year, dateTimeOffset.Month, dateTime |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Returns original <see cref="DateTimeOffset"/> value with time part set to midnight (alias for <see cref="Beg |
| | | 171 | | /// </summary> |
| | 0 | 172 | | public DateTimeOffset Midnight() => dateTimeOffset.BeginningOfDay(); |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Returns original <see cref="DateTimeOffset"/> value with time part set to Noon (12:00:00h). |
| | | 176 | | /// </summary> |
| | | 177 | | /// <returns>A <see cref="DateTimeOffset"/> value with time part set to Noon (12:00:00h).</returns> |
| | 3 | 178 | | public DateTimeOffset Noon() => dateTimeOffset.SetTime(12, 0, 0, 0); |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Returns <see cref="DateTimeOffset"/> with changed Year part. |
| | | 182 | | /// </summary> |
| | 0 | 183 | | public DateTimeOffset SetDate(int year) => new(year, dateTimeOffset.Month, dateTimeOffset.Day, dateTimeOffset.Ho |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// Returns <see cref="DateTimeOffset"/> with changed Year and Month part. |
| | | 187 | | /// </summary> |
| | 0 | 188 | | public DateTimeOffset SetDate(int year, int month) => new(year, month, dateTimeOffset.Day, dateTimeOffset.Hour, |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Returns <see cref="DateTimeOffset"/> with changed Year, Month and Day part. |
| | | 192 | | /// </summary> |
| | 12 | 193 | | public DateTimeOffset SetDate(int year, int month, int day) => new(year, month, day, dateTimeOffset.Hour, dateTi |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Returns <see cref="DateTimeOffset"/> with changed Year part. |
| | | 197 | | /// </summary> |
| | 0 | 198 | | public DateTimeOffset SetYear(int year) => new(year, dateTimeOffset.Month, dateTimeOffset.Day, dateTimeOffset.Ho |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Returns <see cref="DateTimeOffset"/> with changed Month part. |
| | | 202 | | /// </summary> |
| | 0 | 203 | | public DateTimeOffset SetMonth(int month) => new(dateTimeOffset.Year, month, dateTimeOffset.Day, dateTimeOffset. |
| | | 204 | | |
| | | 205 | | /// <summary> |
| | | 206 | | /// Returns <see cref="DateTimeOffset"/> with changed Day part. |
| | | 207 | | /// </summary> |
| | 18 | 208 | | public DateTimeOffset SetDay(int day) => new(dateTimeOffset.Year, dateTimeOffset.Month, day, dateTimeOffset.Hour |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// Returns the given <see cref="DateTimeOffset"/> with hour and minutes set At given values. |
| | | 212 | | /// </summary> |
| | | 213 | | /// <param name="hour">The hour to set time to.</param> |
| | | 214 | | /// <param name="minute">The minute to set time to.</param> |
| | | 215 | | /// <returns><see cref="DateTimeOffset"/> with hour and minute set to given values.</returns> |
| | 0 | 216 | | public DateTimeOffset At(int hour, int minute) => dateTimeOffset.SetTime(hour, minute); |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Returns the given <see cref="DateTimeOffset"/> with hour and minutes and seconds set At given values. |
| | | 220 | | /// </summary> |
| | | 221 | | /// <param name="hour">The hour to set time to.</param> |
| | | 222 | | /// <param name="minute">The minute to set time to.</param> |
| | | 223 | | /// <param name="second">The second to set time to.</param> |
| | | 224 | | /// <returns><see cref="DateTimeOffset"/> with hour and minutes and seconds set to given values.</returns> |
| | 0 | 225 | | public DateTimeOffset At(int hour, int minute, int second) => dateTimeOffset.SetTime(hour, minute, second); |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Returns the given <see cref="DateTimeOffset"/> with hour and minutes and seconds and milliseconds set At giv |
| | | 229 | | /// </summary> |
| | | 230 | | /// <param name="hour">The hour to set time to.</param> |
| | | 231 | | /// <param name="minute">The minute to set time to.</param> |
| | | 232 | | /// <param name="second">The second to set time to.</param> |
| | | 233 | | /// <param name="milliseconds">The milliseconds to set time to.</param> |
| | | 234 | | /// <returns><see cref="DateTimeOffset"/> with hour and minutes and seconds set to given values.</returns> |
| | 0 | 235 | | public DateTimeOffset At(int hour, int minute, int second, int milliseconds) => dateTimeOffset.SetTime(hour, min |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Sets the day of the <see cref="DateTimeOffset"/> to the first day in that calendar quarter. |
| | | 239 | | /// credit to http://www.devcurry.com/2009/05/find-first-and-last-day-of-current.html. |
| | | 240 | | /// </summary> |
| | | 241 | | /// <returns>given <see cref="DateTimeOffset"/> with the day part set to the first day in the quarter.</returns> |
| | | 242 | | public DateTimeOffset FirstDayOfQuarter() |
| | | 243 | | { |
| | 0 | 244 | | var currentQuarter = ((dateTimeOffset.Month - 1) / 3) + 1; |
| | 0 | 245 | | return dateTimeOffset.SetDate(dateTimeOffset.Year, (3 * currentQuarter) - 2, 1); |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | /// <summary> |
| | | 249 | | /// Sets the day of the <see cref="DateTimeOffset"/> to the first day in that month. |
| | | 250 | | /// </summary> |
| | | 251 | | /// <returns>given <see cref="DateTimeOffset"/> with the day part set to the first day in that month.</returns> |
| | 3 | 252 | | public DateTimeOffset FirstDayOfMonth() => dateTimeOffset.SetDay(1); |
| | | 253 | | |
| | | 254 | | /// <summary> |
| | | 255 | | /// Sets the day of the <see cref="DateTimeOffset"/> to the last day in that calendar quarter. |
| | | 256 | | /// credit to http://www.devcurry.com/2009/05/find-first-and-last-day-of-current.html. |
| | | 257 | | /// </summary> |
| | | 258 | | /// <returns>given <see cref="DateTimeOffset"/> with the day part set to the last day in the quarter.</returns> |
| | | 259 | | public DateTimeOffset LastDayOfQuarter() |
| | | 260 | | { |
| | 0 | 261 | | var currentQuarter = ((dateTimeOffset.Month - 1) / 3) + 1; |
| | 0 | 262 | | var firstDay = dateTimeOffset.SetDate(dateTimeOffset.Year, (3 * currentQuarter) - 2, 1); |
| | 0 | 263 | | return firstDay.SetMonth(firstDay.Month + 2).LastDayOfMonth(); |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Sets the day of the <see cref="DateTimeOffset"/> to the last day in that month. |
| | | 268 | | /// </summary> |
| | | 269 | | /// <returns>given <see cref="DateTimeOffset"/> with the day part set to the last day in that month.</returns> |
| | 9 | 270 | | public DateTimeOffset LastDayOfMonth() => dateTimeOffset.SetDay(DateTime.DaysInMonth(dateTimeOffset.Year, dateTi |
| | | 271 | | |
| | | 272 | | /// <summary> |
| | | 273 | | /// Adds the given number of business days to the <see cref="DateTimeOffset"/>. |
| | | 274 | | /// </summary> |
| | | 275 | | /// <param name="days">Number of business days to be added.</param> |
| | | 276 | | /// <returns>A <see cref="DateTimeOffset"/> increased by a given number of business days.</returns> |
| | | 277 | | public DateTimeOffset AddBusinessDays(int days) |
| | | 278 | | { |
| | 3 | 279 | | var sign = Math.Sign(days); |
| | 3 | 280 | | var unsignedDays = Math.Abs(days); |
| | 12 | 281 | | for (var i = 0; i < unsignedDays; i++) |
| | | 282 | | { |
| | | 283 | | do |
| | 9 | 284 | | dateTimeOffset = dateTimeOffset.AddDays(sign); |
| | 9 | 285 | | while (dateTimeOffset.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday); |
| | | 286 | | } |
| | | 287 | | |
| | 3 | 288 | | return dateTimeOffset; |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | /// <summary> |
| | | 292 | | /// Subtracts the given number of business days to the <see cref="DateTimeOffset"/>. |
| | | 293 | | /// </summary> |
| | | 294 | | /// <param name="days">Number of business days to be subtracted.</param> |
| | | 295 | | /// <returns>A <see cref="DateTimeOffset"/> increased by a given number of business days.</returns> |
| | 0 | 296 | | public DateTimeOffset SubtractBusinessDays(int days) => dateTimeOffset.AddBusinessDays(-days); |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// Determine if a <see cref="DateTimeOffset"/> is in the future. |
| | | 300 | | /// </summary> |
| | | 301 | | /// <returns><c>true</c> if <paramref name="dateTimeOffset"/> is in the future; otherwise <c>false</c>.</returns |
| | 3 | 302 | | public bool IsInFuture() => dateTimeOffset > DateTimeOffset.Now; |
| | | 303 | | |
| | | 304 | | /// <summary> |
| | | 305 | | /// Determine if a <see cref="DateTimeOffset"/> is in the past. |
| | | 306 | | /// </summary> |
| | | 307 | | /// <returns><c>true</c> if <paramref name="dateTimeOffset"/> is in the past; otherwise <c>false</c>.</returns> |
| | 3 | 308 | | public bool IsInPast() => dateTimeOffset < DateTimeOffset.Now; |
| | | 309 | | |
| | | 310 | | /// <summary> |
| | | 311 | | /// Rounds <paramref name="dateTimeOffset"/> to the nearest <see cref="RoundTo"/>. |
| | | 312 | | /// </summary> |
| | | 313 | | /// <returns>The rounded <see cref="DateTimeOffset"/>.</returns> |
| | | 314 | | public DateTimeOffset Round(RoundTo rt) |
| | | 315 | | { |
| | | 316 | | DateTimeOffset rounded; |
| | | 317 | | |
| | | 318 | | switch (rt) |
| | | 319 | | { |
| | | 320 | | case RoundTo.Second: |
| | | 321 | | { |
| | 0 | 322 | | rounded = new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day, dateTimeOffset.Hour |
| | 0 | 323 | | if (dateTimeOffset.Millisecond >= 500) |
| | 0 | 324 | | rounded = rounded.AddSeconds(1); |
| | 0 | 325 | | break; |
| | | 326 | | } |
| | | 327 | | |
| | | 328 | | case RoundTo.Minute: |
| | | 329 | | { |
| | 3 | 330 | | rounded = new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day, dateTimeOffset.Hour |
| | 3 | 331 | | if (dateTimeOffset.Second >= 30) |
| | 3 | 332 | | rounded = rounded.AddMinutes(1); |
| | 3 | 333 | | break; |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | case RoundTo.Hour: |
| | | 337 | | { |
| | 0 | 338 | | rounded = new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day, dateTimeOffset.Hour |
| | 0 | 339 | | if (dateTimeOffset.Minute >= 30) |
| | 0 | 340 | | rounded = rounded.AddHours(1); |
| | 0 | 341 | | break; |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | case RoundTo.Day: |
| | | 345 | | { |
| | 0 | 346 | | rounded = new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day, 0, 0, 0, dateTimeOf |
| | 0 | 347 | | if (dateTimeOffset.Hour >= 12) |
| | 0 | 348 | | rounded = rounded.AddDays(1); |
| | 0 | 349 | | break; |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | default: |
| | | 353 | | { |
| | 0 | 354 | | throw new ArgumentOutOfRangeException(nameof(rt)); |
| | | 355 | | } |
| | | 356 | | } |
| | | 357 | | |
| | 3 | 358 | | return rounded; |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | /// <summary> |
| | | 362 | | /// Returns a DateTimeOffset adjusted to the beginning of the week. |
| | | 363 | | /// </summary> |
| | | 364 | | /// <returns>A DateTimeOffset instance adjusted to the beginning of the current week.</returns> |
| | | 365 | | /// <remarks>the beginning of the week is controlled by the current Culture.</remarks> |
| | | 366 | | public DateTimeOffset FirstDayOfWeek() |
| | | 367 | | { |
| | 6 | 368 | | var currentCulture = CultureInfo.CurrentCulture; |
| | 6 | 369 | | var firstDayOfWeek = currentCulture.DateTimeFormat.FirstDayOfWeek; |
| | 6 | 370 | | var offset = dateTimeOffset.DayOfWeek < firstDayOfWeek ? 7 : 0; |
| | 6 | 371 | | var numberOfDaysSinceBeginningOfTheWeek = dateTimeOffset.DayOfWeek + offset - firstDayOfWeek; |
| | | 372 | | |
| | 6 | 373 | | return dateTimeOffset.AddDays(-numberOfDaysSinceBeginningOfTheWeek); |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | /// <summary> |
| | | 377 | | /// Returns the first day of the year keeping the time component intact. Eg, 2011-02-04T06:40:20.005 => 2011-01- |
| | | 378 | | /// </summary> |
| | 3 | 379 | | public DateTimeOffset FirstDayOfYear() => dateTimeOffset.SetDate(dateTimeOffset.Year, 1, 1); |
| | | 380 | | |
| | | 381 | | /// <summary> |
| | | 382 | | /// Returns the last day of the week keeping the time component intact. Eg, 2011-12-24T06:40:20.005 => 2011-12-2 |
| | | 383 | | /// </summary> |
| | 3 | 384 | | public DateTimeOffset LastDayOfWeek() => dateTimeOffset.FirstDayOfWeek().AddDays(6); |
| | | 385 | | |
| | | 386 | | /// <summary> |
| | | 387 | | /// Returns the last day of the year keeping the time component intact. Eg, 2011-12-24T06:40:20.005 => 2011-12-3 |
| | | 388 | | /// </summary> |
| | 3 | 389 | | public DateTimeOffset LastDayOfYear() => dateTimeOffset.SetDate(dateTimeOffset.Year, 12, 31); |
| | | 390 | | |
| | | 391 | | /// <summary> |
| | | 392 | | /// Returns the previous month keeping the time component intact. Eg, 2010-01-20T06:40:20.005 => 2009-12-20T06:4 |
| | | 393 | | /// If the previous month doesn't have that many days the last day of the previous month is used. Eg, 2009-03-31 |
| | | 394 | | /// </summary> |
| | | 395 | | public DateTimeOffset PreviousMonth() |
| | | 396 | | { |
| | 3 | 397 | | var year = dateTimeOffset.Month == 1 ? dateTimeOffset.Year - 1 : dateTimeOffset.Year; |
| | | 398 | | |
| | 3 | 399 | | var month = dateTimeOffset.Month == 1 ? 12 : dateTimeOffset.Month - 1; |
| | | 400 | | |
| | 3 | 401 | | var firstDayOfPreviousMonth = dateTimeOffset.SetDate(year, month, 1); |
| | | 402 | | |
| | 3 | 403 | | var lastDayOfPreviousMonth = firstDayOfPreviousMonth.LastDayOfMonth().Day; |
| | | 404 | | |
| | 3 | 405 | | var day = dateTimeOffset.Day > lastDayOfPreviousMonth ? lastDayOfPreviousMonth : dateTimeOffset.Day; |
| | | 406 | | |
| | 3 | 407 | | return firstDayOfPreviousMonth.SetDay(day); |
| | | 408 | | } |
| | | 409 | | |
| | | 410 | | /// <summary> |
| | | 411 | | /// Returns the next month keeping the time component intact. Eg, 2012-12-05T06:40:20.005 => 2013-01-05T06:40:20 |
| | | 412 | | /// If the next month doesn't have that many days the last day of the next month is used. Eg, 2013-01-31T06:40:2 |
| | | 413 | | /// </summary> |
| | | 414 | | public DateTimeOffset NextMonth() |
| | | 415 | | { |
| | 3 | 416 | | var year = dateTimeOffset.Month == 12 ? dateTimeOffset.Year + 1 : dateTimeOffset.Year; |
| | | 417 | | |
| | 3 | 418 | | var month = dateTimeOffset.Month == 12 ? 1 : dateTimeOffset.Month + 1; |
| | | 419 | | |
| | 3 | 420 | | var firstDayOfNextMonth = dateTimeOffset.SetDate(year, month, 1); |
| | | 421 | | |
| | 3 | 422 | | var lastDayOfPreviousMonth = firstDayOfNextMonth.LastDayOfMonth().Day; |
| | | 423 | | |
| | 3 | 424 | | var day = dateTimeOffset.Day > lastDayOfPreviousMonth ? lastDayOfPreviousMonth : dateTimeOffset.Day; |
| | | 425 | | |
| | 3 | 426 | | return firstDayOfNextMonth.SetDay(day); |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | /// <summary> |
| | | 430 | | /// Determines whether the specified <see cref="DateTimeOffset"/> value is exactly the same day (day + month + y |
| | | 431 | | /// </summary> |
| | | 432 | | /// <param name="date">Value to compare with.</param> |
| | | 433 | | /// <returns> |
| | | 434 | | /// <c>true</c> if the specified date is exactly the same year then current; otherwise, <c>false</c>. |
| | | 435 | | /// </returns> |
| | 3 | 436 | | public bool SameDay(DateTimeOffset date) => dateTimeOffset.Date == date.Date; |
| | | 437 | | |
| | | 438 | | /// <summary> |
| | | 439 | | /// Determines whether the specified <see cref="DateTimeOffset"/> value is exactly the same month (month + year) |
| | | 440 | | /// </summary> |
| | | 441 | | /// <param name="date">Value to compare with.</param> |
| | | 442 | | /// <returns> |
| | | 443 | | /// <c>true</c> if the specified date is exactly the same month and year then current; otherwise, <c>false</ |
| | | 444 | | /// </returns> |
| | 3 | 445 | | public bool SameMonth(DateTimeOffset date) => dateTimeOffset.Month == date.Month && dateTimeOffset.Year == date. |
| | | 446 | | |
| | | 447 | | /// <summary> |
| | | 448 | | /// Determines whether the specified <see cref="DateTimeOffset"/> value is exactly the same year then current. E |
| | | 449 | | /// </summary> |
| | | 450 | | /// <param name="date">Value to compare with.</param> |
| | | 451 | | /// <returns> |
| | | 452 | | /// <c>true</c> if the specified date is exactly the same date then current; otherwise, <c>false</c>. |
| | | 453 | | /// </returns> |
| | 3 | 454 | | public bool SameYear(DateTimeOffset date) => dateTimeOffset.Year == date.Year; |
| | | 455 | | } |
| | | 456 | | } |
| | | 457 | | |