| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateTimeHelper.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Linq; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Primitives.Temporal; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Helper class for DateTime operations. |
| | | 14 | | /// </summary> |
| | | 15 | | public static class DateTimeHelper |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Defines the number of days in a week, and the maximum and minimum number of weeks in a month. |
| | | 19 | | /// </summary> |
| | | 20 | | public const int DaysPerWeek = 7; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Defines the average number of days in a year and a month. The average number of days in a year is calculated bas |
| | | 24 | | /// </summary> |
| | | 25 | | public const double DaysPerYear = 365.2425; // see https://en.wikipedia.org/wiki/Gregorian_calendar |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Defines the average number of days in a month, calculated by dividing the average number of days in a year by 12 |
| | | 29 | | /// </summary> |
| | | 30 | | public const double DaysPerMonth = DaysPerYear / 12; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Defines the maximum and minimum number of weeks in a month. The maximum is 6 because some months can start on th |
| | | 34 | | /// </summary> |
| | | 35 | | public const int MaxNumberOfWeeksPerMonth = 6; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Defines the minimum number of weeks in a month. The minimum is 4 because some months can start on the first day |
| | | 39 | | /// </summary> |
| | | 40 | | public const int MinNumberOfWeeksPerMonth = 4; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the DateTimeFormatInfo for the current culture, ensuring that it uses a Gregorian calendar. If the current |
| | | 44 | | /// </summary> |
| | | 45 | | /// <remarks>This method ensures that the returned DateTimeFormatInfo always uses a Gregorian calendar, which is use |
| | | 46 | | /// <returns>The DateTimeFormatInfo for the current culture, using a Gregorian calendar.</returns> |
| | | 47 | | public static DateTimeFormatInfo GetCurrentDateTimeFormatInfo() |
| | | 48 | | { |
| | 6 | 49 | | if (CultureInfo.CurrentCulture.Calendar is GregorianCalendar) return CultureInfo.CurrentCulture.DateTimeFormat; |
| | 0 | 50 | | Calendar? calendar = |
| | 0 | 51 | | CultureInfo.CurrentCulture.OptionalCalendars.OfType<GregorianCalendar>().FirstOrDefault(); |
| | 0 | 52 | | var cultureName = calendar is null ? CultureInfo.InvariantCulture.Name : CultureInfo.CurrentCulture.Name; |
| | 0 | 53 | | var dt = new CultureInfo(cultureName).DateTimeFormat; |
| | 0 | 54 | | dt.Calendar = calendar ?? new GregorianCalendar(); |
| | 0 | 55 | | return dt; |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |