Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
[Calendar] fix merge issue (#2370)
Browse files Browse the repository at this point in the history
* merge timezone issue fix from master

* fix error
  • Loading branch information
KayMKM authored and lauren-mills committed Sep 14, 2019
1 parent 34877f7 commit 3ffe738
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ protected List<EventModel> GetFilteredEvents(CalendarSkillState state, string us
showingCardTitle = null;

// filter meetings with start time
var timeResult = RecognizeDateTime(userInput, locale, false);
var timeResult = RecognizeDateTime(userInput, locale, state.GetUserTimeZone(), false);
if (filteredMeetingList.Count <= 0 && timeResult != null)
{
foreach (var result in timeResult)
Expand Down Expand Up @@ -767,33 +767,6 @@ protected async Task<string> GetUserPhotoUrlAsync(ITurnContext context, EventMod
return string.Format(AdaptiveCardHelper.DefaultAvatarIconPathFormat, displayName);
}

protected bool IsRelativeTime(string userInput, string resolverResult, string timex)
{
var userInputLower = userInput.ToLower();
if (userInputLower.Contains(CalendarCommonStrings.Ago) ||
userInputLower.Contains(CalendarCommonStrings.Before) ||
userInputLower.Contains(CalendarCommonStrings.Later) ||
userInputLower.Contains(CalendarCommonStrings.Next))
{
return true;
}

if (userInputLower.Contains(CalendarCommonStrings.TodayLower) ||
userInputLower.Contains(CalendarCommonStrings.Now) ||
userInputLower.Contains(CalendarCommonStrings.YesterdayLower) ||
userInputLower.Contains(CalendarCommonStrings.TomorrowLower))
{
return true;
}

if (timex == "PRESENT_REF")
{
return true;
}

return false;
}

protected async Task DigestCalendarLuisResult(DialogContext dc, CalendarLuis luisResult, General generalLuisResult, bool isBeginDialog)
{
try
Expand Down Expand Up @@ -901,7 +874,7 @@ protected async Task DigestCalendarLuisResult(DialogContext dc, CalendarLuis lui

if (entity.Duration != null)
{
var duration = GetDurationFromEntity(entity, dc.Context.Activity.Locale);
var duration = GetDurationFromEntity(entity, dc.Context.Activity.Locale, state.GetUserTimeZone());
if (duration != -1)
{
state.MeetingInfor.CreateHasDetail = true;
Expand Down Expand Up @@ -1047,12 +1020,12 @@ protected async Task DigestCalendarLuisResult(DialogContext dc, CalendarLuis lui

if (entity.MoveEarlierTimeSpan != null)
{
state.UpdateMeetingInfor.MoveTimeSpan = GetMoveTimeSpanFromEntity(entity.MoveEarlierTimeSpan[0], dc.Context.Activity.Locale, false);
state.UpdateMeetingInfor.MoveTimeSpan = GetMoveTimeSpanFromEntity(entity.MoveEarlierTimeSpan[0], dc.Context.Activity.Locale, false, state.GetUserTimeZone());
}

if (entity.MoveLaterTimeSpan != null)
{
state.UpdateMeetingInfor.MoveTimeSpan = GetMoveTimeSpanFromEntity(entity.MoveLaterTimeSpan[0], dc.Context.Activity.Locale, true);
state.UpdateMeetingInfor.MoveTimeSpan = GetMoveTimeSpanFromEntity(entity.MoveLaterTimeSpan[0], dc.Context.Activity.Locale, true, state.GetUserTimeZone());
}

if (entity.datetime != null)
Expand Down Expand Up @@ -1159,9 +1132,10 @@ protected async Task DigestCalendarLuisResult(DialogContext dc, CalendarLuis lui
}
}

protected List<DateTimeResolution> RecognizeDateTime(string dateTimeString, string culture, bool convertToDate = true)
protected List<DateTimeResolution> RecognizeDateTime(string dateTimeString, string culture, TimeZoneInfo userTimeZone, bool convertToDate = true)
{
var results = DateTimeRecognizer.RecognizeDateTime(DateTimeHelper.ConvertNumberToDateTimeString(dateTimeString, convertToDate), culture, options: DateTimeOptions.CalendarMode);
var userNow = TimeConverter.ConvertUtcToUserTime(DateTime.UtcNow, userTimeZone);
var results = DateTimeRecognizer.RecognizeDateTime(DateTimeHelper.ConvertNumberToDateTimeString(dateTimeString, convertToDate), culture, DateTimeOptions.CalendarMode, userNow);

if (results.Count > 0)
{
Expand Down Expand Up @@ -1529,10 +1503,10 @@ private async Task<List<Card>> GetMeetingCardListAsync(CalendarSkillState state,
return eventItemList;
}

private int GetDurationFromEntity(CalendarLuis._Entities entity, string local)
private int GetDurationFromEntity(CalendarLuis._Entities entity, string local, TimeZoneInfo userTimeZone)
{
var culture = local ?? English;
var result = RecognizeDateTime(entity.Duration[0], culture);
var result = RecognizeDateTime(entity.Duration[0], culture, userTimeZone);
if (result != null)
{
if (result[0].Value != null)
Expand All @@ -1544,10 +1518,10 @@ private int GetDurationFromEntity(CalendarLuis._Entities entity, string local)
return -1;
}

private int GetMoveTimeSpanFromEntity(string timeSpan, string local, bool later)
private int GetMoveTimeSpanFromEntity(string timeSpan, string local, bool later, TimeZoneInfo userTimeZone)
{
var culture = local ?? English;
var result = RecognizeDateTime(timeSpan, culture);
var result = RecognizeDateTime(timeSpan, culture, userTimeZone);
if (result != null)
{
if (result[0].Value != null)
Expand Down Expand Up @@ -1585,7 +1559,7 @@ private List<DateTime> GetDateFromDateTimeString(string date, string local, Time
{
// if isTargetTimeRange is true, will only parse the time range
var culture = local ?? English;
var results = RecognizeDateTime(date, culture, true);
var results = RecognizeDateTime(date, culture, userTimeZone, true);
var dateTimeResults = new List<DateTime>();
if (results != null)
{
Expand All @@ -1603,8 +1577,7 @@ private List<DateTime> GetDateFromDateTimeString(string date, string local, Time

if (dateTime != null)
{
var isRelativeTime = IsRelativeTime(date, result.Value, result.Timex);
dateTimeResults.Add(isRelativeTime ? TimeZoneInfo.ConvertTime(dateTime, TimeZoneInfo.Local, userTimeZone) : dateTime);
dateTimeResults.Add(dateTime);
}
}
else
Expand All @@ -1630,7 +1603,7 @@ private List<DateTime> GetTimeFromDateTimeString(string time, string local, Time
{
// if isTargetTimeRange is true, will only parse the time range
var culture = local ?? English;
var results = RecognizeDateTime(time, culture, false);
var results = RecognizeDateTime(time, culture, userTimeZone, false);
var dateTimeResults = new List<DateTime>();
if (results != null)
{
Expand All @@ -1648,8 +1621,7 @@ private List<DateTime> GetTimeFromDateTimeString(string time, string local, Time

if (dateTime != null)
{
var isRelativeTime = IsRelativeTime(time, result.Value, result.Timex);
dateTimeResults.Add(isRelativeTime ? TimeZoneInfo.ConvertTime(dateTime, TimeZoneInfo.Local, userTimeZone) : dateTime);
dateTimeResults.Add(dateTime);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
using CalendarSkill.Models.DialogOptions;
using CalendarSkill.Options;
using CalendarSkill.Prompts;
using CalendarSkill.Prompts.Options;
using CalendarSkill.Responses.CreateEvent;
using CalendarSkill.Responses.Shared;
using CalendarSkill.Services;
using CalendarSkill.Utilities;
using Google.Apis.People.v1.Data;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Skills;
Expand Down Expand Up @@ -670,10 +672,11 @@ public CreateEventDialog(
return await sc.NextAsync(cancellationToken: cancellationToken);
}

return await sc.PromptAsync(Actions.DatePromptForCreate, new PromptOptions
return await sc.PromptAsync(Actions.DatePromptForCreate, new DatePromptOptions
{
Prompt = ResponseManager.GetResponse(CreateEventResponses.NoStartDate),
RetryPrompt = ResponseManager.GetResponse(CreateEventResponses.NoStartDateRetry),
TimeZone = state.GetUserTimeZone()
}, cancellationToken);
}
catch (Exception ex)
Expand Down Expand Up @@ -718,25 +721,12 @@ public CreateEventDialog(

if (dateTime != null)
{
var isRelativeTime = IsRelativeTime(sc.Context.Activity.Text, dateTimeValue, dateTimeConvertType);
if (CalendarCommonUtil.ContainsTime(dateTimeConvertType))
{
state.MeetingInfor.StartTime.Add(TimeZoneInfo.ConvertTime(dateTime, TimeZoneInfo.Local, state.GetUserTimeZone()));
state.MeetingInfor.StartTime.Add(dateTime);
}

// Workaround as DateTimePrompt only return as local time
if (isRelativeTime)
{
dateTime = new DateTime(
dateTime.Year,
dateTime.Month,
dateTime.Day,
DateTime.Now.Hour,
DateTime.Now.Minute,
DateTime.Now.Second);
}

state.MeetingInfor.StartDate.Add(isRelativeTime ? TimeZoneInfo.ConvertTime(dateTime, TimeZoneInfo.Local, state.GetUserTimeZone()) : dateTime);
state.MeetingInfor.StartDate.Add(dateTime);
}
}
catch (FormatException ex)
Expand Down Expand Up @@ -764,11 +754,12 @@ public CreateEventDialog(
var state = await Accessor.GetAsync(sc.Context, cancellationToken: cancellationToken);
if (!state.MeetingInfor.StartTime.Any())
{
return await sc.PromptAsync(Actions.TimePromptForCreate, new NoSkipPromptOptions
return await sc.PromptAsync(Actions.TimePromptForCreate, new TimePromptOptions
{
Prompt = ResponseManager.GetResponse(CreateEventResponses.NoStartTime),
RetryPrompt = ResponseManager.GetResponse(CreateEventResponses.NoStartTimeRetry),
NoSkipPrompt = ResponseManager.GetResponse(CreateEventResponses.NoStartTimeNoSkip),
TimeZone = state.GetUserTimeZone()
}, cancellationToken);
}
else
Expand Down Expand Up @@ -803,8 +794,7 @@ public CreateEventDialog(

if (dateTime != null)
{
var isRelativeTime = IsRelativeTime(sc.Context.Activity.Text, dateTimeValue, dateTimeConvertType);
state.MeetingInfor.StartTime.Add(isRelativeTime ? TimeZoneInfo.ConvertTime(dateTime, TimeZoneInfo.Local, state.GetUserTimeZone()) : dateTime);
state.MeetingInfor.StartTime.Add(dateTime);
}
}
catch (FormatException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Threading.Tasks;
using CalendarSkill.Models;
using CalendarSkill.Models.DialogOptions;
using CalendarSkill.Options;
using CalendarSkill.Prompts;
using CalendarSkill.Prompts.Options;
using CalendarSkill.Responses.Shared;
using CalendarSkill.Responses.UpdateEvent;
Expand Down Expand Up @@ -218,10 +220,11 @@ public UpdateEventDialog(
return await sc.NextAsync();
}

return await sc.PromptAsync(Actions.TimePrompt, new PromptOptions
return await sc.PromptAsync(Actions.TimePrompt, new TimePromptOptions
{
Prompt = ResponseManager.GetResponse(UpdateEventResponses.NoNewTime),
RetryPrompt = ResponseManager.GetResponse(UpdateEventResponses.NoNewTimeRetry)
RetryPrompt = ResponseManager.GetResponse(UpdateEventResponses.NoNewTimeRetry),
TimeZone = state.GetUserTimeZone()
}, cancellationToken);
}
catch (Exception ex)
Expand Down Expand Up @@ -304,13 +307,6 @@ public UpdateEventDialog(
continue;
}

var isRelativeTime = IsRelativeTime(sc.Context.Activity.Text, resolution.Value, dateTimeConvertTypeString);
if (isRelativeTime)
{
dateTimeValue = DateTime.SpecifyKind(dateTimeValue, DateTimeKind.Local);
}

dateTimeValue = isRelativeTime ? TimeZoneInfo.ConvertTime(dateTimeValue, TimeZoneInfo.Local, state.GetUserTimeZone()) : dateTimeValue;
var originalStartDateTime = TimeConverter.ConvertUtcToUserTime(state.ShowMeetingInfor.FocusedEvents[0].StartTime, state.GetUserTimeZone());
if (dateTimeConvertType.Types.Contains(Constants.TimexTypes.Date) && !dateTimeConvertType.Types.Contains(Constants.TimexTypes.DateTime))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public DatePrompt(string dialogId, PromptValidator<IList<DateTimeResolution>> va

if (!(options is DatePromptOptions))
{
throw new Exception(nameof(options) + " should be GetEventOptions");
throw new Exception(nameof(options) + " should be DatePromptOptions");
}

if (isRetry && options.RetryPrompt != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public GetEventPrompt(string dialogId, PromptValidator<IList<EventModel>> valida
{
var message = turnContext.Activity.AsMessageActivity();
var culture = turnContext.Activity.Locale ?? DefaultLocale ?? English;
var date = GetTimeFromMessage(message.Text, culture);
var date = GetTimeFromMessage(message.Text, culture, userTimeZone);
if (date.Count > 0)
{
// input is a time
Expand Down Expand Up @@ -172,14 +172,14 @@ private async Task<IList<EventModel>> GetEventsWithTitle(string title)
return events;
}

private IList<DateTimeResolution> GetTimeFromMessage(string message, string culture)
private IList<DateTimeResolution> GetTimeFromMessage(string message, string culture, TimeZoneInfo userTimeZone)
{
IList<DateTimeResolution> results = RecognizeDateTime(message, culture);
IList<DateTimeResolution> results = RecognizeDateTime(message, culture, userTimeZone);

return results;
}

private List<DateTimeResolution> RecognizeDateTime(string dateTimeString, string culture)
private List<DateTimeResolution> RecognizeDateTime(string dateTimeString, string culture, TimeZoneInfo userTimeZone)
{
var userNow = TimeConverter.ConvertUtcToUserTime(DateTime.UtcNow, userTimeZone);
var results = DateTimeRecognizer.RecognizeDateTime(dateTimeString, culture, DateTimeOptions.CalendarMode, userNow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public TimePrompt(string dialogId, PromptValidator<IList<DateTimeResolution>> va

if (!(options is TimePromptOptions))
{
throw new Exception(nameof(options) + " should be GetEventOptions");
throw new Exception(nameof(options) + " should be TimePromptOptions");
}

if (isRetry && options.RetryPrompt != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CalendarSkill.Models;
using CalendarSkill.Prompts;
using CalendarSkill.Prompts.Options;
using CalendarSkillTest.Flow.Fakes;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Adapters;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Microsoft.Recognizers.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Bot.Builder.Dialogs.Tests
namespace CalendarSkillTest.Prompt
{
[TestClass]
public class DatePromptTests
Expand Down

0 comments on commit 3ffe738

Please sign in to comment.