Exemplo n.º 1
0
        /// <summary>
        /// Combine a SDLDateTime (date only) with a TimeSpanWithZone to create
        /// a date-time
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="tswz"></param>
        /// <returns></returns>
        private static SDLDateTime Combine(SDLDateTime dt,
            TimeSpanWithZone tswz) {

            return new SDLDateTime(dt.Year, dt.Month, dt.Day, tswz.Hours,
                tswz.Minutes, tswz.Seconds, tswz.Milliseconds, tswz.TimeZone);
        }
Exemplo n.º 2
0
        // This special parse method is used only by the Token class for
        // tokens which are ambiguously either a TimeSpan or the time component
        // of a date/time type
        internal TimeSpanWithZone ParseTimeSpanWithZone(string text) {

            int day = 0; // optional (not allowed for date_time)
            int hour = 0; // mandatory
            int minute = 0; // mandatory
            int second = 0; // optional for date_time, mandatory for time span
            int millisecond = 0; // optional

            string timeZone = null;
            string dateText = text;

            int dashIndex = dateText.IndexOf('-', 1);
            if (dashIndex != -1) {
                timeZone = dateText.Substring(dashIndex + 1);
                dateText = text.Substring(0, dashIndex);
            }

            string[] segments = dateText.Split(':');

            // we know this is the time component of a date time type
            // because the time zone has been set
            if (timeZone != null) {
                if (segments.Length < 2 || segments.Length > 3)
                    parser.ParseException("date/time format exception.  Must " +
                            "use hh:mm(:ss)(.xxx)(-z)", line, position);
            } else {
                if (segments.Length < 2 || segments.Length > 4)
                    parser.ParseException("Time format exception.  For time " +
                            "spans use (d:)hh:mm:ss(.xxx) and for the " +
                            "time component of a date/time type use " +
                            "hh:mm(:ss)(.xxx)(-z)  If you use the day " +
                            "component of a time span make sure to " +
                            "prefix it with a lower case d", line,
                            position);
            }

            try {
                if (segments.Length == 4) {
                    string dayString = segments[0];
                    if (!dayString.EndsWith("d"))
                        parser.ParseException("The day component of a time " +
                            "span must end with a lower case d", line,
                            position);

                    day = Convert.ToInt32(dayString.Substring(0,
                            dayString.Length - 1));

                    hour = Convert.ToInt32(segments[1]);
                    minute = Convert.ToInt32(segments[2]);

                    if (segments.Length == 4) {
                        String last = segments[3];
                        int dotIndex = last.IndexOf('.');

                        if (dotIndex == -1) {
                            second = Convert.ToInt32(last);
                        } else {
                            second =
                                Convert.ToInt32(
                                        last.Substring(0, dotIndex));

                            String millis = last.Substring(dotIndex + 1);
                            if (millis.Length == 1)
                                millis = millis + "00";
                            else if (millis.Length == 2)
                                millis = millis + "0";

                            millisecond =
                                Convert.ToInt32(millis);
                        }
                    }

                    if (day < 0) {
                        hour = Parser.ReverseIfPositive(hour);
                        minute = Parser.ReverseIfPositive(minute);
                        second = Parser.ReverseIfPositive(second);
                        millisecond = Parser.ReverseIfPositive(millisecond);
                    }
                } else {
                    hour = Convert.ToInt32(segments[0]);
                    minute = Convert.ToInt32(segments[1]);

                    if (segments.Length == 3) {
                        String last = segments[2];
                        int dotIndex = last.IndexOf(".");

                        if (dotIndex == -1) {
                            second = Convert.ToInt32(last);
                        } else {
                            second = Convert.ToInt32(last.Substring(0, dotIndex));

                            String millis = last.Substring(dotIndex + 1);
                            if (millis.Length == 1)
                                millis = millis + "00";
                            else if (millis.Length == 2)
                                millis = millis + "0";

                            millisecond = Convert.ToInt32(millis);
                        }
                    }

                    if (hour < 0) {
                        minute = Parser.ReverseIfPositive(minute);
                        second = Parser.ReverseIfPositive(second);
                        millisecond = Parser.ReverseIfPositive(millisecond);
                    }
                }
            } catch (FormatException fe) {
                parser.ParseException("Time format: " + fe.Message, line,
                        position);
            }

            TimeSpanWithZone tswz = new TimeSpanWithZone(
                    day, hour, minute, second, millisecond, timeZone
            );

            return tswz;
        }