TryParseDateTime() public static method

public static TryParseDateTime ( string dateTimeString, System.DateTime &dateTime ) : bool
dateTimeString string
dateTime System.DateTime
return bool
Exemplo n.º 1
0
Arquivo: Header.cs Projeto: xj0229/gsf
        // Static Methods

        public static Header Parse(string[] lines, ref int index)
        {
            const string HeaderLine1 = @"(\S.*)\s+Date:\s+(\S+)\s+Time:\s+(\S+)";
            const string HeaderLine2 = @"(\S.*)(?:\s+Serial Number: (\d+))?";

            Header header = new Header();

            Match    regexMatch;
            DateTime eventTime;
            string   eventTimeString;
            int      serialNumber;

            if (index < lines.Length)
            {
                // Apply regex match to get information contained on first line of header
                regexMatch = Regex.Match(lines[index], HeaderLine1);

                if (regexMatch.Success)
                {
                    // Get relay ID from line 1
                    header.RelayID = regexMatch.Groups[1].Value.Trim();

                    // Build date/time string for parsing
                    eventTimeString = string.Format("{0} {1}", regexMatch.Groups[2].Value, regexMatch.Groups[3].Value);

                    // Get event time from line 1
                    if (EventFile.TryParseDateTime(eventTimeString, out eventTime))
                    {
                        header.EventTime = eventTime;
                    }

                    // Advance to the next line
                    index++;

                    if (index < lines.Length)
                    {
                        // Apply regex match to get information contained on second line of header
                        regexMatch = Regex.Match(lines[index], HeaderLine2);

                        if (regexMatch.Success)
                        {
                            // Get station ID and serial number from line 2
                            header.StationID = regexMatch.Groups[1].Value.Trim();

                            if (int.TryParse(regexMatch.Groups[2].Value, out serialNumber))
                            {
                                header.SerialNumber = serialNumber;
                            }

                            // Advance to the next line
                            index++;
                        }
                    }
                }
            }

            return(header);
        }
Exemplo n.º 2
0
        // Static Methods

        public static List <EventHistoryRecord> ParseRecords(string[] lines, ref int index)
        {
            List <EventHistoryRecord> histories = new List <EventHistoryRecord>();
            EventHistoryRecord        eventHistory;
            string currentLine;

            List <Token> tokens;
            List <Token> headers;
            Dictionary <Token, Token> fields;
            Token fieldHeader;
            Token field;

            int      eventNumber;
            DateTime dateTime;
            double   faultLocation;
            double   current;
            double   frequency;
            int      group;
            int      shot;

            string date;
            string time;

            // Parse header
            headers = Split(lines[index++]);

            // Skip to the next nonblank line
            EventFile.SkipBlanks(lines, ref index);

            while (index < lines.Length)
            {
                currentLine = lines[index];

                // Empty line indicates end of event histories
                if (string.IsNullOrWhiteSpace(currentLine))
                {
                    break;
                }

                // Create a new event history record
                eventHistory = new EventHistoryRecord();

                // Parse fields
                tokens = Split(currentLine);

                // Initialize date and time variables
                date = null;
                time = null;

                fields = new Dictionary <Token, Token>();

                foreach (Token token in tokens)
                {
                    fieldHeader = headers.MinBy(header => token.Distance(header));
                    fields.AddOrUpdate(fieldHeader, token, (key, value) => value.JoinWith(token));
                }

                foreach (Token header in headers)
                {
                    if (fields.TryGetValue(header, out field))
                    {
                        switch (header.Text.ToUpper())
                        {
                        case "#":
                        case "REC_NUM":
                            // Parse the field as an event number
                            if (int.TryParse(field.Text, out eventNumber))
                            {
                                eventHistory.EventNumber = eventNumber;
                            }

                            break;

                        case "DATE":
                            // Parse the field as a date value
                            date = field.Text;

                            // If both date and time have been provided, parse them as a DateTime
                            if ((object)time != null && EventFile.TryParseDateTime(string.Format("{0} {1}", date, time), out dateTime))
                            {
                                eventHistory.Time = dateTime;
                            }

                            break;

                        case "TIME":
                            // Parse the field as a time value
                            time = field.Text;

                            // If both date and time have been provided, parse them as a DateTime
                            if ((object)date != null && EventFile.TryParseDateTime(string.Format("{0} {1}", date, time), out dateTime))
                            {
                                eventHistory.Time = dateTime;
                            }

                            break;

                        case "EVENT":
                            // Parse the field as an event type
                            eventHistory.EventType = field.Text;
                            break;

                        case "LOCAT":
                        case "LOCATION":
                            // Parse the field as a fault location value
                            if (double.TryParse(field.Text, out faultLocation))
                            {
                                eventHistory.FaultLocation = faultLocation;
                            }

                            break;

                        case "CURR":
                            // Parse the field as a current magnitude
                            if (double.TryParse(field.Text, out current))
                            {
                                eventHistory.Current = current;
                            }

                            break;

                        case "FREQ":
                            // Parse the field as a frequency value
                            if (double.TryParse(field.Text, out frequency))
                            {
                                eventHistory.Frequency = frequency;
                            }

                            break;

                        case "GRP":
                        case "GROUP":
                            // Parse the field as a group number
                            if (int.TryParse(field.Text, out group))
                            {
                                eventHistory.Group = group;
                            }

                            break;

                        case "SHOT":
                            // Parse the field as a shot number
                            if (int.TryParse(field.Text, out shot))
                            {
                                eventHistory.Shot = shot;
                            }

                            break;

                        case "TARGETS":
                            // Parse the field as targets
                            eventHistory.Targets = field.Text;
                            break;
                        }
                    }
                }

                // Add history record to the list of histories
                histories.Add(eventHistory);

                // Advance to the next line
                index++;
            }

            return(histories);
        }