Esempio n. 1
0
        public void TimeBasedArgs_IsInWindow_MissngFromJson()
        {
            string        json = @"
{
  'timeZoneId': 'Eastern Standard Time'
}";
            TimeBasedArgs args = TimeBasedArgs.FromJson(json);

            Assert.True(args.IsInsideWindow(DateTime.Parse("7/22/2019 18:00:00Z")));
        }
Esempio n. 2
0
        public void IsValidDate_0InvalidDates()
        {
            string        json = @"
{
  'timeZoneId': 'Eastern Standard Time',
  'validDaysOfWeek': [ 'Monday' ],
  'invalidDates': []
}";
            TimeBasedArgs args = TimeBasedArgs.FromJson(json);

            Assert.True(args.IsValidDate(DateTime.Parse("7/22/2019 18:00:00Z")));
        }
Esempio n. 3
0
        public void TimeBasedArgs_IsInWindow()
        {
            string        json = @"
{
  'startTimeSpan': '12:30',
  'endTimeSpan': '17:00',
  'timeZoneId': 'Eastern Standard Time'
}";
            TimeBasedArgs args = TimeBasedArgs.FromJson(json);

            Assert.True(args.IsInsideWindow(DateTime.Parse("7/22/2019 18:00:00Z")));
            Assert.False(args.IsInsideWindow(DateTime.Parse("7/22/2019 1:00:00Z")));
        }
Esempio n. 4
0
        public void TimeBasedArgs_IsValidDayOfWeek()
        {
            string        json = @"
{
  'timeZoneId': 'Eastern Standard Time',
  'validDaysOfWeek': [ 'Monday', 'Tuesday' ]
}";
            TimeBasedArgs args = TimeBasedArgs.FromJson(json);

            Assert.False(args.IsValidDayOfWeek(DateTime.Parse("7/21/2019 18:00:00Z")));
            Assert.True(args.IsValidDayOfWeek(DateTime.Parse("7/22/2019 18:00:00Z")));
            Assert.True(args.IsValidDayOfWeek(DateTime.Parse("7/23/2019 18:00:00Z")));
            Assert.False(args.IsValidDayOfWeek(DateTime.Parse("7/24/2019 18:00:00Z")));
        }
Esempio n. 5
0
        public void TimeBasedArgs_FromJson_DefaultValidDays()
        {
            string        json = @"
{
  'startTimeSpan': '12:30',
  'endTimeSpan': '17:00',
  'timeZoneId': 'Eastern Standard Time'
}";
            TimeBasedArgs args = TimeBasedArgs.FromJson(json);

            Assert.Equal(new TimeSpan(12, 30, 0), args.StartTimeSpan);
            Assert.Equal(new TimeSpan(17, 0, 0), args.EndTimeSpan);
            Assert.Equal(TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"), args.GetTimeZoneInfo());
            Assert.Empty(args.ValidDaysOfWeek);
        }
Esempio n. 6
0
        public void IsValidDate()
        {
            string        json = @"
{
  'timeZoneId': 'Eastern Standard Time',
  'validDaysOfWeek': [ 'Monday' ],
  'invalidDates': [
		'7/22/2019', '7/23/2019'
	]
}";
            TimeBasedArgs args = TimeBasedArgs.FromJson(json);

            Assert.Equal(DateTime.Parse("7/22/2019"), args.InvalidDates.First());
            Assert.True(args.IsValidDate(DateTime.Parse("7/24/2019 18:00:00Z")));
            Assert.False(args.IsValidDate(DateTime.Parse("7/22/2019 18:00:00Z")));
        }
Esempio n. 7
0
        public void TimeBasedArgs_FromJson_validDaysOfWeek()
        {
            string        json = @"
{
  'startTimeSpan': '12:30',
  'endTimeSpan': '17:00',
  'timeZoneId': 'Eastern Standard Time',
  'validDaysOfWeek': [ 'Monday', 'Tuesday' ]
}";
            TimeBasedArgs args = TimeBasedArgs.FromJson(json);

            Assert.Equal(new TimeSpan(12, 30, 0), args.StartTimeSpan);
            Assert.Equal(new TimeSpan(17, 0, 0), args.EndTimeSpan);
            Assert.Equal(TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"), args.GetTimeZoneInfo());
            Assert.Equal(2, args.ValidDaysOfWeek.Count());
            Assert.Contains(DayOfWeek.Monday, args.ValidDaysOfWeek);
            Assert.Contains(DayOfWeek.Tuesday, args.ValidDaysOfWeek);
        }
Esempio n. 8
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("DateTimeGates - C# HTTP trigger function processed a request.");

            TimeBasedArgs args;

            try
            {
                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                args = TimeBasedArgs.FromJson(requestBody);
                log.LogInformation(args.ToString());
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult($"Invalid body. Exception {ex}"));
            }

            // Convert UTC time to the user's requested local time
            DateTime localTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, args.GetTimeZoneInfo());

            log.LogVariable("DateTime.UtcNow", DateTime.UtcNow);
            log.LogVariable(nameof(localTime), localTime);

            // Determine if the datetime is valid.
            bool isInsideTimeWindow = args.IsInsideWindow(localTime);
            bool isValidDayOfWeek   = (args.ValidDaysOfWeek.Count() == 0 || args.IsValidDayOfWeek(localTime));
            bool isValidDate        = args.IsValidDate(localTime);

            /// Log data
            log.LogVariable(nameof(isInsideTimeWindow), isInsideTimeWindow);
            log.LogVariable(nameof(isValidDayOfWeek), isValidDayOfWeek);
            log.LogVariable(nameof(isValidDate), isValidDate);

            return(new OkObjectResult(new
            {
                meetsCriteria = isInsideTimeWindow && isValidDayOfWeek && isValidDate,
                isInsideTimeWindow,
                isValidDayOfWeek,
                isValidDate,
            }));
        }