BsonDateTime.cs 9.7 KB

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