Пример #1
0
        public static bool ParseDate(this string text, DateTimeZone zone, string cultureTab, bool longFormat, out Instant result)
        {
            var rc = false;

            result = InstantError;

            if ((zone != null) && (String.IsNullOrEmpty(cultureTab) == false))
            {
                try
                {
                    var culture = MxCultureInfo.Instance.GetCultureInfo(cultureTab);
                    if (culture != null)
                    {
                        var parseResult = LocalDatePattern.Create(MxCultureInfo.GetFormatSpecifier(MxCultureInfo.FormatType.Date, longFormat), MxCultureInfo.Instance.GetCultureInfo(cultureTab)).Parse(text);
                        if (parseResult.Success)
                        {
                            var instant = parseResult.Value.AtMidnight().InZoneStrictly(zone).ToInstant();
                            var local   = instant.InZone(zone).LocalDateTime;
                            if (zone.IsDaylightSavingsTime(instant))
                            {
                                local = local.PlusSeconds(zone.GetZoneInterval(instant).Savings.Seconds);
                            }
                            result = local.InZoneStrictly(zone).ToInstant();
                            rc     = true;
                        }
                    }
                }
                catch (Exception)
                {
                    //ignore
                }
            }
            return(rc);
        }
Пример #2
0
        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);
        }