public DateTime FromCBORObject(CBORObject obj) { if (obj.HasMostOuterTag(0)) { try { return(StringToDateTime(obj.AsString())); } catch (OverflowException ex) { throw new CBORException(ex.Message, ex); } catch (ArgumentException ex) { throw new CBORException(ex.Message, ex); } } else if (obj.HasMostOuterTag(1)) { if (!obj.IsFinite) { throw new CBORException("Not a finite number"); } EDecimal dec = obj.AsEDecimal(); var lesserFields = new int[7]; var year = new EInteger[1]; CBORUtilities.BreakDownSecondsSinceEpoch( dec, year, lesserFields); return(PropertyMap.BuildUpDateTime(year[0], lesserFields)); } throw new CBORException("Not tag 0 or 1"); }
public static DateTime StringToDateTime(string str) { var lesserFields = new int[7]; var year = new EInteger[1]; CBORUtilities.ParseAtomDateTimeString(str, year, lesserFields); return(PropertyMap.BuildUpDateTime(year[0], lesserFields)); }
/// <summary>Converts a CBOR object to a DateTime (in DotNet) or a Date /// (in Java).</summary> /// <param name='obj'>A CBOR object that specifies a date/time /// according to the conversion type used to create this date /// converter.</param> /// <returns>A DateTime or Date that encodes the date/time specified in /// the CBOR object.</returns> /// <exception cref='ArgumentNullException'>The parameter <paramref /// name='obj'/> is null.</exception> /// <exception cref='PeterO.Cbor.CBORException'>The format of the CBOR /// object is not supported, or another error occurred in /// conversion.</exception> public DateTime FromCBORObject(CBORObject obj) { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } var lesserFields = new int[7]; var outYear = new EInteger[1]; string str = this.TryGetDateTimeFieldsInternal( obj, outYear, lesserFields); if (str == null) { return(PropertyMap.BuildUpDateTime(outYear[0], lesserFields)); } throw new CBORException(str); }