コード例 #1
0
 private long ToInt64(TimeSpan timeSpan, TimeSpanUnits units)
 {
     if (units == TimeSpanUnits.Nanoseconds)
     {
         return timeSpan.Ticks * 100;
     }
     else
     {
         return timeSpan.Ticks / TicksPerUnit(units);
     }
 }
コード例 #2
0
 private double ToDouble(TimeSpan timeSpan, TimeSpanUnits units)
 {
     if (units == TimeSpanUnits.Nanoseconds)
     {
         return (double)(timeSpan.Ticks) * 100.0;
     }
     else
     {
         return (double)timeSpan.Ticks / (double)TicksPerUnit(units); // cast first then divide to preserve fractional part of result
     }
 }
コード例 #3
0
 private int ToInt32(TimeSpan timeSpan, TimeSpanUnits units)
 {
     if (units == TimeSpanUnits.Nanoseconds)
     {
         return (int)(timeSpan.Ticks * 100); 
     }
     else
     {
         return (int)(timeSpan.Ticks / TicksPerUnit(units));
     }
 }
コード例 #4
0
 private TimeSpan FromInt64(long value, TimeSpanUnits units)
 {
     if (units == TimeSpanUnits.Nanoseconds)
     {
         return TimeSpan.FromTicks(value / 100);
     }
     else
     {
         return TimeSpan.FromTicks(value * TicksPerUnit(units));
     }
 }
コード例 #5
0
 private long TicksPerUnit(TimeSpanUnits units)
 {
     switch (units)
     {
         case TimeSpanUnits.Days: return TimeSpan.TicksPerDay;
         case TimeSpanUnits.Hours: return TimeSpan.TicksPerHour;
         case TimeSpanUnits.Minutes: return TimeSpan.TicksPerMinute;
         case TimeSpanUnits.Seconds: return TimeSpan.TicksPerSecond;
         case TimeSpanUnits.Milliseconds: return TimeSpan.TicksPerMillisecond;
         case TimeSpanUnits.Microseconds: return TimeSpan.TicksPerMillisecond / 1000;
         case TimeSpanUnits.Ticks: return 1;
         default:
             var message = string.Format("Invalid TimeSpanUnits value: {0}.", units);
             throw new ArgumentException(message);
     }
 }
コード例 #6
0
 /// <summary>
 /// Initializes a new instance of the TimeSpanSerializationOptions class.
 /// </summary>
 /// <param name="representation">The representation for serialized TimeSpans.</param>
 /// <param name="units">The units for serialized TimeSpans.</param>
 public TimeSpanSerializationOptions(BsonType representation, TimeSpanUnits units)
 {
     _representation = representation;
     _units = units;
 }
コード例 #7
0
 // private methods
 private TimeSpan FromDouble(double value, TimeSpanUnits units)
 {
     if (units == TimeSpanUnits.Nanoseconds)
     {
         return TimeSpan.FromTicks((long)(value / 100.0)); // divide first then cast to reduce chance of overflow
     }
     else
     {
         return TimeSpan.FromTicks((long)(value * TicksPerUnit(units))); // multiply first then cast to preserve fractional part of value
     }
 }
コード例 #8
0
        // public methods
        /// <summary>
        /// Apply an attribute to these serialization options and modify the options accordingly.
        /// </summary>
        /// <param name="serializer">The serializer that these serialization options are for.</param>
        /// <param name="attribute">The serialization options attribute.</param>
        public override void ApplyAttribute(IBsonSerializer serializer, Attribute attribute)
        {
            EnsureNotFrozen();
            var representationAttribute = attribute as BsonRepresentationAttribute;
            if (representationAttribute != null)
            {
                _representation = representationAttribute.Representation;
                return;
            }

            var optionsAttribute = attribute as BsonTimeSpanOptionsAttribute;
            if (optionsAttribute != null)
            {
                _representation = optionsAttribute.Representation;
                _units = optionsAttribute.Units;
                return;
            }

            var message = string.Format("A serialization options attribute of type {0} cannot be applied to serialization options of type {1}.",
                BsonUtils.GetFriendlyTypeName(attribute.GetType()), BsonUtils.GetFriendlyTypeName(GetType()));
            throw new NotSupportedException(message);
        }
コード例 #9
0
        private DateTime getLifetimeSince(double length, TimeSpanUnits units)
        {
            DateTime since = DateTime.MaxValue;
            double lengthModifier = -1 * length;

            switch (units)
            {
                case TimeSpanUnits.Hours:
                    since = _clock.UtcNow.AddHours(lengthModifier);
                    break;
                case TimeSpanUnits.Minutes:
                    since = _clock.UtcNow.AddMinutes(lengthModifier);
                    break;
                case TimeSpanUnits.Seconds:
                    since = _clock.UtcNow.AddSeconds(lengthModifier);
                    break;
            }

            return since;
        }
コード例 #10
0
 /// <summary>
 /// Initializes a new instance of the BsonTimeSpanOptionsAttribute class.
 /// </summary>
 /// <param name="representation">The external representation.</param>
 /// <param name="units">The TimeSpanUnits.</param>
 public BsonTimeSpanOptionsAttribute(BsonType representation, TimeSpanUnits units)
 {
     _representation = representation;
     _units = units;
 }
コード例 #11
0
 private EventCallback <MouseEventArgs> GetIncreaseCallback(TimeSpanUnits unit) => EventCallback.Factory.Create <MouseEventArgs>(this, () => Increase(unit));
コード例 #12
0
 /// <summary>
 /// Initializes a new instance of the TimeSpanSerializationOptions class.
 /// </summary>
 /// <param name="representation">The representation for serialized TimeSpans.</param>
 /// <param name="units">The units for serialized TimeSpans.</param>
 public TimeSpanSerializationOptions(BsonType representation, TimeSpanUnits units)
 {
     _representation = representation;
     _units          = units;
 }