BsonDateTime.cs 10.0 KB

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