BsonDateTime.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using MongoDB.Bson.IO;
  17. namespace MongoDB.Bson
  18. {
  19. /// <summary>
  20. /// Represents a BSON DateTime value.
  21. /// </summary>
  22. [Serializable]
  23. public class BsonDateTime : BsonValue, IComparable<BsonDateTime>, IEquatable<BsonDateTime>
  24. {
  25. // private fields
  26. private readonly long _millisecondsSinceEpoch;
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the BsonDateTime class.
  30. /// </summary>
  31. /// <param name="value">A DateTime.</param>
  32. public BsonDateTime(DateTime value)
  33. {
  34. _millisecondsSinceEpoch = BsonUtils.ToMillisecondsSinceEpoch(value);
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the BsonDateTime class.
  38. /// </summary>
  39. /// <param name="millisecondsSinceEpoch">Milliseconds since Unix Epoch.</param>
  40. public BsonDateTime(long millisecondsSinceEpoch)
  41. {
  42. _millisecondsSinceEpoch = millisecondsSinceEpoch;
  43. }
  44. // public properties
  45. /// <summary>
  46. /// Gets the BsonType of this BsonValue.
  47. /// </summary>
  48. public override BsonType BsonType
  49. {
  50. get { return BsonType.DateTime; }
  51. }
  52. /// <summary>
  53. /// Gets whether this BsonDateTime is a valid .NET DateTime.
  54. /// </summary>
  55. public override bool IsValidDateTime
  56. {
  57. get
  58. {
  59. return
  60. _millisecondsSinceEpoch >= BsonConstants.DateTimeMinValueMillisecondsSinceEpoch &&
  61. _millisecondsSinceEpoch <= BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch;
  62. }
  63. }
  64. /// <summary>
  65. /// Gets the number of milliseconds since the Unix Epoch.
  66. /// </summary>
  67. public long MillisecondsSinceEpoch
  68. {
  69. get { return _millisecondsSinceEpoch; }
  70. }
  71. /// <summary>
  72. /// Gets the number of milliseconds since the Unix Epoch.
  73. /// </summary>
  74. [Obsolete("Use MillisecondsSinceEpoch instead.")]
  75. public override object RawValue
  76. {
  77. get { return _millisecondsSinceEpoch; }
  78. }
  79. /// <summary>
  80. /// Gets the DateTime value.
  81. /// </summary>
  82. [Obsolete("Use ToUniversalTime instead.")]
  83. public DateTime Value
  84. {
  85. get
  86. {
  87. return BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(_millisecondsSinceEpoch);
  88. }
  89. }
  90. // public operators
  91. /// <summary>
  92. /// Converts a DateTime to a BsonDateTime.
  93. /// </summary>
  94. /// <param name="value">A DateTime.</param>
  95. /// <returns>A BsonDateTime.</returns>
  96. public static implicit operator BsonDateTime(DateTime value)
  97. {
  98. return new BsonDateTime(value);
  99. }
  100. /// <summary>
  101. /// Compares two BsonDateTime values.
  102. /// </summary>
  103. /// <param name="lhs">The first BsonDateTime.</param>
  104. /// <param name="rhs">The other BsonDateTime.</param>
  105. /// <returns>True if the two BsonDateTime values are not equal according to ==.</returns>
  106. public static bool operator !=(BsonDateTime lhs, BsonDateTime rhs)
  107. {
  108. return !(lhs == rhs);
  109. }
  110. /// <summary>
  111. /// Compares two BsonDateTime values.
  112. /// </summary>
  113. /// <param name="lhs">The first BsonDateTime.</param>
  114. /// <param name="rhs">The other BsonDateTime.</param>
  115. /// <returns>True if the two BsonDateTime values are equal according to ==.</returns>
  116. public static bool operator ==(BsonDateTime lhs, BsonDateTime rhs)
  117. {
  118. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  119. return lhs.Equals(rhs);
  120. }
  121. // public static methods
  122. /// <summary>
  123. /// Creates a new BsonDateTime.
  124. /// </summary>
  125. /// <param name="value">An object to be mapped to a BsonDateTime.</param>
  126. /// <returns>A BsonDateTime or null.</returns>
  127. public new static BsonDateTime Create(object value)
  128. {
  129. if (value == null)
  130. {
  131. throw new ArgumentNullException("value");
  132. }
  133. return (BsonDateTime)BsonTypeMapper.MapToBsonValue(value, BsonType.DateTime);
  134. }
  135. // public methods
  136. /// <summary>
  137. /// Compares this BsonDateTime to another BsonDateTime.
  138. /// </summary>
  139. /// <param name="other">The other BsonDateTime.</param>
  140. /// <returns>A 32-bit signed integer that indicates whether this BsonDateTime is less than, equal to, or greather than the other.</returns>
  141. public int CompareTo(BsonDateTime other)
  142. {
  143. if (other == null) { return 1; }
  144. return _millisecondsSinceEpoch.CompareTo(other._millisecondsSinceEpoch);
  145. }
  146. /// <summary>
  147. /// Compares the BsonDateTime to another BsonValue.
  148. /// </summary>
  149. /// <param name="other">The other BsonValue.</param>
  150. /// <returns>A 32-bit signed integer that indicates whether this BsonDateTime is less than, equal to, or greather than the other BsonValue.</returns>
  151. public override int CompareTo(BsonValue other)
  152. {
  153. if (other == null) { return 1; }
  154. var otherDateTime = other as BsonDateTime;
  155. if (otherDateTime != null)
  156. {
  157. return _millisecondsSinceEpoch.CompareTo(otherDateTime._millisecondsSinceEpoch);
  158. }
  159. var otherTimestamp = other as BsonTimestamp;
  160. if (otherTimestamp != null)
  161. {
  162. return _millisecondsSinceEpoch.CompareTo(otherTimestamp.Timestamp * 1000L); // timestamp is in seconds
  163. }
  164. return CompareTypeTo(other);
  165. }
  166. /// <summary>
  167. /// Compares this BsonDateTime to another BsonDateTime.
  168. /// </summary>
  169. /// <param name="rhs">The other BsonDateTime.</param>
  170. /// <returns>True if the two BsonDateTime values are equal.</returns>
  171. public bool Equals(BsonDateTime rhs)
  172. {
  173. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  174. return _millisecondsSinceEpoch == rhs._millisecondsSinceEpoch;
  175. }
  176. /// <summary>
  177. /// Compares this BsonDateTime to another object.
  178. /// </summary>
  179. /// <param name="obj">The other object.</param>
  180. /// <returns>True if the other object is a BsonDateTime and equal to this one.</returns>
  181. public override bool Equals(object obj)
  182. {
  183. return Equals(obj as BsonDateTime); // works even if obj is null or of a different type
  184. }
  185. /// <summary>
  186. /// Gets the hash code.
  187. /// </summary>
  188. /// <returns>The hash code.</returns>
  189. public override int GetHashCode()
  190. {
  191. // see Effective Java by Joshua Bloch
  192. int hash = 17;
  193. hash = 37 * hash + BsonType.GetHashCode();
  194. hash = 37 * hash + _millisecondsSinceEpoch.GetHashCode();
  195. return hash;
  196. }
  197. /// <summary>
  198. /// Converts this BsonValue to a DateTime in local time.
  199. /// </summary>
  200. /// <returns>A DateTime.</returns>
  201. public override DateTime ToLocalTime()
  202. {
  203. return BsonUtils.ToLocalTime(ToUniversalTime());
  204. }
  205. /// <summary>
  206. /// Converts this BsonValue to a DateTime? in local time.
  207. /// </summary>
  208. /// <returns>A DateTime?.</returns>
  209. public override DateTime? ToNullableLocalTime()
  210. {
  211. return ToLocalTime();
  212. }
  213. /// <summary>
  214. /// Converts this BsonValue to a DateTime? in UTC.
  215. /// </summary>
  216. /// <returns>A DateTime?.</returns>
  217. public override DateTime? ToNullableUniversalTime()
  218. {
  219. return ToUniversalTime();
  220. }
  221. /// <summary>
  222. /// Converts this BsonValue to a DateTime in UTC.
  223. /// </summary>
  224. /// <returns>A DateTime.</returns>
  225. public override DateTime ToUniversalTime()
  226. {
  227. return BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(_millisecondsSinceEpoch);
  228. }
  229. /// <summary>
  230. /// Returns a string representation of the value.
  231. /// </summary>
  232. /// <returns>A string representation of the value.</returns>
  233. public override string ToString()
  234. {
  235. if (IsValidDateTime)
  236. {
  237. return BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(_millisecondsSinceEpoch).ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFK");
  238. }
  239. else
  240. {
  241. return JsonConvert.ToString(_millisecondsSinceEpoch);
  242. }
  243. }
  244. // protected methods
  245. /// <inheritdoc/>
  246. protected override TypeCode IConvertibleGetTypeCodeImplementation()
  247. {
  248. return TypeCode.DateTime;
  249. }
  250. /// <inheritdoc/>
  251. protected override DateTime IConvertibleToDateTimeImplementation(IFormatProvider provider)
  252. {
  253. return ToUniversalTime();
  254. }
  255. }
  256. }