Пример #1
0
        /// <summary>
        /// Converts a Protobuf <see cref="ProtobufDuration"/> to a Noda Time <see cref="NodaDuration"/>.
        /// </summary>
        /// <remarks>
        /// Every valid Protobuf duration can be represented in Noda Time without loss of information.
        /// </remarks>
        /// <param name="duration">The duration to convert. Must not be null.</param>
        /// <exception cref="ArgumentException"><paramref name="duration"/> represents an invalid duration.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="duration"/> is null.</exception>
        /// <returns>The Noda Time representation.</returns>
        public static NodaDuration ToNodaDuration(this ProtobufDuration duration)
        {
            Preconditions.CheckNotNull(duration, nameof(duration));
            long seconds = duration.Seconds;
            long nanos   = duration.Nanos;

            Preconditions.CheckArgument(
                seconds >= ProtobufDuration.MinSeconds &&
                seconds <= ProtobufDuration.MaxSeconds,
                nameof(duration),
                "duration.Seconds out of range: {0}",
                seconds);
            Preconditions.CheckArgument(
                nanos > -ProtobufDuration.NanosecondsPerSecond &&
                nanos < ProtobufDuration.NanosecondsPerSecond,
                nameof(duration),
                "duration.Nanos out of range: {0}",
                nanos);
            // If either sign is 0, we're fine. Otherwise, they should be the same. Multiplying them
            // together seems the easiest way to check that.
            Preconditions.CheckArgument(Math.Sign(seconds) * Math.Sign(nanos) != -1,
                                        nameof(duration),
                                        "duration.Seconds and duration.Nanos have different signs: {0}s {1}ns",
                                        seconds, nanos);
            return(NodaDuration.FromSeconds(seconds) + NodaDuration.FromNanoseconds(nanos));
        }
        /// <inheritdoc />
        /// <summary>Converts a value. </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>A converted value. If the method returns <see langword="null" />, the valid null value is used.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string str && TimeSpan.TryParse(str, out TimeSpan tSpan))
            {
                return(Duration.FromTimeSpan(tSpan));
            }

            return(default(Duration));
        }
Пример #3
0
        public void Init()
        {
            _campaignSalesAreaList.Add("TCN91");
            _campaignSalesArea           = _campaignSalesAreaList[0];
            _targetStrikeWeightStartDate = new DateTime(2018, 1, 1, 0, 0, 0);
            _targetStrikeWeightEndDate   = new DateTime(2018, 1, 10, 0, 0, 0);
            _recommendation1             = new Mock <Recommendation>();
            _recommendation2             = new Mock <Recommendation>();
            _rec1Rating     = 10;
            _rec2Rating     = 5;
            _rec1Action     = "B";                  //this is a booking and is to be added to the total
            _rec2Action     = "C";                  //this is a cancellation and is to be subtracted from the total
            _rec1Length     = NodaTime.Duration.FromSeconds(15);
            _rec2Length     = NodaTime.Duration.FromSeconds(30);
            _timeslice1From = "00:00";
            _timeslice1To   = "01:00";
            _timeslice2From = "06:00";
            _timeslice2To   = "08:00";
            _recommendationsTotalForDayPart = 0;

            _dayPart = new DayPartModel()
            {
                DesiredPercentageSplit = 0,
                CurrentPercentageSplit = 10,
                Timeslices             = new List <TimesliceModel>()
                {
                    new TimesliceModel()
                    {
                        FromTime = _timeslice1From, ToTime = _timeslice1To, DowPattern = new List <string>()
                        {
                            "Mon", "Tues", "Wed"
                        },
                    },
                    new TimesliceModel()
                    {
                        FromTime = _timeslice2From, ToTime = _timeslice2To, DowPattern = new List <string>()
                        {
                            "Mon", "Tues", "Wed"
                        },
                    }
                }
            };

            _ = _recommendation1.Setup(r => r.SpotRating).Returns(_rec1Rating);
            _ = _recommendation1.Setup(r => r.Action).Returns(_rec1Action);
            _ = _recommendation1.Setup(r => r.StartDateTime).Returns(_targetStrikeWeightStartDate);
            _ = _recommendation1.Setup(r => r.SalesArea).Returns(_campaignSalesArea);

            _ = _recommendation2.Setup(r => r.SpotRating).Returns(_rec2Rating);
            _ = _recommendation2.Setup(r => r.Action).Returns(_rec2Action);
            _ = _recommendation2.Setup(r => r.StartDateTime).Returns(_targetStrikeWeightStartDate);
            _ = _recommendation2.Setup(r => r.SalesArea).Returns(_campaignSalesArea);
        }
        /// <summary>
        /// Converts a Noda Time <see cref="NodaDuration"/> to a Protobuf <see cref="ProtobufDuration"/>.
        /// </summary>
        /// <remarks>
        /// Noda Time has a wider range of valid durations than Protobuf; durations of more than around 10,000
        /// years (positive or negative) cannot be represented.
        /// </remarks>
        /// <param name="duration">The duration to convert. Must not be null.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="duration"/> represents a duration
        /// which is invalid in <see cref="ProtobufDuration"/>.</exception>
        /// <returns>The Protobuf representation.</returns>
        public static ProtobufDuration ToProtobufDuration(this NodaDuration duration)
        {
            if (duration < minProtobufDuration || duration > maxProtobufDuration)
            {
                throw new ArgumentOutOfRangeException(nameof(duration), "Duration is outside the range of valid Protobuf durations.");
            }
            // Deliberately long to keep the later arithmetic in 64-bit.
            long days        = duration.Days;
            long nanoOfDay   = duration.NanosecondOfDay;
            long secondOfDay = nanoOfDay / NodaConstants.NanosecondsPerSecond;
            int  nanos       = duration.SubsecondNanoseconds;

            return(new ProtobufDuration {
                Seconds = days * NodaConstants.SecondsPerDay + secondOfDay, Nanos = nanos
            });
        }