public static bool ParseDateTime(this string text, DateTimeZone zone, string cultureTab, bool withoutDaylightSaving, MxCultureInfo.FormatType formatType, bool longFormat, out Instant result) { var rc = false; result = InstantError; if ((zone != null) && (String.IsNullOrEmpty(cultureTab) == false) && (formatType != MxCultureInfo.FormatType.Date) && (formatType != MxCultureInfo.FormatType.Time)) { try { var culture = MxCultureInfo.Instance.GetCultureInfo(cultureTab); if (culture != null) { var parseResult = LocalDateTimePattern.Create(MxCultureInfo.GetFormatSpecifier(formatType, longFormat), MxCultureInfo.Instance.GetCultureInfo(cultureTab)).Parse(text); //var temp = parseResult.Value; if (parseResult.Success) { var instant = parseResult.Value.InZoneStrictly(zone).ToInstant(); var local = instant.InZone(zone).LocalDateTime; if (withoutDaylightSaving && zone.IsDaylightSavingsTime(instant)) { local = local.Plus(Period.FromSeconds(zone.GetZoneInterval(instant).Savings.Seconds)); } result = local.InZoneStrictly(zone).ToInstant(); rc = true; } } } catch (Exception) { //ignore } } return(rc); }
public Instant ParseTimeDate(string timeDate, bool withoutDaylightSaving = false, MxCultureInfo.FormatType formatType = MxCultureInfo.FormatType.DateTime, bool longFormat = false, string cultureTab = null) { var rc = ExtNodatime.InstantError; var cookies = new MxCookies(HttpContextAccessor); if (SupportedCultures.IsSupported(cultureTab) == false) { cultureTab = SupportedCultures.GetCultureTab(cookies.GetValue(MxSupportedCultures.CookieName)); } if (timeDate.ParseDateTime(DateTimeZoneProviders.Tzdb[SupportedTimeZones.GetTzDbName(cultureTab)], cultureTab, withoutDaylightSaving, formatType, longFormat, out var result)) { rc = result; } return(rc); }
public static string ToString(this Instant instant, string cultureTab, DateTimeZone zone, bool withoutDaylightSaving = false, MxCultureInfo.FormatType formatType = MxCultureInfo.FormatType.DateTime, bool longFormat = false) { var rc = "[error]"; if ((zone != null) && (String.IsNullOrEmpty(cultureTab) == false)) { try { var culture = MxCultureInfo.Instance.GetCultureInfo(cultureTab); if (culture != null) { var local = instant.InZone(zone).LocalDateTime; if (withoutDaylightSaving && zone.IsDaylightSavingsTime(instant)) { local = local.Minus(Period.FromSeconds(zone.GetZoneInterval(instant).Savings.Seconds)); } // ReSharper disable once ReturnValueOfPureMethodIsNotUsed zone.AtStrictly(local); //throws exception if ambiguous or invalid due to daylight saving transition //In all formats except Date the setting of longFormat (capital letter) causes display of hours, minutes and seconds in 24 hour clock //FormatType.Date: en-GB: D=Sunday, 29 March 2020 d=29-03-2020 //FormatType.Time: en-GB: T=00:59:59 t=12:59 AM //FormatType.DateTime: en-GB: G=29-03-2020 00:59:59 g=29-03-2020 12:59 AM //FormatType.Verbose: en-GB: F=Sunday, 29 March 2020 00:59:59 f=Sunday, 29 March 2020 12:59 AM //FormatType.Machine: *: r=2020-03-29T00:59:59.000000000(ISO) s=2020-03-29T00 59:59 if (formatType == MxCultureInfo.FormatType.Date) { rc = local.Date.ToString(MxCultureInfo.GetFormatSpecifier(formatType, longFormat), culture); } else if (formatType == MxCultureInfo.FormatType.Time) { rc = local.TimeOfDay.ToString(MxCultureInfo.GetFormatSpecifier(formatType, longFormat), culture); } else { rc = local.ToString(MxCultureInfo.GetFormatSpecifier(formatType, longFormat), culture); } } } catch (SkippedTimeException) { rc = "[error: invalid time]"; } catch (AmbiguousTimeException) { rc = "[error: ambiguous time]"; } catch (Exception e) { rc = e.Message; } } return(rc); }
public string InstantToString(Instant timeInstant, string cultureTab = null, bool withoutDaylightSaving = false, MxCultureInfo.FormatType formatType = MxCultureInfo.FormatType.DateTime, bool longFormat = false) { var rc = "[error]"; try { var cookies = new MxCookies(HttpContextAccessor); if (SupportedCultures.IsSupported(cultureTab) == false) { cultureTab = SupportedCultures.GetCultureTab(cookies.GetValue(MxSupportedCultures.CookieName)); } rc = timeInstant.ToString(cultureTab, DateTimeZoneProviders.Tzdb[SupportedTimeZones.GetTzDbName(cultureTab)]); } catch (Exception) { //ignore } return(rc); }