public ActionResult TestUiDateTimeModelAsParameter(UiDateTimeModel modelOne, UiDateTimeModel modelTwo) { return(Json(new { modelOneUtcDate = modelOne.DateTimeUtcValue.GetValueOrDefault().ToString("s"), modelTwoUtcDate = modelTwo.DateTimeUtcValue.GetValueOrDefault().ToString("s") })); }
public ActionResult TestUiDateTimeModelAsParameterWithGet(UiDateTimeModel modelOne, UiDateTimeModel modelTwo, string anotherProperty) { return(Json(new { modelOneUtcDate = modelOne.DateTimeUtcValue.GetValueOrDefault().ToString("s"), modelTwoUtcDate = modelTwo.DateTimeUtcValue.GetValueOrDefault().ToString("s"), Other = anotherProperty }, JsonRequestBehavior.AllowGet)); }
public void TestUiDateTimeModelWithLocalTimeOnlyPasses() { var timeZoneId = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").StandardName; var testUiDateTimeModel = new UiDateTimeModel(timeZoneId) { LocalTime = "9:00:00 PM" }; Assert.AreEqual(DateTime.Parse("01/01/1753 9:00:00 PM"), testUiDateTimeModel.DateTimeLocalValue); Assert.AreEqual(DateTime.Parse("01/01/1753 9:00:00 PM").ToUniversalTime(timeZoneId), testUiDateTimeModel.DateTimeUtcValue); }
public void TestUiDateTimeModelWithLocalDateAndLocalTimeSetPasses() { var timeZoneId = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").StandardName; var testUiDateTimeModel = new UiDateTimeModel(timeZoneId) { LocalDate = "02/28/2013", LocalTime = "11:00:00 PM" }; Assert.AreEqual(DateTime.Parse("02/28/2013 11:00:00 PM"), testUiDateTimeModel.DateTimeLocalValue); Assert.AreEqual(DateTime.Parse("02/28/2013 11:00:00 PM").ToUniversalTime(timeZoneId), testUiDateTimeModel.DateTimeUtcValue); }
public void TestUiDateTimeModelWithUtcDateTimeSetPasses() { var timeZoneId = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").StandardName; var dateTimeUtcValue = DateTime.Parse("02/28/2013 11:00:00 PM"); var testUiDateTimeModel = new UiDateTimeModel(timeZoneId) { DateTimeUtcValue = dateTimeUtcValue }; Assert.AreEqual(dateTimeUtcValue.ToLocalTime(timeZoneId), testUiDateTimeModel.DateTimeLocalValue); Assert.AreEqual(dateTimeUtcValue, testUiDateTimeModel.DateTimeUtcValue); }
//public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { //Used to determine if values where intentionally set by the application //or as a result of model binding to keep values in sync StartImplicitlySet = false; //Use Reflection to get the associated property names from the UiDateTimeRangeModel var startDateTime = StaticReflection.GetMemberName <UiDateTimeRangeModel>(x => x.StartDateTime); var startDateTimeLocalDate = StaticReflection.GetMemberName <UiDateTimeRangeModel>(x => x.StartDateTime.LocalDate); var startDateTimeLocalTime = StaticReflection.GetMemberName <UiDateTimeRangeModel>(x => x.StartDateTime.LocalTime); var startDateTimeLocalDateTime = StaticReflection.GetMemberName <UiDateTimeRangeModel>(x => x.StartDateTime.DateTimeLocalValue); var startDateTimeNoSetTime = StaticReflection.GetMemberName <UiDateTimeRangeModel>(x => x.StartDateTime.NoSetTime); var endDateTime = StaticReflection.GetMemberName <UiDateTimeRangeModel>(x => x.EndDateTime); var endDateTimeLocalDate = StaticReflection.GetMemberName <UiDateTimeRangeModel>(x => x.StartDateTime.LocalDate); var endDateTimeLocalTime = StaticReflection.GetMemberName <UiDateTimeRangeModel>(x => x.StartDateTime.LocalTime); var endDateTimeLocalDateTime = StaticReflection.GetMemberName <UiDateTimeRangeModel>(x => x.StartDateTime.DateTimeLocalValue); //Get the model values for binding TimeZoneName = getValues(bindingContext, StaticReflection.GetMemberName <UiDateTimeRangeModel>(x => x.TimeZoneName)); StartLocalDate = getValues(bindingContext, startDateTime + "." + startDateTimeLocalDate); StartLocalTime = getValues(bindingContext, startDateTime + "." + startDateTimeLocalTime); StartLocalDateTime = getValues(bindingContext, startDateTime + "." + startDateTimeLocalDateTime); StartNoSetTime = getValues(bindingContext, startDateTime + "." + startDateTimeNoSetTime); var noSetTime = false; if (!string.IsNullOrWhiteSpace(StartNoSetTime)) { if (bool.TryParse(StartNoSetTime.Split(',')[0], out noSetTime) && noSetTime) { StartLocalTime = SqlDateTime.MinValue.Value.Date.ToShortTimeString(); StartImplicitlySet = true; } } //************************************************************* //Setup the StartDateTime property of the UiDateTimeRangeModel if (!String.IsNullOrEmpty(StartLocalDateTime)) { StartDateTime = new UiDateTimeModel(TimeZoneName) { DateTimeLocalValue = DateTime.Parse(StartLocalDateTime), NoSetTime = noSetTime, ImplicitlySet = StartImplicitlySet }; } else { //Both StartLocalDate and StartLocalTime so we will set the StartDateTime.DateTimeLocalValue to null if (String.IsNullOrEmpty(StartLocalDate) && String.IsNullOrEmpty(StartLocalTime)) { StartDateTime = new UiDateTimeModel(TimeZoneName) { DateTimeLocalValue = null, NoSetTime = noSetTime, ImplicitlySet = StartImplicitlySet }; } else { if (String.IsNullOrEmpty(StartLocalDate)) { StartImplicitlySet = true; StartLocalDate = SqlDateTime.MinValue.Value.Date.ToShortDateString(); } if (!noSetTime && String.IsNullOrEmpty(StartLocalTime)) { StartImplicitlySet = true; } StartDateTime = new UiDateTimeModel(TimeZoneName) { DateTimeLocalValue = DateTime.Parse(StartLocalDate + " " + StartLocalTime), NoSetTime = noSetTime, ImplicitlySet = StartImplicitlySet }; } } EndLocalDate = getValues(bindingContext, endDateTime + "." + endDateTimeLocalDate); EndLocalTime = getValues(bindingContext, endDateTime + "." + endDateTimeLocalTime); EndLocalDateTime = getValues(bindingContext, endDateTime + "." + endDateTimeLocalDateTime); //************************************************************* //Setup the EndDateTime property of the UiDateTimeRangeModel if (!String.IsNullOrEmpty(EndLocalDateTime)) { EndDateTime = new UiDateTimeModel(TimeZoneName) { DateTimeLocalValue = DateTime.Parse(EndLocalDateTime), ImplicitlySet = EndImplicitlySet }; } else { //Both EndLocalDate and EndLocalTime so we will set the EndDateTime.DateTimeLocalValue to null if (String.IsNullOrEmpty(EndLocalDate) && String.IsNullOrEmpty(EndLocalTime)) { EndDateTime = new UiDateTimeModel(TimeZoneName) { DateTimeLocalValue = null, NoSetTime = false, ImplicitlySet = EndImplicitlySet }; } else { if (String.IsNullOrEmpty(EndLocalDate)) { EndLocalDate = StartLocalDate; EndImplicitlySet = true; } if (String.IsNullOrEmpty(EndLocalTime)) { EndImplicitlySet = true; } EndDateTime = new UiDateTimeModel(TimeZoneName) { DateTimeLocalValue = DateTime.Parse(EndLocalDate + " " + EndLocalTime), ImplicitlySet = EndImplicitlySet }; } } return(new UiDateTimeRangeModel(TimeZoneName) { StartDateTime = StartDateTime, EndDateTime = EndDateTime, }); }
//public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { //Used to determine if values where intentionally set by the application //or as a result of model binding to keep values in sync StartImplicitlySet = false; //Use Reflection to get the associated property names from the UiDateTimeRangeModel var startDateTime = StaticReflection.GetMemberName<UiDateTimeRangeModel>(x => x.StartDateTime); var startDateTimeLocalDate = StaticReflection.GetMemberName<UiDateTimeRangeModel>(x => x.StartDateTime.LocalDate); var startDateTimeLocalTime = StaticReflection.GetMemberName<UiDateTimeRangeModel>(x => x.StartDateTime.LocalTime); var startDateTimeLocalDateTime = StaticReflection.GetMemberName<UiDateTimeRangeModel>(x => x.StartDateTime.DateTimeLocalValue); var startDateTimeNoSetTime = StaticReflection.GetMemberName<UiDateTimeRangeModel>(x => x.StartDateTime.NoSetTime); var endDateTime = StaticReflection.GetMemberName<UiDateTimeRangeModel>(x => x.EndDateTime); var endDateTimeLocalDate = StaticReflection.GetMemberName<UiDateTimeRangeModel>(x => x.StartDateTime.LocalDate); var endDateTimeLocalTime = StaticReflection.GetMemberName<UiDateTimeRangeModel>(x => x.StartDateTime.LocalTime); var endDateTimeLocalDateTime = StaticReflection.GetMemberName<UiDateTimeRangeModel>(x => x.StartDateTime.DateTimeLocalValue); //Get the model values for binding TimeZoneName = getValues(bindingContext, StaticReflection.GetMemberName<UiDateTimeRangeModel>(x => x.TimeZoneName)); StartLocalDate = getValues(bindingContext, startDateTime + "." + startDateTimeLocalDate); StartLocalTime = getValues(bindingContext, startDateTime + "." + startDateTimeLocalTime); StartLocalDateTime = getValues(bindingContext, startDateTime + "." + startDateTimeLocalDateTime); StartNoSetTime = getValues(bindingContext, startDateTime + "." + startDateTimeNoSetTime); var noSetTime = false; if (!string.IsNullOrWhiteSpace(StartNoSetTime)) { if (bool.TryParse(StartNoSetTime.Split(',')[0], out noSetTime) && noSetTime) { StartLocalTime = SqlDateTime.MinValue.Value.Date.ToShortTimeString(); StartImplicitlySet = true; } } //************************************************************* //Setup the StartDateTime property of the UiDateTimeRangeModel if (!String.IsNullOrEmpty(StartLocalDateTime)) { StartDateTime = new UiDateTimeModel(TimeZoneName) { DateTimeLocalValue = DateTime.Parse(StartLocalDateTime), NoSetTime = noSetTime, ImplicitlySet = StartImplicitlySet }; } else { //Both StartLocalDate and StartLocalTime so we will set the StartDateTime.DateTimeLocalValue to null if (String.IsNullOrEmpty(StartLocalDate) && String.IsNullOrEmpty(StartLocalTime)) { StartDateTime = new UiDateTimeModel(TimeZoneName) {DateTimeLocalValue = null, NoSetTime = noSetTime, ImplicitlySet = StartImplicitlySet}; } else { if (String.IsNullOrEmpty(StartLocalDate)) { StartImplicitlySet = true; StartLocalDate = SqlDateTime.MinValue.Value.Date.ToShortDateString(); } if (!noSetTime && String.IsNullOrEmpty(StartLocalTime)) { StartImplicitlySet = true; } StartDateTime = new UiDateTimeModel(TimeZoneName) { DateTimeLocalValue = DateTime.Parse(StartLocalDate + " " + StartLocalTime), NoSetTime = noSetTime, ImplicitlySet = StartImplicitlySet }; } } EndLocalDate = getValues(bindingContext, endDateTime + "." + endDateTimeLocalDate); EndLocalTime = getValues(bindingContext, endDateTime + "." + endDateTimeLocalTime); EndLocalDateTime = getValues(bindingContext, endDateTime + "." + endDateTimeLocalDateTime); //************************************************************* //Setup the EndDateTime property of the UiDateTimeRangeModel if (!String.IsNullOrEmpty(EndLocalDateTime)) { EndDateTime = new UiDateTimeModel(TimeZoneName) {DateTimeLocalValue = DateTime.Parse(EndLocalDateTime), ImplicitlySet = EndImplicitlySet}; } else { //Both EndLocalDate and EndLocalTime so we will set the EndDateTime.DateTimeLocalValue to null if (String.IsNullOrEmpty(EndLocalDate) && String.IsNullOrEmpty(EndLocalTime)) { EndDateTime = new UiDateTimeModel(TimeZoneName) {DateTimeLocalValue = null, NoSetTime = false, ImplicitlySet = EndImplicitlySet}; } else { if (String.IsNullOrEmpty(EndLocalDate)) { EndLocalDate = StartLocalDate; EndImplicitlySet = true; } if (String.IsNullOrEmpty(EndLocalTime)) { EndImplicitlySet = true; } EndDateTime = new UiDateTimeModel(TimeZoneName) { DateTimeLocalValue = DateTime.Parse(EndLocalDate + " " + EndLocalTime), ImplicitlySet = EndImplicitlySet }; } } return new UiDateTimeRangeModel(TimeZoneName) { StartDateTime = StartDateTime, EndDateTime = EndDateTime, }; }