Example #1
0
        public static object ConstructYamlTimestamp(IConstructor ctor, Node node) {
            ScalarNode scalar = node as ScalarNode;
            if (scalar == null) {
                throw new ConstructorException("can only contruct timestamp from scalar node");
            }
            
            Match match = TIMESTAMP_REGEXP.Match(scalar.Value);

            if (!match.Success) {
                return ctor.ConstructPrivateType(node);
            }

            string year_s = match.Groups[1].Value;
            string month_s = match.Groups[2].Value;
            string day_s = match.Groups[3].Value;
            string hour_s = match.Groups[4].Value;
            string min_s = match.Groups[5].Value;
            string sec_s = match.Groups[6].Value;
            string fract_s = match.Groups[7].Value;
            string utc = match.Groups[8].Value;
            string timezoneh_s = match.Groups[9].Value;
            string timezonem_s = match.Groups[10].Value;

            bool isUtc = utc == "Z" || utc == "z";

            DateTime dt = new DateTime(
                year_s != "" ? int.Parse(year_s) : 0,
                month_s != "" ? int.Parse(month_s) : 1,
                day_s != "" ? int.Parse(day_s) : 1,
                hour_s != "" ? int.Parse(hour_s) : 0,
                min_s != "" ? int.Parse(min_s) : 0,
                sec_s != "" ? int.Parse(sec_s) : 0,
                isUtc? DateTimeKind.Utc : DateTimeKind.Local
            );

            if (!string.IsNullOrEmpty(fract_s)) {
                long fract = int.Parse(fract_s);
                if (fract > 0) {
                    while (fract < 1000000) {
                        fract *= 10;
                    }
                    dt = dt.AddTicks(fract);
                }
            }

            if (!isUtc) {
                if (timezoneh_s != "" || timezonem_s != "") {
                    int zone = 0;
                    int sign = +1;
                    if (timezoneh_s != "") {
                        if (timezoneh_s.StartsWith("-")) {
                            sign = -1;
                        }
                        zone += int.Parse(timezoneh_s.Substring(1)) * 3600000;
                    }
                    if (timezonem_s != "") {
                        zone += int.Parse(timezonem_s) * 60000;
                    }
                    double utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(dt).TotalMilliseconds;
                    dt = dt.AddMilliseconds(utcOffset - sign * zone);
                }
            }
            return dt;
        }
Example #2
0
        public static object ConstructYamlTimestamp(IConstructor ctor, Node node)
        {
            ScalarNode scalar = node as ScalarNode;

            if (scalar == null)
            {
                throw new ConstructorException("can only contruct timestamp from scalar node");
            }

            Match match = TIMESTAMP_REGEXP.Match(scalar.Value);

            if (!match.Success)
            {
                return(ctor.ConstructPrivateType(node));
            }

            string year_s      = match.Groups[1].Value;
            string month_s     = match.Groups[2].Value;
            string day_s       = match.Groups[3].Value;
            string hour_s      = match.Groups[4].Value;
            string min_s       = match.Groups[5].Value;
            string sec_s       = match.Groups[6].Value;
            string fract_s     = match.Groups[7].Value;
            string utc         = match.Groups[8].Value;
            string timezoneh_s = match.Groups[9].Value;
            string timezonem_s = match.Groups[10].Value;

            bool isUtc = utc == "Z" || utc == "z";

            DateTime dt = new DateTime(
                year_s != "" ? int.Parse(year_s) : 0,
                month_s != "" ? int.Parse(month_s) : 1,
                day_s != "" ? int.Parse(day_s) : 1,
                hour_s != "" ? int.Parse(hour_s) : 0,
                min_s != "" ? int.Parse(min_s) : 0,
                sec_s != "" ? int.Parse(sec_s) : 0,
                isUtc? DateTimeKind.Utc : DateTimeKind.Local
                );

            if (!string.IsNullOrEmpty(fract_s))
            {
                long fract = int.Parse(fract_s);
                if (fract > 0)
                {
                    while (fract < 1000000)
                    {
                        fract *= 10;
                    }
                    dt = dt.AddTicks(fract);
                }
            }

            if (!isUtc)
            {
                if (timezoneh_s != "" || timezonem_s != "")
                {
                    int zone = 0;
                    int sign = +1;
                    if (timezoneh_s != "")
                    {
                        if (timezoneh_s.StartsWith("-"))
                        {
                            sign = -1;
                        }
                        zone += int.Parse(timezoneh_s.Substring(1)) * 3600000;
                    }
                    if (timezonem_s != "")
                    {
                        zone += int.Parse(timezonem_s) * 60000;
                    }
                    double utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(dt).TotalMilliseconds;
                    dt = dt.AddMilliseconds(utcOffset - sign * zone);
                }
            }
            return(dt);
        }