예제 #1
0
        public void NodeAsValuedDateTime3()
        {
            INode       orig   = _graph.CreateLiteralNode("2013-06-19T09:58:00-07:00", UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeDateTime));
            IValuedNode valued = orig.AsValuedNode();

            Assert.AreEqual(16, valued.AsDateTime().Hour);
            Assert.AreEqual(DateTimeKind.Utc, valued.AsDateTime().Kind);
        }
예제 #2
0
        /// <summary>
        /// Compares two Date Times for Date Time ordering
        /// </summary>
        /// <param name="x">Node</param>
        /// <param name="y">Node</param>
        /// <returns></returns>
        protected virtual int DateTimeCompare(IValuedNode x, IValuedNode y)
        {
            if (x == null || y == null)
            {
                throw new RdfQueryException("Cannot evaluate date time comparison when one or both arguments are Null");
            }
            try
            {
                DateTime c = x.AsDateTime();
                DateTime d = y.AsDateTime();

                switch (c.Kind)
                {
                case DateTimeKind.Unspecified:
                    if (d.Kind != DateTimeKind.Unspecified)
                    {
                        throw new RdfQueryException(
                                  "Dates are incomparable, one specifies time zone information while the other does not");
                    }
                    break;

                case DateTimeKind.Local:
                    if (d.Kind == DateTimeKind.Unspecified)
                    {
                        throw new RdfQueryException(
                                  "Dates are incomparable, one specifies time zone information while the other does not");
                    }
                    c = c.ToUniversalTime();
                    if (d.Kind == DateTimeKind.Local)
                    {
                        d = d.ToUniversalTime();
                    }
                    break;

                default:
                    if (d.Kind == DateTimeKind.Unspecified)
                    {
                        throw new RdfQueryException(
                                  "Dates are incomparable, one specifies time zone information while the other does not");
                    }
                    if (d.Kind == DateTimeKind.Local)
                    {
                        d = d.ToUniversalTime();
                    }
                    break;
                }

                // Compare on unspecified/UTC form as appropriate
                return(c.CompareTo(d));
            }
            catch (FormatException)
            {
                throw new RdfQueryException("Cannot evaluate date time comparison since one of the arguments does not have a valid lexical value for a Date");
            }
        }
예제 #3
0
        /// <summary>
        /// Compares two Date Times for Date Time ordering
        /// </summary>
        /// <param name="x">Node</param>
        /// <param name="y">Node</param>
        /// <returns></returns>
        protected override int DateTimeCompare(IValuedNode x, IValuedNode y)
        {
            if (x == null || y == null)
            {
                throw new RdfQueryException("Cannot evaluate date time comparison when one or both arguments are Null");
            }
            try
            {
                DateTime c = x.AsDateTime();
                DateTime d = y.AsDateTime();

                switch (c.Kind)
                {
                case DateTimeKind.Unspecified:
                    // Sort unspecified lower than Local/UTC date
                    if (d.Kind != DateTimeKind.Unspecified)
                    {
                        return(-1);
                    }
                    break;

                case DateTimeKind.Local:
                    // Sort Local higher than Unspecified
                    if (d.Kind == DateTimeKind.Unspecified)
                    {
                        return(1);
                    }
                    c = c.ToUniversalTime();
                    if (d.Kind == DateTimeKind.Local)
                    {
                        d = d.ToUniversalTime();
                    }
                    break;

                default:
                    // Sort UTC higher than Unspecified
                    if (d.Kind == DateTimeKind.Unspecified)
                    {
                        return(1);
                    }
                    if (d.Kind == DateTimeKind.Local)
                    {
                        d = d.ToUniversalTime();
                    }
                    break;
                }

                // Compare on unspecified/UTC form as appropriate
                return(c.CompareTo(d));
            }
            catch (FormatException)
            {
                throw new RdfQueryException("Cannot evaluate date time comparison since one of the arguments does not have a valid lexical value for a Date");
            }
        }
예제 #4
0
        private static object GetPrimitiveValue(IValuedNode valuedNode, IEdmPrimitiveType targetType, bool asNullable)
        {
            // TODO: Some sort of cast to nullable when necessary
            switch (targetType.PrimitiveKind)
            {
            case EdmPrimitiveTypeKind.Boolean:
                return(valuedNode.AsBoolean());

            case EdmPrimitiveTypeKind.Byte:
                return((byte)valuedNode.AsInteger());

            case EdmPrimitiveTypeKind.DateTime:
                return(valuedNode.AsDateTime());

            case EdmPrimitiveTypeKind.Decimal:
                return(valuedNode.AsDecimal());

            case EdmPrimitiveTypeKind.Double:
                return(valuedNode.AsDouble());

            case EdmPrimitiveTypeKind.Int16:
                return((Int16)valuedNode.AsInteger());

            case EdmPrimitiveTypeKind.Int32:
                return((Int32)valuedNode.AsInteger());

            case EdmPrimitiveTypeKind.Int64:
                return(valuedNode.AsInteger());

            case EdmPrimitiveTypeKind.String:
                return(valuedNode.AsString());

            case EdmPrimitiveTypeKind.DateTimeOffset:
                return(valuedNode.AsDateTime());

            default:
                throw new NotSupportedException(
                          String.Format("Support for primitive type {0} has not been implemented yet",
                                        targetType.PrimitiveKind));
            }
        }
예제 #5
0
        /// <summary>
        /// Compares two Dates for Date ordering
        /// </summary>
        /// <param name="x">Node</param>
        /// <param name="y">Node</param>
        /// <returns></returns>
        protected virtual int DateCompare(IValuedNode x, IValuedNode y)
        {
            if (x == null || y == null)
            {
                throw new RdfQueryException("Cannot evaluate date comparison when one or both arguments are Null");
            }
            try
            {
                DateTime c = x.AsDateTime();
                DateTime d = y.AsDateTime();

                switch (c.Kind)
                {
                case DateTimeKind.Unspecified:
                    break;

                case DateTimeKind.Local:
                    c = c.ToUniversalTime();
                    if (d.Kind == DateTimeKind.Local)
                    {
                        d = d.ToUniversalTime();
                    }
                    break;

                default:
                    if (d.Kind == DateTimeKind.Local)
                    {
                        d = d.ToUniversalTime();
                    }
                    break;
                }

                // Timezone irrelevant for date comparisons since we don't have any time to normalize to
                // Thus Open World Assumption means we can compare
                // For Local times we normalize to UTC

                // Compare on the Unspecified/UTC form as appropriate
                int res = c.Year.CompareTo(d.Year);
                if (res == 0)
                {
                    res = c.Month.CompareTo(d.Month);
                    if (res == 0)
                    {
                        res = c.Day.CompareTo(d.Day);
                    }
                }
                return(res);
            }
            catch (FormatException)
            {
                throw new RdfQueryException("Cannot evaluate date comparison since one of the arguments does not have a valid lexical value for a Date");
            }
        }
예제 #6
0
        /// <summary>
        /// Gets the numeric value of the function in the given Evaluation Context for the given Binding ID
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        /// <param name="bindingID">Binding ID</param>
        /// <returns></returns>
        public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode temp = this._expr.Evaluate(context, bindingID);

            if (temp != null)
            {
                return(this.ValueInternal(temp.AsDateTime()));
            }
            else
            {
                throw new RdfQueryException("Unable to evaluate an XPath Date Time function on a null argument");
            }
        }
예제 #7
0
        /// <summary>
        /// Calculates the value of the function in the given Evaluation Context for the given Binding ID
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        /// <param name="bindingID">Binding ID</param>
        /// <returns></returns>
        public virtual IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode temp = this._expr.Evaluate(context, bindingID);

            if (temp != null)
            {
                DateTimeOffset dt = temp.AsDateTime();
                //Regex based check to see if the value has a Timezone component
                //If not then the result is a null
                if (!Regex.IsMatch(temp.AsString(), "(Z|[+-]\\d{2}:\\d{2})$"))
                {
                    return(null);
                }

                //Now we have a DateTime we can try and return the Timezone
                if (dt.Offset.Equals(TimeSpan.Zero))
                {
                    //If Zero it was specified as Z (which means UTC so zero offset)
                    return(new StringNode(null, "PT0S", UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeDayTimeDuration)));
                }
                else
                {
                    //If the Offset is outside the range -14 to 14 this is considered invalid
                    if (dt.Offset.Hours < -14 || dt.Offset.Hours > 14)
                    {
                        return(null);
                    }

                    //Otherwise it has an offset which is a given number of hours and minutse
                    string offset = "PT" + Math.Abs(dt.Offset.Hours) + "H";
                    if (dt.Offset.Hours < 0)
                    {
                        offset = "-" + offset;
                    }
                    if (dt.Offset.Minutes != 0)
                    {
                        offset = offset + Math.Abs(dt.Offset.Minutes) + "M";
                    }
                    if (dt.Offset.Hours == 0 && dt.Offset.Minutes < 0)
                    {
                        offset = "-" + offset;
                    }

                    return(new StringNode(null, offset, UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeDayTimeDuration)));
                }
            }
            else
            {
                throw new RdfQueryException("Unable to evaluate an XPath Date Time function on a null argument");
            }
        }
예제 #8
0
        /// <summary>
        /// Compares two Date Times for Date Time ordering
        /// </summary>
        /// <param name="x">Node</param>
        /// <param name="y">Node</param>
        /// <returns></returns>
        protected int DateTimeCompare(IValuedNode x, IValuedNode y)
        {
            if (x == null || y == null)
            {
                throw new RdfQueryException("Cannot evaluate date time equality when one or both arguments are Null");
            }
            try
            {
                DateTimeOffset a = x.AsDateTime();
                DateTimeOffset b = y.AsDateTime();

                if (!a.Offset.Equals(b.Offset))
                {
                    throw new RdfQueryException("Cannot order Dates which are from different time zones");
                }

                return(a.CompareTo(b));
            }
            catch (FormatException)
            {
                throw new RdfQueryException("Cannot evaluate date time equality since one of the arguments does not have a valid lexical value for a Date Time");
            }
        }
예제 #9
0
        /// <summary>
        /// Gets the Timezone of the Argument Expression as evaluated for the given Binding in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        /// <param name="bindingID">Binding ID</param>
        /// <returns></returns>
        public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode temp = this._expr.Evaluate(context, bindingID);

            if (temp != null)
            {
                DateTimeOffset dt = temp.AsDateTime();
                //Regex based check to see if the value has a Timezone component
                //If not then the result is a null
                if (!Regex.IsMatch(temp.AsString(), "(Z|[+-]\\d{2}:\\d{2})$"))
                {
                    return(new StringNode(null, string.Empty));
                }

                //Now we have a DateTime we can try and return the Timezone
                if (dt.Offset.Equals(TimeSpan.Zero))
                {
                    //If Zero it was specified as Z (which means UTC so zero offset)
                    return(new StringNode(null, "Z"));
                }
                else
                {
                    //If the Offset is outside the range -14 to 14 this is considered invalid
                    if (dt.Offset.Hours < -14 || dt.Offset.Hours > 14)
                    {
                        return(null);
                    }

                    //Otherwise it has an offset which is a given number of hours (and minutes)
                    return(new StringNode(null, dt.Offset.Hours.ToString("00") + ":" + dt.Offset.Minutes.ToString("00")));
                }
            }
            else
            {
                throw new RdfQueryException("Unable to evaluate a Date Time function on a null argument");
            }
        }
예제 #10
0
        /// <summary>
        /// Casts the value of the inner Expression to a Date Time.
        /// </summary>
        /// <param name="context">Evaluation Context.</param>
        /// <param name="bindingID">Binding ID.</param>
        /// <returns></returns>
        public override IValuedNode Evaluate(SparqlEvaluationContext context, int bindingID)
        {
            IValuedNode n = _expr.Evaluate(context, bindingID);//.CoerceToDateTime();

            if (n == null)
            {
                throw new RdfQueryException("Cannot cast a Null to a xsd:dateTime");
            }

            // New method should be much faster
            // if (n is DateTimeNode) return n;
            // if (n is DateNode) return new DateTimeNode(n.Graph, n.AsDateTime());
            // return new DateTimeNode(null, n.AsDateTime());

            switch (n.NodeType)
            {
            case NodeType.Blank:
            case NodeType.GraphLiteral:
            case NodeType.Uri:
                throw new RdfQueryException("Cannot cast a Blank/URI/Graph Literal Node to a xsd:dateTime");

            case NodeType.Literal:
                if (n is DateTimeNode)
                {
                    return(n);
                }
                if (n is DateNode)
                {
                    return(new DateTimeNode(n.Graph, n.AsDateTime()));
                }
                // See if the value can be cast
                ILiteralNode lit = (ILiteralNode)n;
                if (lit.DataType != null)
                {
                    string dt = lit.DataType.ToString();
                    if (dt.Equals(XmlSpecsHelper.XmlSchemaDataTypeDateTime))
                    {
                        // Already a xsd:dateTime
                        DateTimeOffset d;
                        if (DateTimeOffset.TryParse(lit.Value, out d))
                        {
                            // Parsed OK
                            return(new DateTimeNode(lit.Graph, d));
                        }
                        else
                        {
                            throw new RdfQueryException("Invalid lexical form for xsd:dateTime");
                        }
                    }
                    else if (dt.Equals(XmlSpecsHelper.XmlSchemaDataTypeString))
                    {
                        DateTimeOffset d;
                        if (DateTimeOffset.TryParse(lit.Value, out d))
                        {
                            // Parsed OK
                            return(new DateTimeNode(lit.Graph, d));
                        }
                        else
                        {
                            throw new RdfQueryException("Cannot cast the value '" + lit.Value + "' to a xsd:double");
                        }
                    }
                    else
                    {
                        throw new RdfQueryException("Cannot cast a Literal typed <" + dt + "> to a xsd:dateTime");
                    }
                }
                else
                {
                    DateTimeOffset d;
                    if (DateTimeOffset.TryParse(lit.Value, out d))
                    {
                        // Parsed OK
                        return(new DateTimeNode(lit.Graph, d));
                    }
                    else
                    {
                        throw new RdfQueryException("Cannot cast the value '" + lit.Value + "' to a xsd:dateTime");
                    }
                }

            default:
                throw new RdfQueryException("Cannot cast an Unknown Node to a xsd:string");
            }
        }
예제 #11
0
 private static object GetPrimitiveValue(IValuedNode valuedNode, IEdmPrimitiveType targetType, bool asNullable)
 {
     // TODO: Some sort of cast to nullable when necessary
         switch (targetType.PrimitiveKind)
         {
             case EdmPrimitiveTypeKind.Boolean:
                 return valuedNode.AsBoolean();
             case EdmPrimitiveTypeKind.Byte:
                 return (byte) valuedNode.AsInteger();
             case EdmPrimitiveTypeKind.DateTime:
                 return valuedNode.AsDateTime();
             case EdmPrimitiveTypeKind.Decimal:
                 return valuedNode.AsDecimal();
             case EdmPrimitiveTypeKind.Double:
                 return valuedNode.AsDouble();
             case EdmPrimitiveTypeKind.Int16:
                 return (Int16) valuedNode.AsInteger();
             case EdmPrimitiveTypeKind.Int32:
                 return (Int32) valuedNode.AsInteger();
             case EdmPrimitiveTypeKind.Int64:
                 return valuedNode.AsInteger();
             case EdmPrimitiveTypeKind.String:
                 return valuedNode.AsString();
             case EdmPrimitiveTypeKind.DateTimeOffset:
                 return valuedNode.AsDateTime();
             default:
                 throw new NotSupportedException(
                     String.Format("Support for primitive type {0} has not been implemented yet",
                                   targetType.PrimitiveKind));
         }
 }