MongoDB.Bson.BsonUtils.ToUniversalTime C# (CSharp) Method

ToUniversalTime() public static method

Converts a DateTime to UTC (with special handling for MinValue and MaxValue).
public static ToUniversalTime ( System.DateTime dateTime ) : System.DateTime
dateTime System.DateTime A DateTime.
return System.DateTime
        public static DateTime ToUniversalTime(DateTime dateTime)
        {
            if (dateTime == DateTime.MinValue)
            {
                return DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc);
            }
            else if (dateTime == DateTime.MaxValue)
            {
                return DateTime.SpecifyKind(DateTime.MaxValue, DateTimeKind.Utc);
            }
            else
            {
                return dateTime.ToUniversalTime();
            }
        }

Usage Example

示例#1
0
        private static int GetTimestampFromDateTime(DateTime timestamp)
        {
            var secondsSinceEpoch = (long)Math.Floor((BsonUtils.ToUniversalTime(timestamp) - BsonConstants.UnixEpoch).TotalSeconds);

            if (secondsSinceEpoch < int.MinValue || secondsSinceEpoch > int.MaxValue)
            {
                throw new ArgumentOutOfRangeException("timestamp");
            }
            return((int)secondsSinceEpoch);
        }
All Usage Examples Of MongoDB.Bson.BsonUtils::ToUniversalTime