WriteDateTime() public abstract method

Writes a BSON DateTime to the writer.
public abstract WriteDateTime ( long value ) : void
value long The number of milliseconds since the Unix epoch.
return void
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options) {
            var dateTimeOffset = (DateTimeOffset)value;
            var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);

            switch (representationSerializationOptions.Representation) {
            case BsonType.Array:
                bsonWriter.WriteStartArray();
                bsonWriter.WriteInt64(dateTimeOffset.UtcTicks);
                bsonWriter.WriteInt32((int)dateTimeOffset.Offset.TotalMinutes);
                bsonWriter.WriteEndArray();
                break;
            case BsonType.Document:
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteDateTime("DateTime", BsonUtils.ToMillisecondsSinceEpoch(dateTimeOffset.UtcDateTime));
                bsonWriter.WriteInt64("Ticks", dateTimeOffset.UtcTicks);
                bsonWriter.WriteInt32("Offset", (int)dateTimeOffset.Offset.TotalMinutes);
                bsonWriter.WriteEndDocument();
                break;
            default:
                var message = string.Format("'{0}' is not a valid DateTimeOffset representation.", representationSerializationOptions.Representation);
                throw new BsonSerializationException(message);
            }
        }
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            var dateTime = (DateTime)value;
            var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options);

            DateTime utcDateTime;
            if (dateTimeSerializationOptions.DateOnly)
            {
                if (dateTime.TimeOfDay != TimeSpan.Zero)
                {
                    throw new BsonSerializationException("TimeOfDay component is not zero.");
                }
                utcDateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); // not ToLocalTime
            }
            else
            {
                utcDateTime = BsonUtils.ToUniversalTime(dateTime);
            }
            var millisecondsSinceEpoch = BsonUtils.ToMillisecondsSinceEpoch(utcDateTime);

            switch (dateTimeSerializationOptions.Representation)
            {
                case BsonType.DateTime:
                    bsonWriter.WriteDateTime(millisecondsSinceEpoch);
                    break;
                case BsonType.Document:
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteDateTime("DateTime", millisecondsSinceEpoch);
                    bsonWriter.WriteInt64("Ticks", utcDateTime.Ticks);
                    bsonWriter.WriteEndDocument();
                    break;
                case BsonType.Int64:
                    bsonWriter.WriteInt64(utcDateTime.Ticks);
                    break;
                case BsonType.String:
                    if (dateTimeSerializationOptions.DateOnly)
                    {
                        bsonWriter.WriteString(dateTime.ToString("yyyy-MM-dd"));
                    }
                    else
                    {
                        // we're not using XmlConvert.ToString because of bugs in Mono
                        if (dateTime == DateTime.MinValue || dateTime == DateTime.MaxValue)
                        {
                            // serialize MinValue and MaxValue as Unspecified so we do NOT get the time zone offset
                            dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified);
                        }
                        else if (dateTime.Kind == DateTimeKind.Unspecified)
                        {
                            // serialize Unspecified as Local se we get the time zone offset
                            dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Local);
                        }
                        bsonWriter.WriteString(dateTime.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFK"));
                    }
                    break;
                default:
                    var message = string.Format("'{0}' is not a valid DateTime representation.", dateTimeSerializationOptions.Representation);
                    throw new BsonSerializationException(message);
            }
        }
 void IBsonSerializable.Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
 {
     bsonWriter.WriteStartDocument();
     bsonWriter.WriteDateTime("ts", BsonUtils.ToMillisecondsSinceEpoch(_timestamp));
     if (_info != null)
     {
         bsonWriter.WriteString("info", _info);
     }
     if (_op != null)
     {
         bsonWriter.WriteString("op", _op);
     }
     if (_namespace != null)
     {
         bsonWriter.WriteString("ns", _namespace);
     }
     if (_command != null)
     {
         bsonWriter.WriteName("command");
         _command.WriteTo(bsonWriter);
     }
     if (_query != null)
     {
         bsonWriter.WriteName("query");
         _query.WriteTo(bsonWriter);
     }
     if (_updateObject != null)
     {
         bsonWriter.WriteName("updateobj");
         _updateObject.WriteTo(bsonWriter);
     }
     if (_cursorId != 0)
     {
         bsonWriter.WriteInt64("cursorid", _cursorId);
     }
     if (_numberToReturn != 0)
     {
         bsonWriter.WriteInt32("ntoreturn", _numberToReturn);
     }
     if (_numberToSkip != 0)
     {
         bsonWriter.WriteInt32("ntoskip", _numberToSkip);
     }
     if (_exhaust)
     {
         bsonWriter.WriteBoolean("exhaust", _exhaust);
     }
     if (_numberScanned != 0)
     {
         bsonWriter.WriteInt32("nscanned", _numberScanned);
     }
     if (_idHack)
     {
         bsonWriter.WriteBoolean("idhack", _idHack);
     }
     if (_scanAndOrder)
     {
         bsonWriter.WriteBoolean("scanAndOrder", _scanAndOrder);
     }
     if (_moved)
     {
         bsonWriter.WriteBoolean("moved", _moved);
     }
     if (_fastMod)
     {
         bsonWriter.WriteBoolean("fastmod", _fastMod);
     }
     if (_fastModInsert)
     {
         bsonWriter.WriteBoolean("fastmodinsert", _fastModInsert);
     }
     if (_upsert)
     {
         bsonWriter.WriteBoolean("upsert", _upsert);
     }
     if (_keyUpdates != 0)
     {
         bsonWriter.WriteInt32("keyUpdates", _keyUpdates);
     }
     if (_exception != null)
     {
         bsonWriter.WriteString("exception", _exception);
     }
     if (_exceptionCode != 0)
     {
         bsonWriter.WriteInt32("exceptionCode", _exceptionCode);
     }
     if (_numberReturned != 0)
     {
         bsonWriter.WriteInt32("nreturned", _numberReturned);
     }
     if (_responseLength != 0)
     {
         bsonWriter.WriteInt32("responseLength", _responseLength);
     }
     bsonWriter.WriteDouble("millis", _duration.TotalMilliseconds);
     if (_client != null)
     {
         bsonWriter.WriteString("client", _client);
     }
     if (_user != null)
     {
         bsonWriter.WriteString("user", _user);
     }
     if (_error != null)
     {
         bsonWriter.WriteString("err", _error);
     }
     if (_abbreviated != null)
     {
         bsonWriter.WriteString("abbreviated", _abbreviated);
     }
     bsonWriter.WriteEndDocument();
 }
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            bool serializeIdFirst
        )
        {
            var dateTime = (DateTime) value;
            if (options.DateOnly) {
                if (dateTime.TimeOfDay != TimeSpan.Zero) {
                    throw new BsonSerializationException("TimeOfDay component for DateOnly DateTime value is not zero");
                }
            }

            switch (options.Representation) {
                case BsonType.DateTime:
                    if (dateTime.Kind != DateTimeKind.Utc) {
                        if (options.DateOnly) {
                            dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); // not ToUniversalTime!
                        } else {
                            dateTime = ToUniversalTimeHelper(dateTime);
                        }
                    }
                    bsonWriter.WriteDateTime(dateTime);
                    break;
                case BsonType.String:
                    if (options.DateOnly) {
                        bsonWriter.WriteString(dateTime.ToString("yyyy-MM-dd"));
                    } else {
                        if (dateTime == DateTime.MinValue || dateTime == DateTime.MaxValue) {
                            dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
                        }
                        bsonWriter.WriteString(XmlConvert.ToString(dateTime, XmlDateTimeSerializationMode.RoundtripKind));
                    }
                    break;
                default:
                    var message = string.Format("Invalid representation for DateTime: {0}", options.Representation);
                    throw new BsonSerializationException(message);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            var bsonDateTime = (BsonDateTime)value;
            bsonWriter.WriteDateTime(bsonDateTime.MillisecondsSinceEpoch);
        }
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     IBsonSerializationOptions options
 ) {
     // note: the DateTime portion cannot be serialized as a BsonType.DateTime because it is NOT in UTC
     var dateTimeOffset = (DateTimeOffset) value;
     var representation = (options == null) ? BsonType.Array : ((RepresentationSerializationOptions) options).Representation;
     switch (representation) {
         case BsonType.Array:
             bsonWriter.WriteStartArray();
             bsonWriter.WriteInt64("0", dateTimeOffset.Ticks);
             bsonWriter.WriteInt32("1", (int) dateTimeOffset.Offset.TotalMinutes);
             bsonWriter.WriteEndArray();
             break;
         case BsonType.Document:
             bsonWriter.WriteStartDocument();
             bsonWriter.WriteDateTime("DateTime", dateTimeOffset.UtcDateTime);
             bsonWriter.WriteInt64("Ticks", dateTimeOffset.Ticks);
             bsonWriter.WriteInt32("Offset", (int) dateTimeOffset.Offset.TotalMinutes);
             bsonWriter.WriteEndDocument();
             break;
         case BsonType.String:
             bsonWriter.WriteString(XmlConvert.ToString(dateTimeOffset));
             break;
         default:
             var message = string.Format("'{0}' is not a valid representation for type 'DateTimeOffset'", representation);
             throw new BsonSerializationException(message);
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var profileInfo = (SystemProfileInfo)value;

                bsonWriter.WriteStartDocument();
                bsonWriter.WriteDateTime("ts", BsonUtils.ToMillisecondsSinceEpoch(profileInfo.Timestamp));
                if (profileInfo.Info != null)
                {
                    bsonWriter.WriteString("info", profileInfo.Info);
                }
                if (profileInfo.Op != null)
                {
                    bsonWriter.WriteString("op", profileInfo.Op);
                }
                if (profileInfo.Namespace != null)
                {
                    bsonWriter.WriteString("ns", profileInfo.Namespace);
                }
                if (profileInfo.Command != null)
                {
                    bsonWriter.WriteName("command");
                    profileInfo.Command.WriteTo(bsonWriter);
                }
                if (profileInfo.Query != null)
                {
                    bsonWriter.WriteName("query");
                    profileInfo.Query.WriteTo(bsonWriter);
                }
                if (profileInfo.UpdateObject != null)
                {
                    bsonWriter.WriteName("updateobj");
                    profileInfo.UpdateObject.WriteTo(bsonWriter);
                }
                if (profileInfo.CursorId != 0)
                {
                    bsonWriter.WriteInt64("cursorid", profileInfo.CursorId);
                }
                if (profileInfo.NumberToReturn != 0)
                {
                    bsonWriter.WriteInt32("ntoreturn", profileInfo.NumberToReturn);
                }
                if (profileInfo.NumberToSkip != 0)
                {
                    bsonWriter.WriteInt32("ntoskip", profileInfo.NumberToSkip);
                }
                if (profileInfo.Exhaust)
                {
                    bsonWriter.WriteBoolean("exhaust", profileInfo.Exhaust);
                }
                if (profileInfo.NumberScanned != 0)
                {
                    bsonWriter.WriteInt32("nscanned", profileInfo.NumberScanned);
                }
                if (profileInfo.IdHack)
                {
                    bsonWriter.WriteBoolean("idhack", profileInfo.IdHack);
                }
                if (profileInfo.ScanAndOrder)
                {
                    bsonWriter.WriteBoolean("scanAndOrder", profileInfo.ScanAndOrder);
                }
                if (profileInfo.Moved)
                {
                    bsonWriter.WriteBoolean("moved", profileInfo.Moved);
                }
                if (profileInfo.FastMod)
                {
                    bsonWriter.WriteBoolean("fastmod", profileInfo.FastMod);
                }
                if (profileInfo.FastModInsert)
                {
                    bsonWriter.WriteBoolean("fastmodinsert", profileInfo.FastModInsert);
                }
                if (profileInfo.Upsert)
                {
                    bsonWriter.WriteBoolean("upsert", profileInfo.Upsert);
                }
                if (profileInfo.KeyUpdates != 0)
                {
                    bsonWriter.WriteInt32("keyUpdates", profileInfo.KeyUpdates);
                }
                if (profileInfo.Exception != null)
                {
                    bsonWriter.WriteString("exception", profileInfo.Exception);
                }
                if (profileInfo.ExceptionCode != 0)
                {
                    bsonWriter.WriteInt32("exceptionCode", profileInfo.ExceptionCode);
                }
                if (profileInfo.NumberReturned != 0)
                {
                    bsonWriter.WriteInt32("nreturned", profileInfo.NumberReturned);
                }
                if (profileInfo.ResponseLength != 0)
                {
                    bsonWriter.WriteInt32("responseLength", profileInfo.ResponseLength);
                }
                bsonWriter.WriteDouble("millis", profileInfo.Duration.TotalMilliseconds);
                if (profileInfo.Client != null)
                {
                    bsonWriter.WriteString("client", profileInfo.Client);
                }
                if (profileInfo.User != null)
                {
                    bsonWriter.WriteString("user", profileInfo.User);
                }
                if (profileInfo.Error != null)
                {
                    bsonWriter.WriteString("err", profileInfo.Error);
                }
                if (profileInfo.Abbreviated != null)
                {
                    bsonWriter.WriteString("abbreviated", profileInfo.Abbreviated);
                }
                bsonWriter.WriteEndDocument();
            }
        }
 public override void Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     object value,
     bool serializeIdFirst
 )
 {
     if (value == null) {
         bsonWriter.WriteNull();
     } else {
         bsonWriter.WriteDateTime(((BsonDateTime) value).Value);
     }
 }
        /// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options
        ) {
            var dateTime = (DateTime) value;
            var dateTimeOptions = (options == null) ? DateTimeSerializationOptions.Defaults : (DateTimeSerializationOptions) options;

            if (dateTimeOptions.DateOnly) {
                if (dateTime.TimeOfDay != TimeSpan.Zero) {
                    throw new BsonSerializationException("TimeOfDay component for DateOnly DateTime value is not zero");
                }
            }

            if (dateTimeOptions.Representation != BsonType.String) {
                if (dateTime.Kind != DateTimeKind.Utc) {
                    if (dateTimeOptions.DateOnly) {
                        dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); // not ToUniversalTime!
                    } else {
                        dateTime = ToUniversalTimeHelper(dateTime);
                    }
                }
            }

            switch (dateTimeOptions.Representation) {
                case BsonType.DateTime:
                    bsonWriter.WriteDateTime(dateTime);
                    break;
                case BsonType.Document:
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteDateTime("DateTime", dateTime);
                    bsonWriter.WriteInt64("Ticks", dateTime.Ticks);
                    bsonWriter.WriteEndDocument();
                    break;
                case BsonType.Int64:
                    bsonWriter.WriteInt64(dateTime.Ticks);
                    break;
                case BsonType.String:
                    if (dateTimeOptions.DateOnly) {
                        bsonWriter.WriteString(dateTime.ToString("yyyy-MM-dd"));
                    } else {
                        // we're not using XmlConvert.ToString because of bugs in Mono
                        if (dateTime == DateTime.MinValue || dateTime == DateTime.MaxValue) {
                            // serialize MinValue and MaxValue as Unspecified so we do NOT get the time zone offset
                            dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified);
                        } else if (dateTime.Kind == DateTimeKind.Unspecified) {
                            // serialize Unspecified as Local se we get the time zone offset
                            dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Local);
                        }
                        bsonWriter.WriteString(dateTime.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFK"));
                    }
                    break;
                default:
                    var message = string.Format("'{0}' is not a valid representation for type 'DateTime'", dateTimeOptions.Representation);
                    throw new BsonSerializationException(message);
            }
        }
        public override void Serialize(
            BsonWriter bsonWriter,
            Type nominalType,
            object value,
            IBsonSerializationOptions options
        )
        {
            var dateTime = (DateTime) value;
            var dateTimeOptions = (options == null) ? DateTimeSerializationOptions.Defaults : (DateTimeSerializationOptions) options;

            if (dateTimeOptions.DateOnly) {
                if (dateTime.TimeOfDay != TimeSpan.Zero) {
                    throw new BsonSerializationException("TimeOfDay component for DateOnly DateTime value is not zero");
                }
            }
            if (dateTime.Kind != DateTimeKind.Utc && dateTimeOptions.Representation != BsonType.String) {
                if (dateTimeOptions.DateOnly) {
                    dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc); // not ToUniversalTime!
                } else {
                    dateTime = ToUniversalTimeHelper(dateTime);
                }
            }

            switch (dateTimeOptions.Representation) {
                case BsonType.DateTime:
                    bsonWriter.WriteDateTime(dateTime);
                    break;
                case BsonType.Document:
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteDateTime("DateTime", dateTime);
                    bsonWriter.WriteInt64("Ticks", dateTime.Ticks);
                    bsonWriter.WriteEndDocument();
                    break;
                case BsonType.Int64:
                    bsonWriter.WriteInt64(dateTime.Ticks);
                    break;
                case BsonType.String:
                    if (dateTimeOptions.DateOnly) {
                        bsonWriter.WriteString(dateTime.ToString("yyyy-MM-dd"));
                    } else {
                        if (dateTime == DateTime.MinValue || dateTime == DateTime.MaxValue) {
                            dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
                        }
                        bsonWriter.WriteString(XmlConvert.ToString(dateTime, XmlDateTimeSerializationMode.RoundtripKind));
                    }
                    break;
                default:
                    var message = string.Format("'{0}' is not a valid representation for type 'DateTime'", dateTimeOptions.Representation);
                    throw new BsonSerializationException(message);
            }
        }
Exemplo n.º 11
0
 void IBsonSerializable.Serialize(
     BsonWriter bsonWriter,
     Type nominalType,
     IBsonSerializationOptions options
 )
 {
     bsonWriter.WriteStartDocument();
     bsonWriter.WriteDateTime("ts", BsonUtils.ToMillisecondsSinceEpoch(timestamp));
     if (info != null) {
         bsonWriter.WriteString("info", info);
     }
     if (op != null) {
         bsonWriter.WriteString("op", op);
     }
     if (@namespace != null) {
         bsonWriter.WriteString("ns", @namespace);
     }
     if (command != null) {
         bsonWriter.WriteName("command");
         command.WriteTo(bsonWriter);
     }
     if (query != null) {
         bsonWriter.WriteName("query");
         query.WriteTo(bsonWriter);
     }
     if (updateObject != null) {
         bsonWriter.WriteName("updateobj");
         updateObject.WriteTo(bsonWriter);
     }
     if (cursorId != 0) {
         bsonWriter.WriteInt64("cursorid", cursorId);
     }
     if (numberToReturn != 0) {
         bsonWriter.WriteInt32("ntoreturn", numberToReturn);
     }
     if (numberToSkip != 0) {
         bsonWriter.WriteInt32("ntoskip", numberToSkip);
     }
     if (exhaust) {
         bsonWriter.WriteBoolean("exhaust", exhaust);
     }
     if (numberScanned != 0) {
         bsonWriter.WriteInt32("nscanned", numberScanned);
     }
     if (idHack) {
         bsonWriter.WriteBoolean("idhack", idHack);
     }
     if (scanAndOrder) {
         bsonWriter.WriteBoolean("scanAndOrder", scanAndOrder);
     }
     if (moved) {
         bsonWriter.WriteBoolean("moved", moved);
     }
     if (fastMod) {
         bsonWriter.WriteBoolean("fastmod", fastMod);
     }
     if (fastModInsert) {
         bsonWriter.WriteBoolean("fastmodinsert", fastModInsert);
     }
     if (upsert) {
         bsonWriter.WriteBoolean("upsert", upsert);
     }
     if (keyUpdates != 0) {
         bsonWriter.WriteInt32("keyUpdates", keyUpdates);
     }
     if (exception != null) {
         bsonWriter.WriteString("exception", exception);
     }
     if (exceptionCode != 0) {
         bsonWriter.WriteInt32("exceptionCode", exceptionCode);
     }
     if (numberReturned != 0) {
         bsonWriter.WriteInt32("nreturned", numberReturned);
     }
     if (responseLength != 0) {
         bsonWriter.WriteInt32("responseLength", responseLength);
     }
     bsonWriter.WriteDouble("millis", duration.TotalMilliseconds);
     if (client != null) {
         bsonWriter.WriteString("client", client);
     }
     if (user != null) {
         bsonWriter.WriteString("user", user);
     }
     if (error != null) {
         bsonWriter.WriteString("err", error);
     }
     if (abbreviated != null) {
         bsonWriter.WriteString("abbreviated", abbreviated);
     }
     bsonWriter.WriteEndDocument();
 }