예제 #1
0
파일: DateTime.cs 프로젝트: formist/LinkMe
        /// <summary>
        /// Adds nanoseconds to this instance of DateTime
        /// </summary>
        /// <param name="nanoseconds"></param>
        /// <returns></returns>
        public DateTime AddNanoseconds(double nanoseconds)
        {
            DateTime universal = ToUniversalTime();
            DateTime result    = universal.AddNanosecondsInternal(System.Math.Round(nanoseconds));

            return(result.ToTimeZone(TimeZone));
        }
예제 #2
0
파일: DateTime.cs 프로젝트: formist/LinkMe
        /// <summary>
        /// Adds a timespan to this instance of DateTime
        /// </summary>
        /// <param name="timeSpan"></param>
        /// <returns></returns>
        public DateTime Add(TimeSpan timeSpan)
        {
            DateTime result = new DateTime(Universal.Add(timeSpan.ToSystemTimeSpan()), m_nanosecond,
                                           Type.TimeZone.UTC, true);

            result = result.AddMicrosecondsInternal(timeSpan.Microseconds);
            result = result.AddNanosecondsInternal(timeSpan.Nanoseconds);

            return(result.ToTimeZone(TimeZone));
        }
예제 #3
0
파일: DateTime.cs 프로젝트: formist/LinkMe
        // All Add* methods that take a double as input split the value into its whole
        // and fractional parts. The whole part can be added using the corresponding
        // System.DateTime.Add* method, but the fractional part needs to be added using
        // our own method so that microseconds and nanoseconds are updated.

        /// <summary>
        /// Adds days to this instance of DateTime
        /// </summary>
        /// <param name="days"></param>
        /// <returns></returns>
        public DateTime AddDays(double days)
        {
            DateTime universal = ToUniversalTime();

            double whole      = System.Math.Floor(days);
            double fractional = days - whole;

            DateTime result = universal.AddNanosecondsInternal
                                  (fractional * DateTimeHelper.Days2Nanoseconds);

            result = new DateTime(result.m_base.AddDays(whole), result.m_nanosecond, Type.TimeZone.UTC, true);

            return(result.ToTimeZone(TimeZone));
        }
예제 #4
0
파일: DateTime.cs 프로젝트: formist/LinkMe
        /// <summary>
        /// Adds microseconds to this instance of DateTime
        /// </summary>
        /// <param name="microseconds"></param>
        /// <returns></returns>
        public DateTime AddMicroseconds(double microseconds)
        {
            DateTime universal = ToUniversalTime();

            double whole      = System.Math.Floor(microseconds);
            double fractional = microseconds - whole;

            DateTime result = universal.AddNanosecondsInternal
                                  (fractional * DateTimeHelper.Microseconds2Nanoseconds);

            result = result.AddMicrosecondsInternal(whole);

            return(result.ToTimeZone(TimeZone));
        }
예제 #5
0
파일: DateTime.cs 프로젝트: formist/LinkMe
        public DateTime AddMonths(int months)
        {
            DateTime result = new DateTime(Universal.AddMonths(months), m_nanosecond, Type.TimeZone.UTC, true);

            return(result.ToTimeZone(TimeZone));
        }
예제 #6
0
파일: DateTime.cs 프로젝트: formist/LinkMe
        /// <summary>
        /// Converts the XML representation of a date, time and time zone to a DateTime
        /// object. The input string can be a valid XSD dateTime
        /// (http://www.w3.org/TR/xmlschema-2/#dateTime) OR an XSD dateTime in UTC
        /// (with time zone designator 'Z'), followed by a space and a time zone name or
        /// abbreviation.
        /// </summary>
        /// <remarks>
        /// Valid input examples:
        ///
        /// "2004-03-24T17:04:00.56Z" (valid XSD dateTime). The resulting DateTime will have
        /// TimeZone set to UTC.
        ///
        /// "2004-03-24T17:04:00-06:00" (the same DateTime in a UTC-06:00 time zone).
        /// The resulting DateTime will have TimeZone set to a time zone 6 hours behind
        /// UTC with no daylight savings.
        ///
        /// "2004-03-24T06:04:00Z AEST" (the same DateTime in AEST). The resulting DateTime
        /// will have TimeZone set to AUS Eastern Standard Time.
        ///
        /// "2004-03-24T06:04:00Z AUS Eastern Standard Time" (same as above with the full
        /// time zone name).
        ///
        /// "2004-03-24T06:04:00Z +11:00" (the same DateTime in a UTC+11:00 time zone).
        /// </remarks>
        internal static DateTime FromXml(string value)
        {
            const string method = "FromXml";

            // Check that there is something there and of the right length.

            if (value == null)
            {
                throw new NullParameterException(typeof(DateTime), method, "value");
            }

            if (value.Length < DateTimeHelper.DateTimeXmlFormatMinLength)
            {
                throw new InvalidParameterFormatException(typeof(DateTime), method, "value", value, DateTimeHelper.DateTimeXmlFormat);
            }

            // Check for separators in the right places.

            if (value[4] != '-' || value[7] != '-' || value[10] != 'T' || value[13] != ':' ||
                value[16] != ':')
            {
                throw new InvalidParameterFormatException(typeof(DateTime), method, "value", value, DateTimeHelper.DateTimeXmlFormat);
            }

            // Extract each part, except for fractional seconds.

            int year        = ParseInt(value, 0, 4, true);
            int month       = ParseInt(value, 5, 2, true);
            int day         = ParseInt(value, 8, 2, true);
            int hour        = ParseInt(value, 11, 2, true);
            int minute      = ParseInt(value, 14, 2, true);
            int second      = ParseInt(value, 17, 2, true);
            int millisecond = 0;
            int microsecond = 0;
            int nanosecond  = 0;

            int offset;

            // Fractional seconds

            if (value[19] == '.')
            {
                int index = 20;

                while (index < value.Length && System.Char.IsDigit(value, index))
                {
                    index++;
                }

                if (index == 20)
                {
                    throw new InvalidParameterFormatException(typeof(DateTime), method, "value", value, DateTimeHelper.DateTimeXmlFormat);
                }

                string fraction = value.Substring(20, index - 20);

                if (fraction.Length < 9)
                {
                    fraction = fraction.PadRight(9, '0');
                }

                millisecond = System.Int32.Parse(fraction.Substring(0, 3));
                microsecond = System.Int32.Parse(fraction.Substring(3, 3));
                nanosecond  = System.Int32.Parse(fraction.Substring(6, 3));

                // XSD dateTime type allows arbitrary precision, so extra decimal
                // places are valid. Just ignore them.

                offset = index;
            }
            else
            {
                offset = 19;
            }

            // Time zone

            // A time zone must be specified
            if (value.Length == offset)
            {
                throw new InvalidParameterFormatException(typeof(DateTime), method, "value", value, DateTimeHelper.DateTimeXmlFormat);
            }

            if (value[offset] == 'Z')
            {
                DateTime dateTime = new DateTime(year, month, day, hour, minute, second,
                                                 millisecond, microsecond, nanosecond, Type.TimeZone.UTC);

                if (value.Length == offset + 1)
                {
                    return(dateTime);                    // Leave the time zone as UTC
                }
                else
                {
                    // We should have a space followed by a time zone name (or abbreviation)
                    if (value[offset + 1] != ' ')
                    {
                        throw new InvalidParameterFormatException(typeof(DateTime), method, "value", value, DateTimeHelper.DateTimeXmlFormat);
                    }

                    // Convert to the specified time zone

                    string timeZoneString = value.Substring(offset + 2);
                    if (timeZoneString.Length == 0)
                    {
                        throw new InvalidParameterFormatException(typeof(DateTime), method, "value", value, DateTimeHelper.DateTimeXmlFormat);
                    }

                    Type.TimeZone timeZone = Type.TimeZone.Parse(timeZoneString);

                    return(dateTime.ToTimeZone(timeZone));
                }
            }
            else
            {
                // Parse the rest of the string as a time zone - it must begin with
                // '+' or '-', a time zone name is not acceptable here.

                if (value[offset] != '+' && value[offset] != '-')
                {
                    throw new InvalidParameterFormatException(typeof(DateTime), method, "value", value, DateTimeHelper.DateTimeXmlFormat);
                }

                Type.TimeZone timeZone = Type.TimeZone.Parse(value.Substring(offset));

                return(new DateTime(year, month, day, hour, minute, second,
                                    millisecond, microsecond, nanosecond, timeZone));
            }
        }