예제 #1
0
 private object MapDateTime(string value, Type valType, MappingStack mappingStack, MappingAction mappingAction,
                            out Type mappedType)
 {
     CheckExpectedType(valType, typeof(DateTime), mappingStack);
     mappedType = typeof(DateTime);
     return(OnStack("dateTime", mappingStack, (() =>
     {
         if (value == "" && MapEmptyDateTimeToMinValue)
         {
             return DateTime.MinValue;
         }
         DateTime local0;
         if (!DateTime8601.TryParseDateTime8601(value, out local0))
         {
             if (MapZerosDateTimeToMinValue && value.StartsWith("0000") &&
                 (value == "00000000T00:00:00" || value == "0000-00-00T00:00:00Z" ||
                  (value == "00000000T00:00:00Z" || value == "0000-00-00T00:00:00")))
             {
                 throw new XmlRpcInvalidXmlRpcException(mappingStack.MappingType +
                                                        " contains invalid dateTime value " +
                                                        StackDump(mappingStack));
             }
             local0 = DateTime.MinValue;
         }
         return local0;
     })));
 }
        /// <summary>
        /// Get publication date of post
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static DateTime GetPostDate(XElement obj)
        {
            // Try to get the gmt date of this post
            String value = GetMemberValue(obj, "date_created_gmt");

            if (String.IsNullOrWhiteSpace(value))
            {
                // Try to get the date of this post
                value = GetMemberValue(obj, "date_created");
                // Try to pars this date
                if (!String.IsNullOrWhiteSpace(value) &&
                    DateTime8601.TryParseDateTime8601(value, out DateTime result))
                {
                    return(result);
                }
            }
            else
            {
                // Try to pars this date
                if (DateTime8601.TryParseDateTime8601(value, out DateTime result))
                {
                    return(result);
                }
            }
            // Return the current utc time as default
            return(DateTime.UtcNow);
        }
        public static DateTime ToISO8601DateTime(this string date)
        {
            DateTime dateInst;

            DateTime8601.TryParseDateTime8601(date, out dateInst);

            return(dateInst);
        }
예제 #4
0
        public object ParseDateTime(XmlNode node, Type valueType, ParseStack parseStack)
        {
            if (valueType.IsNoDateTime())
            {
                throw new XmlRpcTypeMismatchException(parseStack.ParseType + " contains dateTime.iso8601 value where " + XmlRpcServiceInfo.GetXmlRpcTypeString(valueType) + " expected " + parseStack.Dump());
            }

            parseStack.Push("dateTime");
            try
            {
                var child = node.FirstChild;
                if (child == null)
                {
                    if (_config.MapEmptyDateTimeToMinValue())
                    {
                        return(DateTime.MinValue);
                    }
                    else
                    {
                        throw new XmlRpcInvalidXmlRpcException(parseStack.ParseType + " contains empty dateTime value " + parseStack.Dump());
                    }
                }

                var datestring = child.Value;

                if (!DateTime8601.TryParseDateTime8601(datestring, out var retVal))
                {
                    if (_config.MapZerosDateTimeToMinValue() &&
                        datestring.StartsWith("0000") &&
                        (datestring == "00000000T00:00:00" || datestring == "0000-00-00T00:00:00Z" || datestring == "00000000T00:00:00Z" || datestring == "0000-00-00T00:00:00"))
                    {
                        retVal = DateTime.MinValue;
                    }
                    else
                    {
                        throw new XmlRpcInvalidXmlRpcException(parseStack.ParseType + " contains invalid dateTime value " + parseStack.Dump());
                    }
                }

                return(valueType == typeof(XmlRpcDateTime) ? new XmlRpcDateTime(retVal) : (object)retVal);
            }
            finally
            {
                parseStack.Pop();
            }
        }