public NhsTime(string time, bool parseAll) { NhsTime newTime = NhsTime.ParseExact(time, CultureInfo.CurrentCulture); this.timeValue = newTime.timeValue; this.timeType = newTime.timeType; this.nullIndex = newTime.nullIndex; }
/// <summary> /// Determines whether the specified <see cref="NhsCui.Toolkit.DateAndTime.NhsTime"/> is equal to this instance. /// </summary> /// <param name="nhsTime">The <see cref="NhsCui.Toolkit.DateAndTime.NhsTime"/> to compare with this instance.</param> /// <returns> /// Returns <c>true</c> if the specified <see cref="NhsCui.Toolkit.DateAndTime.NhsTime"/> is equal to this instance; otherwise, <c>false</c>. /// </returns> /// <exception cref="T:System.ArgumentNullException">The <paramref name="nhsTime"/> parameter is null.</exception> public bool Equals(NhsTime nhsTime) { if (nhsTime == null) { throw new ArgumentNullException("nhsTime"); } return(this.Equals(nhsTime.TimeValue)); }
/// <summary> /// Adds a number of hours or minutes to a time. /// </summary> /// <param name="instruction">Add instructions such as +12h to add 12 hours or -20m to subtract 20 minutes.</param> /// <exception cref="System.ArgumentNullException">Instruction is null. </exception> /// // <remarks> /// The operand can be positive or negative; if the operand is not included, it is assumed to be positive. /// </remarks> public void Add(string instruction) { if (instruction == null) { throw new ArgumentNullException("instruction"); } this.TimeValue = NhsTime.Add(this, instruction).TimeValue; }
/// <summary> /// Converts the given object to the type of the converter using the specified context and culture information. /// </summary> /// <param name="context">An ITypeDescriptorContext that provides a format context. </param> /// <param name="culture">CultureInfo culture.</param> /// <param name="value">The Object to convert. </param> /// <returns>An Object that represents the converted value. </returns> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string stringValue = value as string; if (stringValue != null) { return(NhsTime.ParseExact(stringValue, CultureInfo.CurrentCulture)); } return(base.ConvertFrom(context, culture, 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; } }
/// <summary> /// Converts the given value object to the specified type, /// using the specified context and culture information. /// </summary> /// <param name="context">An ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">CultureInfo culture.</param> /// <param name="value">The Object to be converted. </param> /// <param name="destinationType">The Type the value parameter is to be converted to. </param> /// <returns>The Type the value parameter is to be converted to. </returns> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType != null && destinationType == typeof(string) || destinationType == typeof(InstanceDescriptor)) { NhsTime time = (NhsTime)value; string formattedTime = time.ToString( time.TimeType == TimeType.Approximate, CultureInfo.CurrentCulture, time.TimeType == TimeType.Exact && time.TimeValue.Second > 0, false, false); if (destinationType == typeof(string)) { return(formattedTime); } ConstructorInfo constructor = typeof(NhsTime).GetConstructor(new Type[] { typeof(string), typeof(bool) }); return(new InstanceDescriptor(constructor, new object[] { formattedTime, true })); } return(base.ConvertTo(context, culture, value, destinationType)); }
/// <summary> /// Adds a number of hours or minutes to a time and returns a new NhsTime object. /// </summary> /// <param name="sourceTime">The time to be added to. </param> /// <param name="instruction">Add instructions such as +12h to add 12 hours or -20m to subtract 20 minutes. </param> /// <exception cref="System.ArgumentNullException">If either argument is null.</exception> /// <returns>A new NhsTime object.</returns> /// <remarks> /// The operand can be positive or negative; if the operand is not included, it is assumed to be positive. /// </remarks> public static NhsTime Add(NhsTime sourceTime, string instruction) { if (instruction == null) { throw new ArgumentNullException("instruction"); } if (sourceTime == null) { throw new ArgumentNullException("sourceTime"); } if (NhsTime.IsAddValid(instruction) == false) { throw new ArgumentOutOfRangeException("instruction", instruction, Resources.NhsTimeResources.AddInstructionInvalidFormat); } // check resources are valid if (Resources.NhsTimeResources.HoursUnit.Length == 0 || Resources.NhsTimeResources.MinutesUnit.Length == 0 || Resources.NhsTimeResources.HoursUnit == Resources.NhsTimeResources.MinutesUnit) { throw new InvalidArithmeticSetException(Resources.NhsTimeResources.ArithmeticSetInvalidResources); } Regex shortcut = new Regex(ResolveAddInstructionRegExTokens(), RegexOptions.IgnoreCase); Match match = shortcut.Match(instruction.Replace(" ", string.Empty)); if (match.Success == true) { // We need to skip the first group because with this RegEx it is always the complete string int groupNumber = 0; foreach (Group g in match.Groups) { if (groupNumber > 0) // Skip first group { foreach (Capture c in g.Captures) { int increment; if (int.TryParse(c.Value.Substring(0, c.Value.Length - 1), out increment)) { if (c.Value.EndsWith(Resources.NhsTimeResources.HoursUnit, StringComparison.OrdinalIgnoreCase) == true) { // Add hours sourceTime.TimeValue = sourceTime.TimeValue.AddHours(increment); } else if (c.Value.EndsWith(Resources.NhsTimeResources.MinutesUnit, StringComparison.OrdinalIgnoreCase) == true) { // Add minutes sourceTime.TimeValue = sourceTime.TimeValue.AddMinutes(increment); } } } } groupNumber++; } } return(sourceTime); }
/// <summary> /// Parses a string that represents a time. /// </summary> /// <param name="time">A string containing the value to be parsed. </param> /// <param name="result">A container for a successfully-parsed time. </param> /// <param name="cultureInfo">The culture that should be used to parse the string. If a string is culture-agnostic, /// this should be CultureInfo.InvariantCulture. Defaults to CurrentCulture.</param> /// <returns>True if time string was successfully parsed; otherwise, false. </returns> /// <remarks>If the string be parsed, the result parameter is set to an NhsTime object corresponding to the parsed timeString. /// If the time string cannot be parsed, the result parameter is set to DateTime.MinValue. </remarks> public static bool TryParseExact(string time, out NhsTime result, CultureInfo cultureInfo) { result = null; // check for true null if (string.IsNullOrEmpty(time)) { result = new NhsTime(); result.TimeType = TimeType.Null; return(true); } bool approxIndicatorPresent = false; // first check to see if approx indicator is present if (time.IndexOf(Resources.NhsTimeResources.Approximate, StringComparison.CurrentCultureIgnoreCase) >= 0) { time = time.Replace(Resources.NhsTimeResources.Approximate, string.Empty).Trim(); approxIndicatorPresent = true; } DateTime parsedDateTime; // try standard datetime parse with our custom formats GlobalizationService gs = new GlobalizationService(); string[] formats = new string[] { gs.ShortTimePattern, gs.ShortTimePatternWithSeconds, gs.ShortTimePatternAMPM, gs.ShortTimePatternWithSecondsAMPM, gs.ShortTimePattern12Hour, gs.ShortTimePatternWithSeconds12Hour, gs.ShortTimePattern12HourAMPM, gs.ShortTimePatternWithSeconds12HourAMPM }; if (DateTime.TryParseExact(time, formats, cultureInfo, DateTimeStyles.None, out parsedDateTime)) { result = new NhsTime(parsedDateTime, approxIndicatorPresent); return(true); } if (!approxIndicatorPresent) { // Check if 'time' is a Null time Regex nullTimeRegEx = new Regex(@"^Null:(?<Index>-?\d+)$", RegexOptions.IgnoreCase); Match match = nullTimeRegEx.Match(time); if (match.Success == true) { // Pull the numeric Index text and see if it is in range int nullIndex; if (int.TryParse(match.Groups[1].Captures[0].Value, out nullIndex) == true) { if (nullIndex >= MinimumNullIndex && nullIndex <= MaximumNullIndex) { result = new NhsTime(); result.NullIndex = nullIndex; result.TimeType = TimeType.NullIndex; return(true); } } } } return(false); }
/// <summary> /// Parses a string that represents a time. /// </summary> /// <param name="time">A string containing the value to be parsed. </param> /// <param name="result">A container for a successfully-parsed time. </param> /// <returns>True if time string was successfully parsed; otherwise, false. </returns> /// <remarks>If the string be parsed, the result parameter is set to an NhsTime object corresponding to the parsed timeString. /// If the time string cannot be parsed, the result parameter is set to DateTime.MinValue. </remarks> public static bool TryParseExact(string time, out NhsTime result) { return(NhsTime.TryParseExact(time, out result, CultureInfo.CurrentCulture)); }
/// <summary> /// Parses a string that represents a time and returns a corresponding NhsTime object. /// </summary> /// <param name="time">The time to be parsed.</param> /// <returns>An NhsTime object. </returns> /// <exception cref="System.ArgumentNullException">Time is null. </exception> /// <exception cref="System.FormatException">Time is not in a recognised format.</exception> public static NhsTime ParseExact(string time) { return(NhsTime.ParseExact(time, CultureInfo.CurrentCulture)); }
/// <summary> /// Constructs an NhsTime object, uses Time.Parse to create a new temporary NhsTime object for /// the given string and copies the property values from the temporary NhsTime object /// to the new NhsTime object. /// </summary> /// <param name="time">The time as a string.</param> public NhsTime(string time) { this.timeType = TimeType.Exact; this.timeValue = NhsTime.ParseExact(time, CultureInfo.CurrentCulture).timeValue; }