| | | 1 | | namespace ZhEaIsNsAaBn.Infrastructure.Core; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Hijri column text shared by the Hijri converters: written as <c>yyyy-MM-dd</c> or 24-hour <c>yyyy-MM-dd HH:mm:ss</c> |
| | | 5 | | /// invariant separators; read in those forms and in the old 12-hour <c>yyyy-MM-dd hh:mm:ss tt</c> form (<c>ص</c>/<c>م</ |
| | | 6 | | /// </summary> |
| | | 7 | | internal static class HijriText |
| | | 8 | | { |
| | | 9 | | /// <summary>Length of the date-only text.</summary> |
| | | 10 | | internal const int DateLength = 10; |
| | | 11 | | |
| | | 12 | | /// <summary>Length of the date-and-time text.</summary> |
| | | 13 | | internal const int DateTimeLength = 19; |
| | | 14 | | |
| | | 15 | | private const string DateFormat = "yyyy'-'MM'-'dd"; |
| | | 16 | | private const string DateTimeFormat = "yyyy'-'MM'-'dd HH':'mm':'ss"; |
| | | 17 | | |
| | 4 | 18 | | private static readonly string[] _readFormats = [ DateTimeFormat, "yyyy'-'MM'-'dd hh':'mm':'ss tt", DateFormat ]; |
| | | 19 | | |
| | 4 | 20 | | private static readonly DateTimeFormatInfo _arabicDesignators = HijriFormat( null, null ); |
| | 4 | 21 | | private static readonly DateTimeFormatInfo _latinDesignators = HijriFormat( "AM", "PM" ); |
| | | 22 | | |
| | | 23 | | |
| | | 24 | | /// <summary>Formats <paramref name="date"/> for the column.</summary> |
| | 40 | 25 | | internal static string Format( HijriDate date, bool withTime ) => date.ToString( withTime ? DateTimeFormat : DateFor |
| | | 26 | | |
| | | 27 | | /// <summary>Reads column text written by any version of the converters.</summary> |
| | | 28 | | /// <exception cref="HijriDateException"><paramref name="text"/> isn't a supported Hijri date.</exception> |
| | | 29 | | internal static HijriDate Parse( string text ) |
| | | 30 | | { |
| | 200 | 31 | | foreach ( var format in new[] { _arabicDesignators, _latinDesignators } ) |
| | | 32 | | { |
| | 132 | 33 | | if ( DateTime.TryParseExact( text, _readFormats, format, DateTimeStyles.AllowWhiteSpaces, out var parsed ) ) |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | |
| | 0 | 37 | | return new HijriDate( text, isHijri: true ); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | |
| | | 41 | | private static DateTimeFormatInfo HijriFormat( string? am, string? pm ) |
| | | 42 | | { |
| | 8 | 43 | | var format = (DateTimeFormatInfo) CultureInfo.GetCultureInfo( "ar-SA" ).DateTimeFormat.Clone(); |
| | | 44 | | |
| | 8 | 45 | | format.Calendar = new HijriCalendar(); |
| | | 46 | | |
| | | 47 | | |
| | 12 | 48 | | if ( am is not null ) { format.AMDesignator = am; } |
| | | 49 | | |
| | | 50 | | |
| | 12 | 51 | | if ( pm is not null ) { format.PMDesignator = pm; } |
| | | 52 | | |
| | | 53 | | |
| | 8 | 54 | | return format; |
| | | 55 | | } |
| | | 56 | | } |