| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateTimeExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using MyNet.Primitives; |
| | | 8 | | using TimeOnly = System.TimeOnly; |
| | | 9 | | using TimeZoneInfo = System.TimeZoneInfo; |
| | | 10 | | |
| | | 11 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 12 | | namespace MyNet.Globalization.Facade; |
| | | 13 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 14 | | |
| | | 15 | | public static class DateTimeExtensions |
| | | 16 | | { |
| | | 17 | | extension(System.DateTime dateTime) |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Converts the specified <see cref="DateTime"/> to the application's current time zone. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <returns>The converted <see cref="DateTime"/> in the configured current time zone.</returns> |
| | 0 | 23 | | public System.DateTime ToTimeZone(TimeZoneInfo? timeZone = null) => GlobalizationServices.Current.Convert(dateTi |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Determines whether the specified <see cref="DateTime"/> falls on the current day in the application's curren |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="timeZone">The time zone to use for the comparison. If null, the application's current time zone |
| | | 29 | | /// <returns>True if the specified <see cref="DateTime"/> falls on the current day in the specified or current t |
| | 0 | 30 | | public bool IsTodayForTimeZone(TimeZoneInfo? timeZone = null) => dateTime.SameDay(dateTime.ToTimeZone(timeZone)) |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | extension(TimeOnly time) |
| | | 34 | | { |
| | | 35 | | /// <summary> |
| | | 36 | | /// Converts the given <see cref="DateTime"/> from a source time zone to a destination time zone. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="sourceTimeZone">The source time zone.</param> |
| | | 39 | | /// <param name="destinationTimeZone">The destination time zone.</param> |
| | | 40 | | /// <returns>The converted <see cref="TimeOnly"/> value.</returns> |
| | | 41 | | public TimeOnly ToTimeZone(TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone) |
| | 0 | 42 | | => GlobalizationServices.Current.Convert(System.DateTime.UtcNow.ToTimeZone(sourceTimeZone).At(time), sourceT |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Converts the given <see cref="DateTime"/> from a source time zone to the current time zone. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="sourceTimeZone">The source time zone.</param> |
| | | 48 | | /// <returns>The converted <see cref="System.TimeOnly"/> value.</returns> |
| | 0 | 49 | | public TimeOnly ToCurrentTime(TimeZoneInfo sourceTimeZone) => time.ToTimeZone(sourceTimeZone, GlobalizationServi |
| | | 50 | | } |
| | | 51 | | } |
| | | 52 | | |