Ejemplo n.º 1
0
    internal Bar(JsonElement json)
    {
        string start = json.GetProperty("start").GetString() ?? throw new InvalidDataException("Missing property: 'start'");

        Start = TimePattern.Parse(start).Value;

        string end = json.GetProperty("end").GetString() ?? throw new InvalidDataException("Missing property: 'end'");

        End = TimePattern.Parse(end).Value;

        string type = json.GetProperty("type").GetString() ?? throw new InvalidDataException("Missing property: 'type'");

        BarSize = Enum.Parse <BarSize>(type);

        bool hasLabel = json.TryGetProperty("label", out JsonElement label);

        if (BarSize == BarSize.L)
        {
            Label = label.GetString() ?? throw new InvalidDataException("Missing property: 'label'");
        }
        else if (hasLabel)
        {
            throw new JsonException($"Bar label will not be displayed for BarHeight={BarSize}.");
        }
    }
Ejemplo n.º 2
0
        internal Bar(JsonElement json)
        {
            var start = json.GetProperty("start").GetString();

            Start = TimePattern.Parse(start).Value;

            var end = json.GetProperty("end").GetString();

            End = TimePattern.Parse(end).Value;

            var type = json.GetProperty("type").GetString();

            BarSize = (BarSize)Enum.Parse(typeof(BarSize), type);

            var hasLabel = json.TryGetProperty("label", out var label);

            if (BarSize == BarSize.L)
            {
                Label = label.GetString();
            }
            else if (hasLabel)
            {
                throw new JsonException($"Bar label will not be displayed for BarHeight={BarSize}.");
            }
        }
Ejemplo n.º 3
0
        public static void ApplyEditChanges(this ScheduledStream model,
                                            ScheduledStreamEditModel viewModel)
        {
            var parsedStart = TimePattern.Parse(viewModel.LocalStartTime);
            var parsedEnd   = TimePattern.Parse(viewModel.LocalEndTime);

            model.DayOfWeek      = viewModel.DayOfWeek;
            model.LocalStartTime = parsedStart.Value;
            model.LocalEndTime   = parsedEnd.Value;
        }
        public ScheduledStream ToModel()
        {
            var parsedStart    = TimePattern.Parse(LocalStartTime);
            var parsedEnd      = TimePattern.Parse(LocalEndTime);
            var localStartTime = parsedStart.Value;
            var localEndTime   = parsedEnd.Value;

            return(new ScheduledStream
            {
                DayOfWeek = DayOfWeek,
                LocalStartTime = localStartTime,
                LocalEndTime = localEndTime,
            });
        }
Ejemplo n.º 5
0
        internal Notification(JsonElement json)
        {
            string time = json.GetProperty("time").GetString() ?? throw new InvalidDataException("Missing property: 'time'");

            Time = TimePattern.Parse(time).Value;
            Text = json.GetProperty("text").GetString() ?? throw new InvalidDataException("Missing property: 'text'");
        }
            static (LocalTime start, LocalTime end) GetSessionTime(string session)
            {
                var times = session.Split('-').Select(t => TimePattern.Parse(t).Value).ToList();

                Debug.Assert(times.Count == 2);
                return(times[0], times[1]);
            }
Ejemplo n.º 7
0
        internal Notification(JsonElement json, DateTimeZone timeZone)
        {
            var time = json.GetProperty("time").GetString();

            Time = TimePattern.Parse(time).Value;
            Text = json.GetProperty("text").GetString();
        }
Ejemplo n.º 8
0
        private static List <(LocalDateTime start, LocalDateTime end)> GetSessions(string s)
        {
            var list = new List <(LocalDateTime start, LocalDateTime end)>();

            if (string.IsNullOrEmpty(s))
            {
                return(list);
            }

            var days = s.Split(';');

            foreach (var day in days)
            {
                var parts = day.Split(':');
                Debug.Assert(parts.Count() == 2);
                var date     = DatePattern.Parse(parts[0]).Value;
                var sessions = parts[1].Split(',').Distinct(); // the sessions may be repeated(?)
                foreach (var session in sessions.Where(x => x != "CLOSED"))
                {
                    var times = GetSessionTime(session);
                    var start = date.At(times.start);
                    var end   = date.At(times.end);
                    if (end < start)
                    {
                        start = start.PlusDays(-1);                               //end = end.PlusDays(1); ??
                    }
                    if (list.Any() && (start <= list.Last().end || end <= start)) // ensure consecutive, non-overlapping periods
                    {
                        throw new InvalidDataException("Invalid period.");
                    }
                    list.Add((start, end));
                }
            }
            return(list);

            // local
            (LocalTime start, LocalTime end) GetSessionTime(string session)
            {
                var times = session.Split('-').Select(t => TimePattern.Parse(t).Value).ToList();

                Debug.Assert(times.Count == 2);
                return(times[0], times[1]);
            }
        }
Ejemplo n.º 9
0
        private object ParseImpl <T>(string s)
        {
            if (s == null)
            {
                throw new ArgumentNullException(nameof(s));
            }

            var type = typeof(T);

            if (type == typeof(string))
            {
                return(s);
            }

            if (type == typeof(LocalTime))
            {
                return(TimePattern.Parse(s).Value);
            }

            if (type == typeof(LocalDateTime))
            {
                return(DateTimePattern.Parse(s).Value);
            }

            if (type == typeof(bool))
            {
                if (s == "0" || string.Compare(s, "false", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return(false);
                }
                if (s == "1" || string.Compare(s, "true", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return(true);
                }
                throw new InvalidDataException($"Parse<bool>('{s}') failure.");
            }

            if (type == typeof(char))
            {
                if (s.Length == 1)
                {
                    return(s[0]);
                }
                throw new InvalidDataException($"Parse<char>('{s}') failure.");
            }

            if (type.GetTypeInfo().IsEnum)
            {
                return(ParseEnum(type, s));
            }

            var utype = Nullable.GetUnderlyingType(type);

            if (utype != null)
            {
                if (utype != typeof(int) && utype != typeof(long) && utype != typeof(double))
                {
                    throw new InvalidDataException($"Parse: '{utype.Name}' nullable is not supported.");
                }
                if (s.Length == 0)
                {
                    return(null);
                }
                type = utype;
            }

            if (s.Length == 0)
            {
                return(0);
            }

            if (type == typeof(int))
            {
                if (int.TryParse(s, NumberStyles.AllowLeadingSign, NumberFormatInfo.InvariantInfo, out int n))
                {
                    return(n);
                }
            }
            else if (type == typeof(long))
            {
                if (long.TryParse(s, NumberStyles.AllowLeadingSign, NumberFormatInfo.InvariantInfo, out long n))
                {
                    return(n);
                }
            }
            else if (type == typeof(double))
            {
                if (double.TryParse(s, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out double n))
                {
                    return(n);
                }
            }

            throw new InvalidDataException($"Parse<{typeof(T).Name}>('{s}') failure.");
        }
 public override object FromStringValue(string xml)
 {
     return(_timePattern.Parse(xml).Value);
 }
Ejemplo n.º 11
0
 internal LocalTime ReadLocalTime(LocalTimePattern p) => p.Parse(ReadString()).GetValueOrThrow();
Ejemplo n.º 12
0
 public bool IsValid(string literal)
 {
     return(_hasOffset ? _offsetPattern.Parse(literal).Success : _localPattern.Parse(literal).Success);
 }
Ejemplo n.º 13
0
 public void IsoPatternToSecond_Parse() =>
 IsoPatternToSecond.Parse(SampleIsoFormattedTime);
Ejemplo n.º 14
0
 // local
 static (LocalTime start, LocalTime end) GetSessionTime(string session)
 {
     LocalTime[] times = session.Split('-').Select(t => TimePattern.Parse(t).Value).ToArray();
     Debug.Assert(times.Length == 2);
     return(times[0], times[1]);
 }