Example #1
0
        public static object ConstructYamlTimestamp(BaseConstructor ctor, Node node)
        {
            ScalarNode scalar = node as ScalarNode;

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

            Match match = TimestampRegex.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 != "" ? Int32.Parse(year_s, CultureInfo.InvariantCulture) : 0,
                month_s != "" ? Int32.Parse(month_s, CultureInfo.InvariantCulture) : 1,
                day_s != "" ? Int32.Parse(day_s, CultureInfo.InvariantCulture) : 1,
                hour_s != "" ? Int32.Parse(hour_s, CultureInfo.InvariantCulture) : 0,
                min_s != "" ? Int32.Parse(min_s, CultureInfo.InvariantCulture) : 0,
                sec_s != "" ? Int32.Parse(sec_s, CultureInfo.InvariantCulture) : 0,
                DateTimeKind.Utc
                );

            if (!String.IsNullOrEmpty(fract_s))
            {
                long fract = Int32.Parse(fract_s, CultureInfo.InvariantCulture);
                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("-", StringComparison.Ordinal))
                        {
                            sign = -1;
                        }
                        zone += Int32.Parse(timezoneh_s.Substring(1), CultureInfo.InvariantCulture) * 3600000;
                    }
                    if (timezonem_s != "")
                    {
                        zone += Int32.Parse(timezonem_s, CultureInfo.InvariantCulture) * 60000;
                    }
                    dt = dt.AddMilliseconds(-sign * zone);
                }
                dt = RubyTime.ToLocalTime(dt);
            }
            return(new RubyTime(dt));
        }
Example #2
0
        public static Node ToYaml(RubyTime/*!*/ self, [NotNull]RubyRepresenter/*!*/ rep)
        {
            TimeSpan offset = self.GetCurrentZoneOffset();
            long fractional = self.Microseconds;
            string value = String.Format(CultureInfo.InvariantCulture,
                "{0:yyyy-MM-dd HH:mm:ss}" + (fractional == 0 ? "" : ".{1:D6}") + (self.Kind == DateTimeKind.Utc ? " Z" : " {2}{3:D2}:{4:D2}"),
                self.DateTime,
                fractional,
                offset.Hours >= 0 ? "+" : "",
                offset.Hours,
                offset.Minutes
            );

            return rep.Scalar(rep.GetTagUri(self), value, ScalarQuotingStyle.None);
        }
Example #3
0
 public static MutableString TagUri(RubyContext/*!*/ context, RubyTime/*!*/ self)
 {
     return RubyYaml.GetTagUri(context, self, Tags.Timestamp, typeof(RubyTime));
 }
Example #4
0
 public static MutableString/*!*/ TagUri(RubyTime self) {
     return MutableString.CreateAscii("tag:yaml.org,2002:timestamp");
 }
Example #5
0
 public static Node/*!*/ ToYaml(RubyTime self, [NotNull]RubyRepresenter/*!*/ rep) {
     TimeSpan offset = self.GetCurrentZoneOffset();
     long fractional = self.Microseconds;
     return rep.Scalar(self, MutableString.CreateAscii(String.Format(CultureInfo.InvariantCulture,
         "{0:yyyy-MM-dd HH:mm:ss}" + (fractional == 0 ? "" : ".{1:D6}") + (self.Kind == DateTimeKind.Utc ? " Z" : " {2}{3:D2}:{4:D2}"),
         self.DateTime,
         fractional,
         offset.Hours >= 0 ? "+" : "",
         offset.Hours, 
         offset.Minutes
     )));
 }
Example #6
0
 public static MutableString /*!*/ TagUri(RubyContext /*!*/ context, RubyTime /*!*/ self)
 {
     return(RubyYaml.GetTagUri(context, self, Tags.Timestamp, typeof(RubyTime)));
 }