/// <summary> /// Get the list of next available, free slots. /// </summary> /// <param name="numberOfSlots">Number of next free slot to return. Defaults to 3.</param> /// <param name="cancellationToken" >(Optional) A <see cref="CancellationToken"/> that can be used by other objects /// or threads to receive notice of cancellation.</param> /// <returns>A list of free slots.</returns> internal async Task <IEnumerable <string> > GetNextFreeSlotsAsync(int numberOfSlots = 3, CancellationToken cancellationToken = default(CancellationToken)) { var notAvailable = await GetAsync <NotAvailableDatesAndTimes>("/api/reservations/notavailabledates", cancellationToken); var freeslots = new List <string>(); var dateIterator = DateTime.Today; while (freeslots.Count < numberOfSlots && dateIterator < DateTime.Today.AddYears(1)) { foreach (var slot in Constants.Slots) { var slotStartTime = new DateTime(dateIterator.Year, dateIterator.Month, dateIterator.Day, slot.StartTime, 0, 0); var slotEndTime = new DateTime(dateIterator.Year, dateIterator.Month, dateIterator.Day, slot.EndTime, 0, 0); if (!notAvailable.Times.Contains(slotStartTime) && freeslots.Count <= numberOfSlots && slotStartTime >= DateTime.Now) { var timex = TimexProperty.FromDateTime(slotStartTime); freeslots.Add($"{timex.ToNaturalLanguage(DateTime.Now)}-{slotEndTime.ToString("htt")}"); } } dateIterator = dateIterator.AddDays(1); } return(freeslots); }
/// <summary> /// Converts the DateTime object to natural languge with the given reference point. /// </summary> /// <param name="dateTime">the DateTime object.</param> /// <param name="referenceDate">(Optional) Reference point. Defaults to DateTime.Now.</param> /// <returns> /// Natural languge string of the DateTime. /// </returns> public static string ToNaturalLanguage(this DateTime dateTime, DateTime?referenceDate = null) { if (referenceDate == null) { referenceDate = DateTime.Now; } var timex = TimexProperty.FromDateTime(dateTime); return(timex.ToNaturalLanguage(referenceDate.Value)); }
public void DataTypes_Timex_FromDateTime() { Assert.AreEqual("2017-12-05T23:57:35", TimexProperty.FromDateTime(new System.DateTime(2017, 12, 5, 23, 57, 35)).TimexValue); }
private async Task <DialogTurnResult> RecommendSlotsStepAsync(WaterfallStepContext step, CancellationToken cancellationToken) { var state = await _stateAccessor.GetAsync(step.Context, cancellationToken : cancellationToken); if (state.Services.Count == 0) { state.Services = ParseServiceSelectionCardResponse(step.Context.Activity); await _stateAccessor.SetAsync(step.Context, state, cancellationToken); } // Check whether we already know something about the date. if (state.StartDate != null) { return(await step.NextAsync(cancellationToken : cancellationToken)); } var recommendedSlots = new List <DateTime>(); try { var api = new CarwashService(step, cancellationToken); var notAvailable = await api.GetNotAvailableDatesAndTimesAsync(cancellationToken); recommendedSlots = GetRecommendedSlots(notAvailable); } catch (AuthenticationException) { await step.Context.SendActivityAsync(AuthDialog.NotAuthenticatedMessage, cancellationToken : cancellationToken); return(await step.ClearStateAndEndDialogAsync(_stateAccessor, cancellationToken : cancellationToken)); } catch (Exception e) { _telemetryClient.TrackException(e); await step.Context.SendActivityAsync(e.Message, cancellationToken : cancellationToken); return(await step.ClearStateAndEndDialogAsync(_stateAccessor, cancellationToken : cancellationToken)); } // Save recommendations to state state.RecommendedSlots = recommendedSlots; await _stateAccessor.SetAsync(step.Context, state, cancellationToken); var choices = new List <Choice>(); foreach (var slot in recommendedSlots) { var timex = TimexProperty.FromDateTime(slot); choices.Add(new Choice(timex.ToNaturalLanguage(DateTime.Now))); } return(await step.PromptAsync( RecommendedSlotsPromptName, new PromptOptions { Prompt = MessageFactory.Text("Can I recommend you one of these slots? If you want to choose something else, just type skip."), Choices = choices, }, cancellationToken)); }