Ejemplo n.º 1
0
 public void TestAbs()
 {
     TestCommon.CompareTestEqual(
         ToCN(2),
         ToCN(-2).Abs());
     TestCommon.CompareTestEqual(
         ToCN(2),
         ToCN(2).Abs());
     TestCommon.CompareTestEqual(
         ToCN(2.5),
         ToCN(-2.5).Abs());
     {
         CBORNumber objectTemp  = ToCN(EDecimal.FromString("6.63"));
         CBORNumber objectTemp2 = ToCN(EDecimal.FromString(
                                           "-6.63")).Abs();
         TestCommon.CompareTestEqual(objectTemp, objectTemp2);
     }
     {
         CBORNumber objectTemp  = ToCN(EFloat.FromString("2.75"));
         CBORNumber objectTemp2 = ToCN(EFloat.FromString("-2.75")).Abs();
         TestCommon.CompareTestEqual(objectTemp, objectTemp2);
     }
     {
         CBORNumber objectTemp  = ToCN(ERational.FromDouble(2.5));
         CBORNumber objectTemp2 = ToCN(ERational.FromDouble(-2.5)).Abs();
         TestCommon.CompareTestEqual(objectTemp, objectTemp2);
     }
 }
        public DateTime FromCBORObject(CBORObject obj)
        {
            CBORObject untaggedObject = obj;

            if (obj.HasMostOuterTag(0))
            {
                throw new CBORException("Only numeric representation of date is permitted");
            }
            else if (obj.HasMostOuterTag(1))
            {
                Console.WriteLine("Section 2 of RFC8392 states that the leading 1 tag MUST be omitted, but it is present here");
                untaggedObject = obj.UntagOne();
            }

            if (!untaggedObject.IsNumber)
            {
                throw new CBORException("Expected number for representation of date");
            }
            CBORNumber num = untaggedObject.AsNumber();

            if (!num.IsFinite())
            {
                throw new CBORException("Not a finite number");
            }
            if (num.CompareTo(Int64.MinValue) < 0 || num.CompareTo(Int64.MaxValue) > 0)
            {
                throw new CBORException("Date can not be represented as Instant (too small or large)");
            }
            // Section 2.4.1 of RFC7049 states:
            // Tag value 1 is for numerical representation of seconds relative to
            // 1970-01-01T00:00Z in UTC time. (For the non-negative values that the
            // Portable Operating System Interface (POSIX) defines, the number of
            // seconds is counted in the same way as for POSIX "seconds since the
            // epoch" [TIME_T].) The tagged item can be a positive or negative
            // integer (major types 0 and 1), or a floating-point number (major type
            // 7 with additional information 25, 26, or 27). Note that the number
            // can be negative (time before 1970-01-01T00:00Z) and, if a floating-
            // point number, indicate fractional seconds.
            //

            // We only support a positive integer ...
            //
            if (!num.IsInteger())
            {
                throw new CBORException(
                          String.Format("Date is represented as {0} - Only {1} is supported", num.GetType(), CBORNumber.NumberKind.Integer));
            }
            System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
            dtDateTime = dtDateTime.AddSeconds(num.ToInt64Unchecked()).ToUniversalTime();

            return(dtDateTime);
        }