Esempio n. 1
0
        public void WeeklyTime_Ctor()
        {
            var weekly_time = new WeeklyTime(DayOfWeek.Monday, 12, 2, 3);

            Assert.Equal(DayOfWeek.Monday, weekly_time.DayOfWeek);
            Assert.Equal(12, weekly_time.Hour);
            Assert.Equal(2, weekly_time.Minute);
            Assert.Equal(3, weekly_time.Second);
        }
Esempio n. 2
0
        public void WeeklyTime_AsTuple()
        {
            var weekly_time = new WeeklyTime(DayOfWeek.Monday, 12, 2, 3);
            var tuple       = weekly_time.AsTuple();

            Assert.Equal(weekly_time.DayOfWeek, tuple.Item1);
            Assert.Equal(weekly_time.Hour, tuple.Item2);
            Assert.Equal(weekly_time.Minute, tuple.Item3);
            Assert.Equal(weekly_time.Second, tuple.Item4);
        }
Esempio n. 3
0
        public void WeeklyTime_CompareTo()
        {
            var zoo = new WeeklyTime(DayOfWeek.Monday, 0, 0, 0);
            var foo = new WeeklyTime(DayOfWeek.Monday, 1, 1, 1);
            var moo = new WeeklyTime(DayOfWeek.Monday, 1, 1, 1);
            var bar = new WeeklyTime(DayOfWeek.Monday, 2, 2, 2);

            Assert.True(foo.CompareTo(bar) == -1);
            Assert.True(foo.CompareTo(moo) == 0);
            Assert.True(foo.CompareTo(zoo) == 1);
        }
Esempio n. 4
0
        public void WeeklyTime_Equals()
        {
            var foo    = new WeeklyTime(DayOfWeek.Monday, 12, 12, 12);
            var foobar = new WeeklyTime(DayOfWeek.Monday, 12, 12, 12);
            var bar    = new WeeklyTime(DayOfWeek.Monday, 12, 5, 5);
            var baz    = new WeeklyTime(DayOfWeek.Tuesday, 12, 12, 12);

            Assert.True(foo.Equals(foobar));
            Assert.False(foo.Equals(bar));
            Assert.False(foo.Equals(baz));
        }
Esempio n. 5
0
        public void WeeklyTime_HashCode()
        {
            var foo    = new WeeklyTime(DayOfWeek.Monday, 12, 12, 12);
            var foobar = new WeeklyTime(DayOfWeek.Monday, 12, 12, 12);
            var bar    = new WeeklyTime(DayOfWeek.Monday, 5, 5, 5);

            var set = new HashSet <WeeklyTime>();

            set.Add(foo);
            Assert.True(set.Contains(foobar));
            Assert.False(set.Contains(bar));
        }
Esempio n. 6
0
        public void WeeklyTime_InvalidCtor()
        {
            var did_throw = false;

            try
            {
                var weekly_time = new WeeklyTime(DayOfWeek.Monday, 64, 2, 3);
            }
            catch (ArgumentOutOfRangeException)
            {
                did_throw = true;
            }

            Assert.True(did_throw);
        }
Esempio n. 7
0
        /// <summary>
        /// Returns the duration to the next day of week and time of day (UTC).
        /// </summary>
        /// <param name="weekly">The weekly time.</param>
        /// <param name="now">The current time instant.</param>
        /// <returns>The next date time (UTC).</returns>
        public static ZonedDateTime GetNextUtc(WeeklyTime weekly, [CanBeDefault] Instant now)
        {
            var localNow  = now.InZone(DateTimeZone.Utc).LocalDateTime;
            var localNext = localNow
                            .Date.With(DateAdjusters.NextOrSame(weekly.DayOfWeek))
                            .At(weekly.Time);

            // Handle "we're already on the right day-of-week, but later in the day"
            if (localNext <= localNow)
            {
                localNext = localNext.PlusWeeks(1);
            }

            return(localNext.InUtc());
        }
Esempio n. 8
0
        public void WeeklyTime_CompareToInvalid()
        {
            var did_throw = false;
            var zoo       = new WeeklyTime(DayOfWeek.Monday, 0, 0, 0);
            var boo       = new object();

            try
            {
                zoo.CompareTo(boo);
            }
            catch (ArgumentException)
            {
                did_throw = true;
            }

            Assert.True(did_throw);
        }
Esempio n. 9
0
        /// <summary>
        /// Returns a value indicating whether the given instant now is outside the given weekly interval.
        /// </summary>
        /// <param name="start">The start of the weekly interval.</param>
        /// <param name="end">The end of the weekly interval.</param>
        /// <param name="now">The current time instant.</param>
        /// <returns>True if now is outside the given interval, else false.</returns>
        public static bool IsOutsideInterval(WeeklyTime start, WeeklyTime end, [CanBeDefault] Instant now)
        {
            Debug.NotDefault(start.DayOfWeek, nameof(start.DayOfWeek));
            Debug.NotDefault(end.DayOfWeek, nameof(end.DayOfWeek));
            Debug.True(start.DayOfWeek <= end.DayOfWeek, nameof(start.DayOfWeek));

            var localNow = now.InZone(DateTimeZone.Utc).LocalDateTime;

            var localStart = localNow
                             .Date.With(DateAdjusters.NextOrSame(start.DayOfWeek))
                             .At(start.Time);

            var localEnd = localNow
                           .Date.With(DateAdjusters.NextOrSame(end.DayOfWeek))
                           .At(end.Time);

            return(localNow < localStart || localNow > localEnd);
        }
Esempio n. 10
0
        internal void IsOutSideInterval_WithVariousDateTimes_ReturnsExpectedResult(
            int year,
            int month,
            int day,
            int hour,
            int minute,
            bool expected)
        {
            // Arrange
            var localUtc = new LocalDateTime(year, month, day, hour, minute);
            var utcNow   = new ZonedDateTime(localUtc, DateTimeZone.Utc, Offset.Zero);

            // Act
            var start  = new WeeklyTime(IsoDayOfWeek.Saturday, new LocalTime(20, 00));
            var end    = new WeeklyTime(IsoDayOfWeek.Sunday, new LocalTime(21, 00));
            var result = TimingProvider.IsOutsideInterval(start, end, utcNow.ToInstant());

            // Assert
            Assert.Equal(expected, result);
        }
Esempio n. 11
0
        /// <summary>
        /// Returns the datetime for the weekly day and time this week.
        /// </summary>
        /// <param name="weekly">The weekly day and time.</param>
        /// <param name="now">The current time instant.</param>
        /// <returns>The <see cref="ZonedDateTime"/> for this week.</returns>
        public static ZonedDateTime ThisWeek(WeeklyTime weekly, [CanBeDefault] Instant now)
        {
            Debug.NotDefault(weekly.DayOfWeek, nameof(weekly.DayOfWeek));

            var localNow = now.InZone(DateTimeZone.Utc).LocalDateTime;

            var midnightToday = localNow.Date.AtMidnight();
            var difference    = Period.FromDays(weekly.DayOfWeek - localNow.DayOfWeek);

            var weeklyMidnight = midnightToday + difference;
            var weeklyDateTime = new LocalDateTime(
                weeklyMidnight.Year,
                weeklyMidnight.Month,
                weeklyMidnight.Day,
                weekly.Time.Hour,
                weekly.Time.Minute,
                weekly.Time.Second,
                weekly.Time.Millisecond);

            return(new ZonedDateTime(weeklyDateTime, DateTimeZone.Utc, Offset.Zero));
        }
Esempio n. 12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FixConfiguration"/> class.
        /// </summary>
        /// <param name="broker">The FIX brokerage name.</param>
        /// <param name="accountType">The FIX account type.</param>
        /// <param name="accountCurrency">The FIX account currency.</param>
        /// <param name="configPath">The FIX configuration file path.</param>
        /// <param name="credentials">The FIX credentials.</param>
        /// <param name="sendAccountTag">The option flag to send account tags with messages.</param>
        /// <param name="connectWeeklyTime">The time to connect FIX sessions.</param>
        /// <param name="disconnectWeeklyTime">The time to disconnect FIX sessions.</param>
        public FixConfiguration(
            Brokerage broker,
            AccountType accountType,
            Currency accountCurrency,
            string configPath,
            FixCredentials credentials,
            bool sendAccountTag,
            WeeklyTime connectWeeklyTime,
            WeeklyTime disconnectWeeklyTime)
        {
            Condition.NotEmptyOrWhiteSpace(configPath, nameof(configPath));

            this.AccountId            = new AccountId(broker, credentials.AccountNumber, accountType);
            this.Broker               = broker;
            this.AccountType          = accountType;
            this.AccountCurrency      = accountCurrency;
            this.ConfigPath           = configPath;
            this.Credentials          = credentials;
            this.SendAccountTag       = sendAccountTag;
            this.ConnectWeeklyTime    = connectWeeklyTime;
            this.DisconnectWeeklyTime = disconnectWeeklyTime;
        }
Esempio n. 13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NautilusServiceBase"/> class.
        /// </summary>
        /// <param name="container">The componentry container.</param>
        /// <param name="messagingAdapter">The messaging adapter.</param>
        /// <param name="config">The service configuration.</param>
        /// <exception cref="ArgumentException">If the addresses is empty.</exception>
        protected NautilusServiceBase(
            IComponentryContainer container,
            MessageBusAdapter messagingAdapter,
            FixConfiguration config)
            : base(container, messagingAdapter)
        {
            this.messaging            = messagingAdapter;
            this.connectWeeklyTime    = config.ConnectWeeklyTime;
            this.disconnectWeeklyTime = config.DisconnectWeeklyTime;
            this.maintainConnection   = false;

            // Commands
            this.RegisterHandler <ConnectSession>(this.OnMessage);
            this.RegisterHandler <DisconnectSession>(this.OnMessage);

            // Events
            this.RegisterHandler <SessionConnected>(this.OnMessage);
            this.RegisterHandler <SessionDisconnected>(this.OnMessage);

            // Event Subscriptions
            this.Subscribe <SessionConnected>();
            this.Subscribe <SessionDisconnected>();
        }
Esempio n. 14
0
        /// <summary>
        /// This method returns the next RouteInstance to depart from the given the date time.
        /// 
        /// If the Route has no deliveryTimes, the method will return null.  Otherwise, it will return a RouteInstance with
        /// the DateTime of the next delivery time.
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public RouteInstance GetNextDeparture(DateTime time)
        {
            if (departureTimes.Count == 0) {
                return null;
            }

            var asWeeklyTime = new WeeklyTime(time);

            // find next departure time
            WeeklyTime nextDeparture = null;
            foreach (WeeklyTime departureTime in departureTimes)
            {

                var value = asWeeklyTime.CompareTo(departureTime);
                if ( value <= 0)
                {
                    nextDeparture = departureTime;
                    break;
                }
            }

            int extraMinutes = 0; // extra time to add if the departureTime is next week.

            // if no deliveries this week, get first one next week.
            if (nextDeparture == null) {
                nextDeparture = departureTimes[0];
                extraMinutes = WeeklyTime.MINUTES_IN_A_WEEK;
            }

            // work out datetime
            int minutesDifference = (int)(nextDeparture.Value.TotalMinutes - asWeeklyTime.Value.TotalMinutes) + extraMinutes;
            var departureDateTime = time.AddMinutes(minutesDifference);

            // create and return route instance
            return new RouteInstance(this, departureDateTime);
        }
Esempio n. 15
0
 /// <summary>
 /// Removes the given weeklyTime if it is contained.
 /// </summary>
 /// <param name="weeklyTime"></param>
 public void RemoveDepartureTime(WeeklyTime weeklyTime)
 {
     this.departureTimes.Remove(weeklyTime);
 }
        /// <summary>
        /// Returns the departure_time_id of the given combination.
        /// 
        /// Returns 0 if it doesn't exist.
        /// </summary>
        /// <param name="route_id"></param>
        /// <param name="departureTime"></param>
        /// <returns></returns>
        private int GetId(int route_id, WeeklyTime departureTime)
        {
            // check arguments
            if (route_id <= 0)
                throw new ArgumentException("Route_id cannot be less than or equal to zero.", "route_id");

            if (departureTime == null)
                throw new ArgumentException("departureTime cannot be null", "departureTime");

            // get the id
            int id = 0;
            lock (Database.Instance)
            {
                var sql = string.Format("SELECT {1} FROM `{0}` WHERE active=1 AND route_id='{2}' AND weekly_time='{3}'", TABLE_NAME, ID_COL_NAME, route_id, departureTime.Value.Ticks.ToString());
                var row = Database.Instance.FetchRow(sql);
                if (row.Length != 0)
                    id =  row[0].ToInt();
            }

            // return
            return id;
        }
Esempio n. 17
0
        public void AddDepartureTime(WeeklyTime weeklyTime)
        {
            // validation
            if (this.departureTimes.Contains(weeklyTime))
                throw new IllegalActionException("That time is already part of the route.");

            this.departureTimes.Add(weeklyTime);
            this.departureTimes.Sort();
        }