/* Copyright 2010-2014 MongoDB Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Xml; namespace MongoDB.Bson { /// /// Represents a BSON DateTime value. /// [Serializable] public class BsonDateTime : BsonValue, IComparable, IEquatable { // private fields private long _millisecondsSinceEpoch; // constructors /// /// Initializes a new instance of the BsonDateTime class. /// /// A DateTime. public BsonDateTime(DateTime value) : base(BsonType.DateTime) { _millisecondsSinceEpoch = BsonUtils.ToMillisecondsSinceEpoch(value); } /// /// Initializes a new instance of the BsonDateTime class. /// /// Milliseconds since Unix Epoch. public BsonDateTime(long millisecondsSinceEpoch) : base(BsonType.DateTime) { _millisecondsSinceEpoch = millisecondsSinceEpoch; } // public properties /// /// Gets whether this BsonDateTime is a valid .NET DateTime. /// public override bool IsValidDateTime { get { return _millisecondsSinceEpoch >= BsonConstants.DateTimeMinValueMillisecondsSinceEpoch && _millisecondsSinceEpoch <= BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch; } } /// /// Gets the number of milliseconds since the Unix Epoch. /// public long MillisecondsSinceEpoch { get { return _millisecondsSinceEpoch; } } /// /// Gets the number of milliseconds since the Unix Epoch. /// [Obsolete("Use MillisecondsSinceEpoch instead.")] public override object RawValue { get { return _millisecondsSinceEpoch; } } /// /// Gets the DateTime value. /// [Obsolete("Use ToUniversalTime instead.")] public DateTime Value { get { return BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(_millisecondsSinceEpoch); } } // public operators /// /// Converts a DateTime to a BsonDateTime. /// /// A DateTime. /// A BsonDateTime. public static implicit operator BsonDateTime(DateTime value) { return new BsonDateTime(value); } /// /// Compares two BsonDateTime values. /// /// The first BsonDateTime. /// The other BsonDateTime. /// True if the two BsonDateTime values are not equal according to ==. public static bool operator !=(BsonDateTime lhs, BsonDateTime rhs) { return !(lhs == rhs); } /// /// Compares two BsonDateTime values. /// /// The first BsonDateTime. /// The other BsonDateTime. /// True if the two BsonDateTime values are equal according to ==. public static bool operator ==(BsonDateTime lhs, BsonDateTime rhs) { if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); } return lhs.Equals(rhs); } // public static methods /// /// Creates a new BsonDateTime. /// /// A DateTime. /// A BsonDateTime. [Obsolete("Use new BsonDateTime(DateTime value) instead.")] public static BsonDateTime Create(DateTime value) { return new BsonDateTime(value); } /// /// Creates a new BsonDateTime. /// /// A DateTime. /// Milliseconds since Unix Epoch. [Obsolete("Use new BsonDateTime(long millisecondsSinceEpoch) instead.")] public static BsonDateTime Create(long millisecondsSinceEpoch) { return new BsonDateTime(millisecondsSinceEpoch); } /// /// Creates a new BsonDateTime. /// /// An object to be mapped to a BsonDateTime. /// A BsonDateTime or null. public new static BsonDateTime Create(object value) { if (value != null) { return (BsonDateTime)BsonTypeMapper.MapToBsonValue(value, BsonType.DateTime); } else { return null; } } // public methods /// /// Compares this BsonDateTime to another BsonDateTime. /// /// The other BsonDateTime. /// A 32-bit signed integer that indicates whether this BsonDateTime is less than, equal to, or greather than the other. public int CompareTo(BsonDateTime other) { if (other == null) { return 1; } return _millisecondsSinceEpoch.CompareTo(other._millisecondsSinceEpoch); } /// /// Compares the BsonDateTime to another BsonValue. /// /// The other BsonValue. /// A 32-bit signed integer that indicates whether this BsonDateTime is less than, equal to, or greather than the other BsonValue. public override int CompareTo(BsonValue other) { if (other == null) { return 1; } var otherDateTime = other as BsonDateTime; if (otherDateTime != null) { return _millisecondsSinceEpoch.CompareTo(otherDateTime._millisecondsSinceEpoch); } var otherTimestamp = other as BsonTimestamp; if (otherTimestamp != null) { return _millisecondsSinceEpoch.CompareTo(otherTimestamp.Timestamp * 1000L); // timestamp is in seconds } return CompareTypeTo(other); } /// /// Compares this BsonDateTime to another BsonDateTime. /// /// The other BsonDateTime. /// True if the two BsonDateTime values are equal. public bool Equals(BsonDateTime rhs) { if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; } return _millisecondsSinceEpoch == rhs._millisecondsSinceEpoch; } /// /// Compares this BsonDateTime to another object. /// /// The other object. /// True if the other object is a BsonDateTime and equal to this one. public override bool Equals(object obj) { return Equals(obj as BsonDateTime); // works even if obj is null or of a different type } /// /// Gets the hash code. /// /// The hash code. public override int GetHashCode() { // see Effective Java by Joshua Bloch int hash = 17; hash = 37 * hash + BsonType.GetHashCode(); hash = 37 * hash + _millisecondsSinceEpoch.GetHashCode(); return hash; } /// /// Converts this BsonValue to a DateTime in local time. /// /// A DateTime. public override DateTime ToLocalTime() { return BsonUtils.ToLocalTime(ToUniversalTime()); } /// /// Converts this BsonValue to a DateTime? in local time. /// /// A DateTime?. public override DateTime? ToNullableLocalTime() { return ToLocalTime(); } /// /// Converts this BsonValue to a DateTime? in UTC. /// /// A DateTime?. public override DateTime? ToNullableUniversalTime() { return ToUniversalTime(); } /// /// Converts this BsonValue to a DateTime in UTC. /// /// A DateTime. public override DateTime ToUniversalTime() { return BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(_millisecondsSinceEpoch); } /// /// Returns a string representation of the value. /// /// A string representation of the value. public override string ToString() { if (IsValidDateTime) { return BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(_millisecondsSinceEpoch).ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFK"); } else { return XmlConvert.ToString(_millisecondsSinceEpoch); } } } }