public IScheduleRecord ParseRecord(string recordString)
        {
            if (string.IsNullOrWhiteSpace(recordString))
            {
                throw new ArgumentNullException(nameof(recordString));
            }

            var record = new AssociationRecord
            {
                RecordIdentity = (ScheduleRecordType)_enumPropertyParsers["ScheduleRecordType"].ParseProperty(recordString.Substring(0, 3)),
                MainTrainUid   = recordString.Substring(3, 6),
                AssocTrainUid  = recordString.Substring(9, 6),
                DateTo         = _dateTimeParser.ParseDateTime(new DateTimeParserRequest
                {
                    DateTimeFormat = "yyMMdd",
                    DateTimeString = recordString.Substring(21, 6)
                }),
                AssocDays           = (Days)_enumPropertyParsers["RunningDays"].ParseProperty(recordString.Substring(27, 7)),
                Category            = (AssociationCategory)_enumPropertyParsers["AssociationCategory"].ParseProperty(recordString.Substring(34, 2)),
                DateIndicator       = (DateIndicator)_enumPropertyParsers["DateIndicator"].ParseProperty(recordString.Substring(36, 1)),
                Location            = recordString.Substring(37, 7).Trim(),
                BaseLocationSuffix  = recordString.Substring(44, 1).Trim(),
                AssocLocationSuffix = recordString.Substring(45, 1).Trim(),
                DiagramType         = recordString.Substring(46, 1).Trim(),
                StpIndicator        = (StpIndicator)_enumPropertyParsers["StpIndicator"].ParseProperty(recordString.Substring(79, 1))
            };

            var dateFromResult = _dateTimeParser.ParseDateTime(new DateTimeParserRequest
            {
                DateTimeFormat = "yyMMdd",
                DateTimeString = recordString.Substring(15, 6)
            });

            if (dateFromResult.HasValue)
            {
                record.DateFrom = dateFromResult.Value;
            }
            else
            {
                throw new ArgumentException("Failed to parse Date From for Association record.");
            }

            return(record);
        }
示例#2
0
        public void ValidDateTimeWithPositiveTimezone_ParsedUtcTime()
        {
            string dateTime = "2017.10.19t22:02:40 +2:30Z";

            var utcTime = _parser.ParseDateTime(dateTime);

            Assert.IsTrue(utcTime.Hours == 22 &&
                          utcTime.Minutes == 2 &&
                          utcTime.Seconds == 40 &&
                          utcTime.TimeZone.Hours == 2 &&
                          utcTime.TimeZone.Minutes == 30 &&
                          utcTime.TimeZone.TimeZoneSign == 1);
        }
        private UtcTime ParseDateTime(string dateTime)
        {
            if (dateTime == null)
            {
                throw new ArgumentNullException($"{nameof(dateTime)} is null");
            }

            UtcTime time;

            try
            {
                time = dateTimeParser.ParseDateTime(dateTime);
            }
            catch (InvalidDateTimeFormatException) { throw; }
            catch (ArgumentNullException) { throw; }
            catch (Exception ex)
            {
                throw new TimeConverterException($"Cannot parse {nameof(dateTime)}", ex);
            }

            return(time);
        }
        public IScheduleRecord ParseRecord(string recordString)
        {
            if (string.IsNullOrWhiteSpace(recordString))
            {
                throw new ArgumentNullException(nameof(recordString));
            }

            var record = new HeaderRecord
            {
                RecordIdentity    = ScheduleRecordType.HD,
                MainFrameIdentity = recordString.Substring(2, 20),
                TimeOfExtract     = recordString.Substring(28, 4)
            };

            var mainFrameUserRegex = new Regex("TPS.U(.{6}).PD(.{6})");

            if (!mainFrameUserRegex.IsMatch(record.MainFrameIdentity))
            {
                throw new InvalidOperationException("The main frame id is not valid in the header record.");
            }

            var dateOfExtractResult = _dateTimeParser.ParseDateTime(new DateTimeParserRequest
            {
                DateTimeFormat = "ddMMyy",
                DateTimeString = recordString.Substring(22, 6)
            });

            if (dateOfExtractResult.HasValue)
            {
                record.DateOfExtract = dateOfExtractResult.Value;
            }
            else
            {
                throw new ArgumentException("Failed to parse Date of Extract in Header Record");
            }

            var userExtractStartDateResult = _dateTimeParser.ParseDateTime(new DateTimeParserRequest
            {
                DateTimeFormat = "ddMMyy",
                DateTimeString = recordString.Substring(48, 6)
            });

            if (userExtractStartDateResult.HasValue)
            {
                record.UserExtractStartDate = userExtractStartDateResult.Value;
            }
            else
            {
                throw new ArgumentException("Failed to parse User Extract Start Date in Header Record");
            }

            var userExtractEndDateResult = _dateTimeParser.ParseDateTime(new DateTimeParserRequest
            {
                DateTimeFormat = "ddMMyy",
                DateTimeString = recordString.Substring(54, 6)
            });

            if (userExtractEndDateResult.HasValue)
            {
                record.UserExtractEndDate = userExtractEndDateResult.Value;
            }
            else
            {
                throw new ArgumentException("Failed to parse User Extract End Date in Header Record");
            }

            var mainFrameExtractDateResult = _dateTimeParser.ParseDateTime(new DateTimeParserRequest
            {
                DateTimeFormat = "yyMMdd",
                DateTimeString = record.MainFrameIdentity.Substring(14, 6)
            });

            if (mainFrameExtractDateResult.HasValue)
            {
                record.MainFrameExtractDate = mainFrameExtractDateResult.Value;
            }
            else
            {
                throw new ArgumentException("Failed to parse User Extract End Date in Header Record");
            }

            record.CurrentFileRef     = recordString.Substring(32, 7);
            record.LastFileRef        = recordString.Substring(39, 7);
            record.ExtractUpdateType  = (ExtractUpdateType)_enumPropertyParsers["ExtractUpdateType"].ParseProperty(recordString.Substring(46, 1));
            record.CifSoftwareVersion = recordString.Substring(47, 1);

            record.MainFrameUser = record.MainFrameIdentity.Substring(5, 6);

            return(record);
        }
示例#5
0
        public IScheduleRecord ParseRecord(string recordString)
        {
            if (string.IsNullOrWhiteSpace(recordString))
            {
                throw new ArgumentNullException(nameof(recordString));
            }

            var record = new ScheduleRecord
            {
                RecordIdentity = (ScheduleRecordType)_enumPropertyParsers["ScheduleRecordType"].ParseProperty(recordString.Substring(0, 3)),
                TrainUid       = recordString.Substring(3, 6),
                DateRunsTo     = _dateTimeParser.ParseDateTime(new DateTimeParserRequest
                {
                    DateTimeFormat = "yyMMdd",
                    DateTimeString = recordString.Substring(15, 6)
                }),
                RunningDays                    = (Days)_enumPropertyParsers["RunningDays"].ParseProperty(recordString.Substring(21, 7)),
                BankHolidayRunning             = (BankHolidayRunning)_enumPropertyParsers["BankHolidayRunning"].ParseProperty(recordString.Substring(28, 1)),
                TrainStatus                    = recordString.Substring(29, 1).Trim(),
                TrainCategory                  = recordString.Substring(30, 2).Trim(),
                TrainIdentity                  = recordString.Substring(32, 4).Trim(),
                HeadCode                       = recordString.Substring(36, 4).Trim(),
                CourseIndicator                = recordString.Substring(40, 1).Trim(),
                TrainServiceCode               = recordString.Substring(41, 8).Trim(),
                PortionId                      = recordString.Substring(49, 1).Trim(),
                PowerType                      = (PowerType)_enumPropertyParsers["PowerType"].ParseProperty(recordString.Substring(50, 3).Trim()),
                TimingLoad                     = recordString.Substring(53, 4).Trim(),
                OperatingCharacteristicsString = recordString.Substring(60, 6),
                SeatingClass                   = (SeatingClass)_enumPropertyParsers["SeatingClass"].ParseProperty(recordString.Substring(66, 1)),
                Sleepers                       = (SleeperDetails)_enumPropertyParsers["SleeperDetails"].ParseProperty(recordString.Substring(67, 1)),
                Reservations                   = (ReservationDetails)_enumPropertyParsers["ReservationDetails"].ParseProperty(recordString.Substring(68, 1)),
                ConnectionIndicator            = recordString.Substring(69, 1).Trim(),
                CateringCode                   = (CateringCode)_enumPropertyParsers["CateringCode"].ParseProperty(recordString.Substring(70, 4).Trim()),
                ServiceBranding                = (ServiceBranding)_enumPropertyParsers["ServiceBranding"].ParseProperty(recordString.Substring(74, 4).Trim()),
                StpIndicator                   = (StpIndicator)_enumPropertyParsers["StpIndicator"].ParseProperty(recordString.Substring(79, 1))
            };

            int speed;
            var speedParsed = int.TryParse(recordString.Substring(57, 3).Trim(), NumberStyles.Any,
                                           new CultureInfo("en-gb"), out speed);

            if (speedParsed)
            {
                record.Speed = speed;
            }

            var dateRunsFromResult = _dateTimeParser.ParseDateTime(new DateTimeParserRequest
            {
                DateTimeFormat = "yyMMdd",
                DateTimeString = recordString.Substring(9, 6)
            });

            if (dateRunsFromResult.HasValue)
            {
                record.DateRunsFrom = dateRunsFromResult.Value;
            }
            else
            {
                throw new ArgumentException("Failed to parse Date Runs From in Basic Schedule Record");
            }

            record.UniqueId = record.TrainUid + recordString.Substring(9, 6) + record.StpIndicator;

            if (record.TrainCategory == "BR" || record.TrainCategory == "BS")
            {
                record.ServiceTypeFlags = record.ServiceTypeFlags | ServiceTypeFlags.Bus;
                record.ServiceTypeFlags = record.ServiceTypeFlags &= ~ServiceTypeFlags.Train;
            }
            else if (record.TrainStatus == "S" || record.TrainStatus == "4")
            {
                record.ServiceTypeFlags = record.ServiceTypeFlags | ServiceTypeFlags.Ship;
                record.ServiceTypeFlags = record.ServiceTypeFlags &= ~ServiceTypeFlags.Train;
            }

            if (record.ServiceTypeFlags.HasFlag(ServiceTypeFlags.Bus) || record.ServiceTypeFlags.HasFlag(ServiceTypeFlags.Ship) ||
                record.TrainCategory == "OL" || record.TrainCategory == "OO" || record.TrainCategory == "XC" || record.TrainCategory == "XX" || record.TrainCategory == "XZ")
            {
                record.ServiceTypeFlags = record.ServiceTypeFlags | ServiceTypeFlags.Passenger;
            }

            record.OperatingCharacteristics = (OperatingCharacteristics)_enumPropertyParsers["OperatingCharacteristics"].ParseProperty(record.OperatingCharacteristicsString);

            return(record);
        }