public void AddAddHoursAndMinutes() { DateTime baseDateTime = DateTime.Now; // :-) NhsTime time = new NhsTime(baseDateTime); string addInstruction = string.Format(CultureInfo.CurrentCulture, "+1{0}+5{1}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.HoursUnit, NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.MinutesUnit); Assert.IsTrue(NhsTime.IsAddValid(addInstruction), string.Format(CultureInfo.CurrentCulture, "{0} was not recognised as an Time Add Instruction", addInstruction)); time.Add(addInstruction); Assert.AreEqual(time.TimeValue, baseDateTime.AddHours(1).AddMinutes(5), string.Format(CultureInfo.CurrentCulture, "Failed using {0}", addInstruction)); // Now try without + // Reset baseDateTime = DateTime.Now; time = new NhsTime(baseDateTime); // reinitialise add instructionstring as instruction without operands addInstruction = string.Format(CultureInfo.CurrentCulture, "1{0}5{1}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.HoursUnit, NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.MinutesUnit); Assert.IsTrue(NhsTime.IsAddValid(addInstruction), string.Format(CultureInfo.CurrentCulture, "{0} was not recognised as an Time Add Instruction", addInstruction)); time.Add(addInstruction); Assert.AreEqual(time.TimeValue, baseDateTime.AddHours(1).AddMinutes(5), string.Format(CultureInfo.CurrentCulture, "Failed using {0}", addInstruction)); }
public void NullInstructionTimeOneParameterAddTest() { DateTime baseDateTime = DateTime.Now; NhsTime time = new NhsTime(baseDateTime); time.Add(null); }
public void NullInstructionTimeTwoParametersAddTest() { DateTime baseDateTime = DateTime.Now; NhsTime time = new NhsTime(baseDateTime); NhsTime.Add(time, null); }
public void InvalidInstructionTimeAddTest() { DateTime baseDateTime = DateTime.Now; NhsTime time = new NhsTime(baseDateTime); NhsTime.Add(time, string.Format(CultureInfo.CurrentCulture, "+1{0}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.resourceCulture)); }
/// <summary> /// Check if the ControlToValidate is valid /// </summary> /// <returns>True if control properties is valid</returns> protected override bool EvaluateIsValid() { string value = this.GetControlValidationValue(this.ControlToValidate); NhsTime tryParseResult; return(NhsTime.TryParseExact(value, out tryParseResult, CultureInfo.CurrentCulture)); }
public void ValueProperty() { NhsTime time = new NhsTime(); TimeLabel testLabel = new TimeLabel(); testLabel.Value = time; Assert.AreEqual<NhsTime>(testLabel.Value, time); }
public void IsNullProperty() { NhsTime time = new NhsTime(); // default datetype is exact Assert.IsFalse(time.IsNull); time.TimeType = TimeType.Null; Assert.IsTrue(time.IsNull); }
public void ValueProperty() { NhsTime time = new NhsTime(); TimeLabel testLabel = new TimeLabel(); testLabel.Value = time; Assert.AreEqual <NhsTime>(testLabel.Value, time); }
public void ToStringDisplaySeconds() { NhsTime time = new NhsTime(); string formattedTime = time.ToString(false, CultureInfo.CurrentCulture, true, false, false); TimeSpan parsedTimeSpan = TimeSpan.Parse(formattedTime); Assert.AreEqual <int>(parsedTimeSpan.Seconds, time.TimeValue.Second, "NhsTime DisplaySeconds incorrect"); }
/// <summary> /// Converts the provided dictionary into an object of the specified type /// </summary> /// <param name="dictionary">An IDictionary instance of property data stored as name/value pairs</param> /// <param name="type">The Type of the resulting object</param> /// <param name="serializer">The JavaScriptSerializer instance</param> /// <returns>The deserialized Object</returns> /// <remarks>KeyboardShortcut is never sent back up to the server so we do not support Deserialization</remarks> /// <exception cref="NotImplementedException">Always returns this exception</exception> public override object Deserialize(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer) { NhsTime deserialisationTarget = new NhsTime(); deserialisationTarget.NullIndex = (dictionary.ContainsKey("_nullIndex") ? (int)dictionary["_nullIndex"] : (int)dictionary["nullIndex"]); deserialisationTarget.TimeType = (dictionary.ContainsKey("_timeType") ? (TimeType)dictionary["_timeType"] : (TimeType)dictionary["timeType"]); deserialisationTarget.TimeValue = (dictionary.ContainsKey("_timeValue") ? ((DateTime)dictionary["_timeValue"]).ToLocalTime() : ((DateTime)dictionary["timeValue"]).ToLocalTime()); return(deserialisationTarget); }
public void ParseNotApproximateTruePassedToToString() { System.DateTime baseDateTime = System.DateTime.Now; NhsTime time = new NhsTime(baseDateTime); Assert.AreEqual <TimeType>(time.TimeType, TimeType.Exact, "TimeType should be Exact when set to a date"); NhsTime.ParseExact(time.ToString(true), CultureInfo.CurrentCulture); }
public void ToStringDisplay12Hour() { int hours = 14; DateTime dateTime = new DateTime(2006, 1, 1, hours, 20, 11); NhsTime time = new NhsTime(dateTime); string formattedTime = time.ToString(false, CultureInfo.CurrentCulture, false, true, false); DateTime parsedDateTime = DateTime.Parse(formattedTime, CultureInfo.CurrentCulture); Assert.AreEqual <int>(parsedDateTime.Hour, hours - 12, "NhsTime Display12Hour incorrect"); }
public void ParseWithAMPMDesignator() { GlobalizationService gs = new GlobalizationService(); DateTime now = DateTime.Now; string formattedTime = now.ToString(gs.ShortTimePatternWithSecondsAMPM, CultureInfo.CurrentCulture); NhsTime time = NhsTime.ParseExact(formattedTime, CultureInfo.CurrentCulture); Assert.AreEqual <int>(now.Hour, time.TimeValue.Hour, "Parse failed to set hours with am/pm designator"); Assert.AreEqual <int>(now.Minute, time.TimeValue.Minute, "Parse failed to set minutes with am/pm designator"); Assert.AreEqual <int>(now.Second, time.TimeValue.Second, "Parse failed to set seconds with am/pm designator"); }
/// <summary> /// Handle loading of client state /// </summary> /// <param name="sender">sender</param> /// <param name="e">args</param> private void ExtenderClientStateLoaded(object sender, EventArgs e) { if (this.ClientState != null) { JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new NhsTimeJavascriptConverter() }); this.state = jss.Deserialize <TimeInputClientState>(ClientState); this.value = this.state.Value; } }
public void ParseInvalidNullIndex() { NhsTime parsedTime; string invalidNullIndexString = "Null:-256"; Assert.IsFalse(NhsTime.TryParseExact(invalidNullIndexString, out parsedTime, CultureInfo.CurrentCulture), string.Format(CultureInfo.InvariantCulture, "TryParse failed to return false with string of, {0}", invalidNullIndexString)); invalidNullIndexString = "Null:124"; Assert.IsFalse(NhsTime.TryParseExact(invalidNullIndexString, out parsedTime, CultureInfo.CurrentCulture), string.Format(CultureInfo.InvariantCulture, "TryParse failed to return false with string of, {0}", invalidNullIndexString)); }
public void ToStringDisplayAMPM() { NhsTime morningTime = NhsTime.ParseExact("10:00:00", CultureInfo.CurrentCulture); NhsTime afternoonTime = NhsTime.ParseExact("15:00:00", CultureInfo.CurrentCulture); string formattedMorningTime = morningTime.ToString(false, CultureInfo.CurrentCulture, false, true, true); string formattedAfternoonTime = afternoonTime.ToString(false, CultureInfo.CurrentCulture, false, true, true); string morningDesignator = CultureInfo.CurrentCulture.DateTimeFormat.AMDesignator.ToLower(CultureInfo.CurrentCulture); string afternoonDesignator = CultureInfo.CurrentCulture.DateTimeFormat.PMDesignator.ToLower(CultureInfo.CurrentCulture); Assert.IsTrue(formattedMorningTime.Contains(morningDesignator)); Assert.IsTrue(formattedAfternoonTime.Contains(afternoonDesignator)); }
public void AddSubtract2HoursAdd25Minutes() { DateTime nowAsItWasAtTheStartofTheTest = DateTime.Now; // :-) NhsTime time = new NhsTime(nowAsItWasAtTheStartofTheTest); string addInstruction = string.Format(CultureInfo.CurrentCulture, "-2{0}+25{1}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.HoursUnit, NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.MinutesUnit); Assert.IsTrue(NhsTime.IsAddValid(addInstruction), string.Format(CultureInfo.CurrentCulture, "{0} was not recognised as a Time Add Instruction", addInstruction)); time.Add(addInstruction); Assert.AreEqual(nowAsItWasAtTheStartofTheTest.AddHours(-2).AddMinutes(25), time.TimeValue); }
public void ParseNullIndex() { NhsTime time = new NhsTime(); time.TimeType = TimeType.NullIndex; time.NullIndex = 4; NhsTime parsedTime; Assert.IsTrue(NhsTime.TryParseExact(time.ToString(), out parsedTime, CultureInfo.CurrentCulture), string.Format(CultureInfo.InvariantCulture, "TryParse failed with time, {0}", time.ToString())); Assert.AreEqual <TimeType>(time.TimeType, parsedTime.TimeType, "Parse did not error but TimeType does not match"); Assert.AreEqual(time.NullIndex, parsedTime.NullIndex, "Parse did not error but NullIndex does not match"); }
public void ParseApproximate() { System.DateTime baseDateTime = System.DateTime.Now; NhsTime time = new NhsTime(baseDateTime, true); Assert.AreEqual <TimeType>(time.TimeType, TimeType.Approximate, "TimeType should be Approxinmate when set to a date"); NhsTime parsedTime; Assert.IsTrue(NhsTime.TryParseExact(time.ToString(), out parsedTime, CultureInfo.CurrentCulture), string.Format(CultureInfo.InvariantCulture, "TryParse failed with time, {0}", time.ToString())); Assert.AreEqual(time.TimeType, parsedTime.TimeType, "Parse did not error but TimeType does not match"); Assert.AreEqual <string>(time.TimeValue.ToString("HH:mm", CultureInfo.InvariantCulture), parsedTime.TimeValue.ToString("HH:mm", CultureInfo.InvariantCulture), "Parse did not error but TimeValue does not match"); }
/// <summary> /// Builds a dictionary of name/value pairs /// </summary> /// <param name="obj">The Object to serialize</param> /// <param name="serializer">The JavaScriptSerializer responsible for the serialization</param> /// <returns>An IDictionary object that contains key/value pairs that represent the object�s data</returns> public override IDictionary <string, object> Serialize(object obj, JavaScriptSerializer serializer) { NhsTime time = obj as NhsTime; if (time != null) { //// Create the representation Dictionary <string, object> result = new Dictionary <string, object>(); result.Add("nullIndex", time.NullIndex); result.Add("timeType", time.TimeType); result.Add("timeValue", time.TimeValue); return(result); } return(new Dictionary <string, object>()); }
public void ToStringApproximate() { DateTime baseDateTime = System.DateTime.Now; GlobalizationService gc = new GlobalizationService(); NhsTime time = new NhsTime(baseDateTime, true); Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, "{0} {1}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.Approximate, baseDateTime.ToString(gc.ShortTimePattern, CultureInfo.CurrentCulture)), time.ToString()); Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, "{0} {1}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.Approximate, baseDateTime.ToString(gc.ShortTimePattern, CultureInfo.CurrentCulture)), time.ToString(true)); time = new NhsTime(baseDateTime); time.TimeType = TimeType.Approximate; Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, "{0} {1}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.Approximate, baseDateTime.ToString(gc.ShortTimePattern, CultureInfo.CurrentCulture)), time.ToString()); Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, "{0} {1}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.Approximate, baseDateTime.ToString(gc.ShortTimePattern, CultureInfo.CurrentCulture)), time.ToString(true)); }
/// <summary> /// Refresh the text displayed in the text box. /// </summary> private void RefreshDisplayText() { NhsTime value = this.Value; string[] nullStrings = this.NullStrings; if (value.TimeType != TimeType.NullIndex || nullStrings == null || value.NullIndex < 0 || value.NullIndex >= nullStrings.Length) { this.TextBox.Text = value.ToString( false, CultureInfo.CurrentCulture, this.DisplaySeconds, this.Display12Hour, this.DisplayAMPM); } else { this.TextBox.Text = nullStrings[value.NullIndex]; } }
public void AddAddHour() { DateTime baseDateTime = DateTime.Now; // :-) NhsTime time = new NhsTime(baseDateTime); Assert.IsTrue(NhsTime.IsAddValid(string.Format(CultureInfo.CurrentCulture, "+1{0}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.HoursUnit))); time.Add(string.Format(CultureInfo.CurrentCulture, "+1{0}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.HoursUnit)); Assert.AreEqual(time.TimeValue, baseDateTime.AddHours(1)); // Now try it without the "+", this will check that + is the default // Reset baseDateTime = DateTime.Now; time = new NhsTime(baseDateTime); Assert.IsTrue(NhsTime.IsAddValid(string.Format(CultureInfo.CurrentCulture, "1{0}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.HoursUnit)), "1h was not recognised as a Time Add Instruction"); time.Add(string.Format(CultureInfo.CurrentCulture, "1{0}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.HoursUnit)); Assert.AreEqual(time.TimeValue, baseDateTime.AddHours(1)); }
public void ParseEmptyString() { NhsTime time = NhsTime.ParseExact(string.Empty, CultureInfo.CurrentCulture); Assert.AreEqual <TimeType>(time.TimeType, TimeType.Null, "Parse did not error but TimeType does not match"); }
public void ParseWithSeconds() { NhsTime time = NhsTime.ParseExact("10:30:23", CultureInfo.CurrentCulture); Assert.AreEqual <int>(time.TimeValue.Second, 23, "Parse failed to set seconds"); }
public void ParseNullIndex() { NhsTime time = new NhsTime(); time.TimeType = TimeType.NullIndex; time.NullIndex = 4; NhsTime parsedTime; Assert.IsTrue(NhsTime.TryParseExact(time.ToString(), out parsedTime, CultureInfo.CurrentCulture), string.Format(CultureInfo.InvariantCulture, "TryParse failed with time, {0}", time.ToString())); Assert.AreEqual<TimeType>(time.TimeType, parsedTime.TimeType, "Parse did not error but TimeType does not match"); Assert.AreEqual(time.NullIndex, parsedTime.NullIndex, "Parse did not error but NullIndex does not match"); }
public void ParseShouldFail() { NhsTime.ParseExact("barf", CultureInfo.CurrentCulture); }
public void NullSourceTimeAddTest() { NhsTime.Add(null, string.Format(CultureInfo.CurrentCulture, "+1{0}", NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsTimeResourcesAccessor.HoursUnit)); }
public void ParseNotApproximateTruePassedToToString() { System.DateTime baseDateTime = System.DateTime.Now; NhsTime time = new NhsTime(baseDateTime); Assert.AreEqual<TimeType>(time.TimeType, TimeType.Exact, "TimeType should be Exact when set to a date"); NhsTime.ParseExact(time.ToString(true), CultureInfo.CurrentCulture); }
/// <summary> /// Converts the provided dictionary into an object of the specified type /// </summary> /// <param name="dictionary">An IDictionary instance of property data stored as name/value pairs</param> /// <param name="type">The Type of the resulting object</param> /// <param name="serializer">The JavaScriptSerializer instance</param> /// <returns>The deserialized Object</returns> /// <remarks>KeyboardShortcut is never sent back up to the server so we do not support Deserialization</remarks> /// <exception cref="NotImplementedException">Always returns this exception</exception> public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) { NhsTime deserialisationTarget = new NhsTime(); deserialisationTarget.NullIndex = (dictionary.ContainsKey("_nullIndex") ? (int)dictionary["_nullIndex"] : (int)dictionary["nullIndex"]); deserialisationTarget.TimeType = (dictionary.ContainsKey("_timeType") ? (TimeType)dictionary["_timeType"] : (TimeType)dictionary["timeType"]); deserialisationTarget.TimeValue = (dictionary.ContainsKey("_timeValue") ? ((DateTime)dictionary["_timeValue"]).ToLocalTime() : ((DateTime)dictionary["timeValue"]).ToLocalTime()); return deserialisationTarget; }
/// <summary> /// Handle loading of client state /// </summary> /// <param name="sender">sender</param> /// <param name="e">args</param> private void ExtenderClientStateLoaded(object sender, EventArgs e) { if (this.ClientState != null) { JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new NhsTimeJavascriptConverter() }); this.state = jss.Deserialize<TimeInputClientState>(ClientState); this.value = this.state.Value; } }
private void ValueTextBox_Leave(object sender, EventArgs e) { NhsTime time; if (NhsTime.TryParseExact(this.valueTextBox.Text, out time, CultureInfo.CurrentCulture)) { this.value = time; if (this.value.TimeType == TimeType.Exact && this.approxCheckbox.Checked) { this.value.TimeType = TimeType.Approximate; } } this.valueTextBox.Text = this.value.ToString(false); if (this.value.TimeType != TimeType.Approximate) { this.approxCheckbox.Checked = false; } }
public void ToStringDisplaySeconds() { NhsTime time = new NhsTime(); string formattedTime = time.ToString(false, CultureInfo.CurrentCulture, true, false, false); TimeSpan parsedTimeSpan = TimeSpan.Parse(formattedTime); Assert.AreEqual<int>(parsedTimeSpan.Seconds, time.TimeValue.Second, "NhsTime DisplaySeconds incorrect"); }
/// <summary> /// Resets the control's state fields to their default value. /// </summary> private void ResetFields() { this.currentField = Field.Hours; this.currentMode = InputMode.Simple; this.allowTimeTypeChange = true; this.currentValue = this.Value; this.EditingTimeType = this.TimeType; this.invalidFormat = false; this.LoadResources(); this.SelectCurrentField(); }
public void ParseApproximate() { System.DateTime baseDateTime = System.DateTime.Now; NhsTime time = new NhsTime(baseDateTime, true); Assert.AreEqual<TimeType>(time.TimeType, TimeType.Approximate, "TimeType should be Approxinmate when set to a date"); NhsTime parsedTime; Assert.IsTrue(NhsTime.TryParseExact(time.ToString(), out parsedTime, CultureInfo.CurrentCulture), string.Format(CultureInfo.InvariantCulture, "TryParse failed with time, {0}", time.ToString())); Assert.AreEqual(time.TimeType, parsedTime.TimeType, "Parse did not error but TimeType does not match"); Assert.AreEqual<string>(time.TimeValue.ToString("HH:mm", CultureInfo.InvariantCulture), parsedTime.TimeValue.ToString("HH:mm", CultureInfo.InvariantCulture), "Parse did not error but TimeValue does not match"); }
public void ToStringDisplay12Hour() { int hours = 14; DateTime dateTime = new DateTime(2006, 1, 1, hours, 20, 11); NhsTime time = new NhsTime(dateTime); string formattedTime = time.ToString(false, CultureInfo.CurrentCulture, false, true, false); DateTime parsedDateTime = DateTime.Parse(formattedTime, CultureInfo.CurrentCulture); Assert.AreEqual<int>(parsedDateTime.Hour, hours - 12, "NhsTime Display12Hour incorrect"); }
/// <summary> /// Refresh the text displayed in the text box. /// </summary> /// <returns> /// Status of update. /// </returns> private bool ValidateAndUpdateValue() { NhsTime result; bool status = true; bool containedPM = this.ISPMContained(); this.SetValid(); this.recentSelectionStart = this.txtInput.SelectionStart; this.recentSelectionLength = this.txtInput.SelectionLength; // for null index nothing to be done as index is updated while setting. if (this.EditingTimeType != TimeType.NullIndex) { if (NhsTime.IsAddValid(this.txtInput.Text)) { this.Value.Add(this.txtInput.Text); this.RefreshDisplayText(); this.ResetFields(); return status; } else if (NhsTime.TryParseExact(this.txtInput.Text, out result, CultureInfo.CurrentCulture)) { // Handles an exception case, TryParse always returns the text in am no matter // input contains am or pm. if (containedPM && result.TimeValue.Hour < 12) { result.TimeValue = result.TimeValue.AddHours(12); } this.Value = result; this.invalidFormat = false; } else { this.SetInvalid(ref status); } } this.allowTimeTypeChange = true; this.currentValue = this.Value; this.EditingTimeType = this.TimeType; this.currentMode = InputMode.Simple; this.txtInput.SelectionStart = this.txtInput.TextLength; this.txtInput.SelectionLength = 0; return status; }
/// <summary> /// Set the value to 23:59. /// </summary> /// <param name="sender">Sender of the event.</param> /// <param name="e">Event args for the event.</param> private void SetTo2359Button_Click(object sender, EventArgs e) { this.timeInputBox3.Value = NhsTime.ParseExact("23:59", CultureInfo.CurrentCulture); }
public void NullInstructionTimeIsAddInstructionTest() { NhsTime.IsAddValid(null); }