private long ToInt64(TimeSpan timeSpan, TimeSpanUnits units) { if (units == TimeSpanUnits.Nanoseconds) { return timeSpan.Ticks * 100; } else { return timeSpan.Ticks / TicksPerUnit(units); } }
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 } }
private int ToInt32(TimeSpan timeSpan, TimeSpanUnits units) { if (units == TimeSpanUnits.Nanoseconds) { return (int)(timeSpan.Ticks * 100); } else { return (int)(timeSpan.Ticks / TicksPerUnit(units)); } }
private TimeSpan FromInt64(long value, TimeSpanUnits units) { if (units == TimeSpanUnits.Nanoseconds) { return TimeSpan.FromTicks(value / 100); } else { return TimeSpan.FromTicks(value * TicksPerUnit(units)); } }
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); } }
/// <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; }
// 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 } }
// 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); }
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; }
/// <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; }
private EventCallback <MouseEventArgs> GetIncreaseCallback(TimeSpanUnits unit) => EventCallback.Factory.Create <MouseEventArgs>(this, () => Increase(unit));