Ejemplo n.º 1
0
        public void SetMinutesWorks() {
			var dt = new JsDate(2000, 0, 1);
			dt.SetMinutes(34);
			Assert.AreEqual(dt.GetMinutes(), 34);
        }
Ejemplo n.º 2
-1
            private object GetObjectValue(IXmlSchemaElementDefinition elementDefinition, XmlNode node)
            {
                XmlNodeList nilNodes = XPath.Evaluate(node, "./@i:nil", prefix => prefix == "i" ? "http://www.w3.org/2001/XMLSchema-instance" : null);
                if (nilNodes.Count == 1 && (nilNodes[0].Value == "true" || nilNodes[0].Value == "1"))
                {
                    return null;
                }

                IXmlSchemaSimpleTypeDefinition simpleTypeDefinition = elementDefinition.Type as IXmlSchemaSimpleTypeDefinition;
                if (simpleTypeDefinition != null)
                {
                    if (node.ChildNodes.Count != 1 || node.ChildNodes[0].NodeType != XmlNodeType.Text)
                    {
                        throw new InvalidOperationException("Expected text node as child for element " + elementDefinition.Name + ".");
                    }
                    string text = node.ChildNodes[0].Value;
                    switch (simpleTypeDefinition.Type)
                    {
                        case XmlBuiltInSimpleType.AnyType:
                        case XmlBuiltInSimpleType.AnyUri:
                        case XmlBuiltInSimpleType.Guid:
                        case XmlBuiltInSimpleType.QName:
                        case XmlBuiltInSimpleType.String:
                        case XmlBuiltInSimpleType.Enum:
                            return DecodeString(text);
                        case XmlBuiltInSimpleType.Base64Binary:
                            return StringUtility.FromBase64ToByteArray(text);
                        case XmlBuiltInSimpleType.Boolean:
                            return text == "true" || text == "1";
                        case XmlBuiltInSimpleType.Byte:
                            return FrameworkUtility.ByteTryParse(text);
                        case XmlBuiltInSimpleType.Char:
                            return (int)DecodeString(text)[0];
                        case XmlBuiltInSimpleType.Decimal:
                            return FrameworkUtility.DecimalTryParse(text);
                        case XmlBuiltInSimpleType.Double:
                            return FrameworkUtility.DoubleTryParse(text);
                        case XmlBuiltInSimpleType.Float:
                            return FrameworkUtility.FloatTryParse(text);
                        case XmlBuiltInSimpleType.Int:
                            return FrameworkUtility.IntTryParse(text);
                        case XmlBuiltInSimpleType.Long:
                            return FrameworkUtility.LongTryParse(text);
                        case XmlBuiltInSimpleType.Short:
                            return FrameworkUtility.ShortTryParse(text);
                        case XmlBuiltInSimpleType.UnsignedByte:
                            return FrameworkUtility.ByteTryParse(text);
                        case XmlBuiltInSimpleType.UnsignedInt:
                            return FrameworkUtility.IntTryParse(text);
                        case XmlBuiltInSimpleType.UnsignedLong:
                            return FrameworkUtility.LongTryParse(text);
                        case XmlBuiltInSimpleType.UnsignedShort:
                            return FrameworkUtility.ShortTryParse(text);
                        case XmlBuiltInSimpleType.DateTime:
                            string[] bits = text.Split(new Regex("[-T:+]", "g"));
                            int timeZoneStart;
                            int milliseconds;
                            if (bits.Length == 9)
                            {
                                timeZoneStart = 7;
                                milliseconds = FrameworkUtility.IntTryParse(bits[6]) ?? 0;
                            }
                            else
                            {
                                timeZoneStart = 6;
                                milliseconds = 0;
                            }
                            JsDate dateTime = new JsDate(int.Parse(bits[0]), int.Parse(bits[1]) - 1, int.Parse(bits[2]), int.Parse(bits[3]), int.Parse(bits[4]), int.Parse(bits[5]), milliseconds);
                            int offsetMinutes = int.Parse(bits[timeZoneStart])*60 + int.Parse(bits[timeZoneStart + 1]);
                            bool offsetIsNegative = new Regex("-\\d\\d:\\d\\d$").Test(text);
                            if (offsetIsNegative)
                            {
                                offsetMinutes *= -1;
                            }
                            dateTime.SetMinutes(dateTime.GetMinutes() - offsetMinutes - dateTime.GetTimezoneOffset());
                            return (DateTime)dateTime;
                        case XmlBuiltInSimpleType.Duration:
                            Regex regex = new Regex("([-+]?)P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?");
                            RegexMatch match = regex.Exec(text);
                            bool durationIsNegative = match[1] == "-";
                            int durationYears = match[3] == null ? 0 : FrameworkUtility.IntTryParse(match[3]) ?? 0;
                            int durationMonths = match[5] == null ? 0 : FrameworkUtility.IntTryParse(match[5]) ?? 0;
                            if (durationYears != 0 || durationMonths != 0)
                            {
                                throw new InvalidOperationException("Durations may not contain years or months.");
                            }
                            int durationDays = match[7] == null ? 0 : FrameworkUtility.IntTryParse(match[7]) ?? 0;
                            int durationHours = match[10] == null ? 0 : FrameworkUtility.IntTryParse(match[10]) ?? 0;
                            int durationMinutes = match[12] == null ? 0 : FrameworkUtility.IntTryParse(match[12]) ?? 0;
                            double durationFractionalSeconds = match[14] == null ? 0 : FrameworkUtility.DoubleTryParse(match[14]) ?? 0;
                            int durationSeconds = Math.Truncate(durationFractionalSeconds);
                            int durationMilliseconds = Math.Truncate(durationFractionalSeconds*1000)%1000;
                            if (durationIsNegative)
                            {
                                durationDays *= -1;
                                durationHours *= -1;
                                durationMinutes *= -1;
                                durationSeconds *= -1;
                                durationMilliseconds *= -1;
                            }
                            return new TimeSpan(durationDays, durationHours, durationMinutes, durationSeconds, durationMilliseconds);
                        default:
                            throw UnhandledEnumValueExceptionFactory.Create(simpleTypeDefinition.Type);
                    }
                }

                IXmlSchemaComplexTypeDefinition complexTypeDefinition = elementDefinition.Type as IXmlSchemaComplexTypeDefinition;
                if (complexTypeDefinition == null)
                {
                    throw new InvalidOperationException("Unknown element type.");
                }
                JsDictionary d = new JsDictionary();
                foreach (IXmlSchemaElementDefinition childElementDefinition in complexTypeDefinition.Elements)
                {
                    d[childElementDefinition.Name] = GetObjectRecursive(childElementDefinition, node);
                }
                return d;
            }