public CuiTime(string time, bool parseAll) { CuiTime newTime = CuiTime.ParseExact(time, CultureInfo.CurrentCulture); this.timeValue = newTime.timeValue; this.timeType = newTime.timeType; this.nullIndex = newTime.nullIndex; }
/// <summary> /// Determines whether the specified <see cref="Microsoft.Cui.Controls.Common.DateAndTime.CuiTime"/> is equal to this instance. /// </summary> /// <param name="cuiTime">The <see cref="Microsoft.Cui.Controls.Common.DateAndTime.CuiTime"/> to compare with this instance.</param> /// <returns> /// Returns <c>true</c> if the specified <see cref="Microsoft.Cui.Controls.Common.DateAndTime.CuiTime"/> is equal to this instance; otherwise, <c>false</c>. /// </returns> /// <exception cref="T:System.ArgumentNullException">The <paramref name="cuiTime"/> parameter is null.</exception> public bool Equals(CuiTime cuiTime) { if (cuiTime == null) { throw new ArgumentNullException("cuiTime"); } return(this.Equals(cuiTime.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 = CuiTime.Add(this, instruction).TimeValue; }
/// <summary> /// Determines whether the specified <see cref="Microsoft.Cui.Controls.Common.DateAndTime.CuiTime"/> is equal to this instance. /// </summary> /// <param name="cuiTime">The <see cref="Microsoft.Cui.Controls.Common.DateAndTime.CuiTime"/> to compare with this instance.</param> /// <returns> /// Returns <c>true</c> if the specified <see cref="Microsoft.Cui.Controls.Common.DateAndTime.CuiTime"/> is equal to this instance; otherwise, <c>false</c>. /// </returns> /// <exception cref="T:System.ArgumentNullException">The <paramref name="cuiTime"/> parameter is null.</exception> public bool Equals(CuiTime cuiTime) { if (cuiTime == null) { throw new ArgumentNullException("cuiTime"); } return this.Equals(cuiTime.TimeValue); }
/// <summary> /// Adds a number of hours or minutes to a time and returns a new CuiTime 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 CuiTime 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 CuiTime Add(CuiTime sourceTime, string instruction) { if (instruction == null) { throw new ArgumentNullException("instruction"); } if (sourceTime == null) { throw new ArgumentNullException("sourceTime"); } if (CuiTime.IsAddValid(instruction) == false) { throw new ArgumentOutOfRangeException("instruction", Resources.CuiTimeResources.AddInstructionInvalidFormat); } // check resources are valid if (Resources.CuiTimeResources.HoursUnit.Length == 0 || Resources.CuiTimeResources.MinutesUnit.Length == 0 || Resources.CuiTimeResources.HoursUnit == Resources.CuiTimeResources.MinutesUnit) { throw new InvalidArithmeticSetException(Resources.CuiTimeResources.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) { 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.CuiTimeResources.HoursUnit, StringComparison.OrdinalIgnoreCase) == true) { // Add hours sourceTime.TimeValue = sourceTime.TimeValue.AddHours(increment); } else if (c.Value.EndsWith(Resources.CuiTimeResources.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 a CuiTime 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 CuiTime result, IFormatProvider cultureInfo) { result = null; // check for true null if (string.IsNullOrEmpty(time)) { result = new CuiTime(); result.TimeType = TimeType.Null; return true; } bool approxIndicatorPresent = false; // first check to see if approx indicator is present if (time.IndexOf(Resources.CuiTimeResources.Approximate, StringComparison.CurrentCultureIgnoreCase) >= 0) { time = time.Replace(Resources.CuiTimeResources.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 CuiTime(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 CuiTime(); 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 a CuiTime 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 CuiTime result) { return CuiTime.TryParseExact(time, out result, CultureInfo.CurrentCulture); }
/// <summary> /// Adds a number of hours or minutes to a time and returns a new CuiTime 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 CuiTime 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 CuiTime Add(CuiTime sourceTime, string instruction) { if (instruction == null) { throw new ArgumentNullException("instruction"); } if (sourceTime == null) { throw new ArgumentNullException("sourceTime"); } if (CuiTime.IsAddValid(instruction) == false) { throw new ArgumentOutOfRangeException("instruction", Resources.CuiTimeResources.AddInstructionInvalidFormat); } // check resources are valid if (Resources.CuiTimeResources.HoursUnit.Length == 0 || Resources.CuiTimeResources.MinutesUnit.Length == 0 || Resources.CuiTimeResources.HoursUnit == Resources.CuiTimeResources.MinutesUnit) { throw new InvalidArithmeticSetException(Resources.CuiTimeResources.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) { 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.CuiTimeResources.HoursUnit, StringComparison.OrdinalIgnoreCase) == true) { // Add hours sourceTime.TimeValue = sourceTime.TimeValue.AddHours(increment); } else if (c.Value.EndsWith(Resources.CuiTimeResources.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 a CuiTime 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 CuiTime result, IFormatProvider cultureInfo) { result = null; // check for true null if (string.IsNullOrEmpty(time)) { result = new CuiTime(); result.TimeType = TimeType.Null; return(true); } bool approxIndicatorPresent = false; // first check to see if approx indicator is present if (time.IndexOf(Resources.CuiTimeResources.Approximate, StringComparison.CurrentCultureIgnoreCase) >= 0) { time = time.Replace(Resources.CuiTimeResources.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 CuiTime(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 CuiTime(); 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 a CuiTime 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 CuiTime result) { return(CuiTime.TryParseExact(time, out result, CultureInfo.CurrentCulture)); }
/// <summary> /// Parses a string that represents a time and returns a corresponding CuiTime object. /// </summary> /// <param name="time">The time to be parsed.</param> /// <returns>A CuiTime object. </returns> /// <exception cref="System.ArgumentNullException">Time is null. </exception> /// <exception cref="System.FormatException">Time is not in a recognised format.</exception> public static CuiTime ParseExact(string time) { return(CuiTime.ParseExact(time, CultureInfo.CurrentCulture)); }
/// <summary> /// Constructs a CuiTime object, uses Time.Parse to create a new temporary CuiTime object for /// the given string and copies the property values from the temporary CuiTime object /// to the new CuiTime object. /// </summary> /// <param name="time">The time as a string.</param> public CuiTime(string time) { this.timeType = TimeType.Exact; this.timeValue = CuiTime.ParseExact(time, CultureInfo.CurrentCulture).timeValue; }