コード例 #1
0
ファイル: NhsTime.cs プロジェクト: odnodn/mscui
        /// <summary>
        /// Gets the time format to use from the supplied globalization service.
        /// </summary>
        /// <param name="gs">globalization service to get format from</param>
        /// <param name="displaySeconds">whether to display seconds</param>
        /// <param name="display12Hour">whether to use 12 hour or 24 hour clock</param>
        /// <param name="displayAMPM">whether to display am / pm indicator</param>
        /// <returns>time format</returns>
        private static string GetTimeFormat(GlobalizationService gs, bool displaySeconds, bool display12Hour, bool displayAMPM)
        {
            string format;

            if (displaySeconds)
            {
                if (display12Hour)
                {
                    format = displayAMPM ? gs.ShortTimePatternWithSeconds12HourAMPM :
                             gs.ShortTimePatternWithSeconds12Hour;
                }
                else
                {
                    format = displayAMPM ? gs.ShortTimePatternWithSecondsAMPM :
                             gs.ShortTimePatternWithSeconds;
                }
            }
            else
            {
                if (display12Hour)
                {
                    format = displayAMPM ? gs.ShortTimePattern12HourAMPM :
                             gs.ShortTimePattern12Hour;
                }
                else
                {
                    format = displayAMPM ? gs.ShortTimePatternAMPM :
                             gs.ShortTimePattern;
                }
            }

            return(format);
        }
コード例 #2
0
ファイル: NhsTime.cs プロジェクト: odnodn/mscui
        public string ToString(bool approximate, CultureInfo cultureInfo, bool displaySeconds, bool display12Hour, bool displayAMPM)
        {
            if (approximate && this.TimeType != TimeType.Approximate)
            {
                // This is invalid. You cannot make use of the indicateWhenApproximate flag when the
                // TimeType is not Approximate
                throw new ArgumentOutOfRangeException("approximate", Resources.NhsTimeResources.ShowApproxIndicatorInvalidForTimeType);
            }

            string formattedTime;

            switch (this.TimeType)
            {
            case TimeType.Exact:
            case TimeType.Approximate:
                GlobalizationService gs = new GlobalizationService();

                // we never return a string such as "14:04 (pm)"
                bool reallyDisplayAMPM = displayAMPM && (display12Hour || this.TimeValue.TimeOfDay.Hours < 12);

                string format = GetTimeFormat(gs, displaySeconds, display12Hour, reallyDisplayAMPM);

                formattedTime = this.timeValue.ToString(format, cultureInfo);

                if (reallyDisplayAMPM)
                {
                    // make am / pm lowercase
                    formattedTime = formattedTime.ToLower(cultureInfo);
                }

                if (approximate)
                {
                    // prepend "Approx" indicator
                    formattedTime = string.Format(
                        cultureInfo,
                        Resources.NhsTimeResources.ApproximateTimeFormat,
                        Resources.NhsTimeResources.Approximate,
                        formattedTime);
                }

                break;

            case TimeType.NullIndex:
                formattedTime = string.Format(cultureInfo, "Null:{0}", this.NullIndex);
                break;

            case TimeType.Null:
                formattedTime = string.Empty;
                break;

            default:
                throw new InvalidOperationException();
            }

            return(formattedTime);
        }
コード例 #3
0
ファイル: TimeInputBox.cs プロジェクト: rbirkby/mscui
 /// <summary>
 /// Returns the time Separator being used.
 /// </summary>
 /// <returns>The time Separator.</returns>
 private static String GetTimeSeparator()
 {
     GlobalizationService gs = new GlobalizationService();
     return gs.ShortTimePattern.Substring(2, 1);            
 }
コード例 #4
0
ファイル: TimeInputBox.cs プロジェクト: rbirkby/mscui
 /// <summary>
 /// Returns the string for PM depending upon the current culture.
 /// </summary>
 /// <returns>The string for PM depending upon the current culture.</returns>
 private static String GetCultureSpecificPmString()
 {
     GlobalizationService gs = new GlobalizationService();
     DateTime sampleDate = new DateTime(2007, 12, 12, 14, 12, 12);
     return (sampleDate.ToString(gs.ShortTimePatternAMPM.Substring(gs.ShortTimePatternAMPM.Length - 3, 2), CultureInfo.CurrentCulture).ToLower(CultureInfo.CurrentCulture));
 }
コード例 #5
0
ファイル: TimeInputBox.cs プロジェクト: rbirkby/mscui
 /// <summary>
 /// Handles the font changed event of the control.
 /// </summary>
 /// <param name="sender">Object calling the event</param>
 /// <param name="e">Event arguments</param> 
 private void TimeInputBox_FontChanged(object sender, EventArgs e)
 {
     GlobalizationService gs = new GlobalizationService();
     string max = gs.ShortTimePatternWithSecondsAMPM + "d";            
     Graphics g = this.CreateGraphics();
     SizeF newSize = g.MeasureString(max, this.Font);
     int singleCharWidth = (int)(newSize.Width / max.Length);
     this.btnDown.Width = singleCharWidth > MinButtonWidth ? singleCharWidth : MinButtonWidth;
     this.btnUp.Width = this.btnDown.Width;
     this.Width = newSize.Width > MinWidth ? (int)newSize.Width : MinWidth;
     this.Height = newSize.Height + BorderPadding > MinHeight ? (int)newSize.Height + BorderPadding : MinHeight;
 }
コード例 #6
0
ファイル: DateInputBox.cs プロジェクト: rbirkby/mscui
 /// <summary>
 /// Handles the font changed event of the control.
 /// </summary>
 /// <param name="sender">Object calling the event</param>
 /// <param name="e">Event arguments</param> 
 private void DateInputBox_FontChanged(object sender, EventArgs e)
 {
     GlobalizationService gs = new GlobalizationService();
     string max = gs.ShortDatePatternWithDayOfWeek;
     Graphics g = this.CreateGraphics();
     SizeF newSize = g.MeasureString(max, this.Font);
     this.Width = newSize.Width + this.btnCalendar.Width > MinWidth ? (int)newSize.Width + this.btnCalendar.Width : MinWidth;
     this.Height = newSize.Height + BorderPadding > MinHeight ? (int)newSize.Height + BorderPadding : MinHeight;
 }
コード例 #7
0
ファイル: NhsDate.cs プロジェクト: rbirkby/mscui
        /// <summary>
        /// Returns a string representing the date.
        /// </summary>
        /// <param name="includeDayOfWeek">Includes the day of the week in the string. </param>
        /// <param name="approximate">When the <see cref="P:NhsCui.Toolkit.DateAndTime.NhsDate.DateType">DateType</see> 
        /// is DateType.Approximate, shows the Approx text indicator.</param>
        /// <param name="showRelativeDayText">If the date is Today, Tomorrow or Yesterday, returns a string rather than the date. </param>
        /// <param name="cultureInfo">The culture that should be used to parse the string. </param>
        /// <returns>The date as a string. </returns>
        public string ToString(bool includeDayOfWeek, bool approximate, bool showRelativeDayText, CultureInfo cultureInfo)
        {
            if (cultureInfo == null)
            {
                throw new ArgumentNullException("cultureInfo");
            }

            if (this.DateType != DateType.Approximate && approximate == true)
            {
                // This is invalid. You cannot make use of the indicateWhenApproximate flag when the 
                // Date is not Approximate
                throw new ArgumentOutOfRangeException("approximate", Resources.NhsDateResources.ShowApproxIndicatorInvalidForDateType);
            }

            string formattedDate = null;

            switch (this.dateType)
            {
                case DateType.Exact:
                case DateType.Approximate:

                    if (showRelativeDayText)
                    {
                        DateTime date = this.DateValue.Date;
                        if (date == System.DateTime.Today)
                        {
                            formattedDate = Resources.NhsDateResources.Today;
                        }
                        else if (date == System.DateTime.Today.AddDays(-1))
                        {
                            formattedDate = Resources.NhsDateResources.Yesterday;
                        }
                        else if (date == System.DateTime.Today.AddDays(1))
                        {
                            formattedDate = Resources.NhsDateResources.Tomorrow;
                        }
                    }

                    if (formattedDate == null)
                    {
                        GlobalizationService gs = new GlobalizationService();
                        formattedDate = this.DateValue.ToString(includeDayOfWeek ? gs.ShortDatePatternWithDayOfWeek : gs.ShortDatePattern, cultureInfo);
                    }

                    if (this.DateType == DateType.Approximate && approximate)
                    {
                        formattedDate = string.Format(
                                        cultureInfo,
                                        Resources.NhsDateResources.ApproximateDateFormat,
                                        Resources.NhsDateResources.Approximate,
                                        formattedDate);
                    }

                    break;               

                case DateType.Year:
                    formattedDate = this.Year.ToString("0000", cultureInfo);
                    break;

                case DateType.YearMonth:
                    formattedDate = string.Format(
                                    cultureInfo, 
                                    "{0}-{1}", 
                                    cultureInfo.DateTimeFormat.MonthNames[this.Month - 1], 
                                    this.Year.ToString("0000", cultureInfo));
                    break;

                case DateType.NullIndex:
                    formattedDate = string.Format(cultureInfo, "Null:{0}", this.NullIndex);
                    break;

                case DateType.Null:
                    formattedDate = string.Empty;
                    break;

                default:
                    throw new InvalidOperationException();
            }

            return formattedDate;
        }
コード例 #8
0
ファイル: NhsDate.cs プロジェクト: rbirkby/mscui
        /// <summary>
        /// Parses a string that represents a date. 
        /// </summary>
        /// <param name="date">A string containing the value to be converted.   </param>
        /// <param name="result">A container for a successfully-parsed date.  </param>
        /// <param name="cultureInfo">The culture that should be used to parse the string.</param>
        /// <returns>True if the date string was successfully parsed; otherwise, false.  </returns>
        /// <remarks>
        ///  If the string can be parsed, 
        /// the result parameter is set to an NhsDate object corresponding to 
        /// the parsed dateString. If the string cannot be parsed, the result parameter is set to DateTime.MinValue. 
        /// </remarks>
        public static bool TryParseExact(string date, out NhsDate result, CultureInfo cultureInfo)
        {
            // check for true null
            if (string.IsNullOrEmpty(date))
            {
                result = new NhsDate();
                result.DateType = DateType.Null;
                return true;
            }

            // first check for numeric year month as this can be confused as a date
            Regex numericYearMonthRegEx = new Regex("^(?<Month>0?[1-9]|10|11|12)[-\\s/](?<Year>\\d{4}|\\d{2})$");
            Match numericYearMonthRegExResults = numericYearMonthRegEx.Match(date);

            if (numericYearMonthRegExResults.Success)
            {
                result = new NhsDate();
                result.DateType = DateType.YearMonth;
                result.Month = int.Parse(numericYearMonthRegExResults.Groups["Month"].Value, cultureInfo);
                result.Year = NhsDate.ParseYear(numericYearMonthRegExResults.Groups["Year"].Value, cultureInfo);

                return true;
            }

            // check to see if approx indicator is present
            bool approxIndicatorPresent = (date.IndexOf(Resources.NhsDateResources.Approximate, StringComparison.CurrentCultureIgnoreCase) >= 0);

            if (approxIndicatorPresent)
            {
                date = date.Replace(Resources.NhsDateResources.Approximate, string.Empty).Trim();
            }

            // try datetime parse with our standard formats
            GlobalizationService gs = new GlobalizationService();
            string[] standardFormats = new string[] 
                                   {
                                       gs.ShortDatePattern,
                                       gs.ShortDatePatternWithDayOfWeek,
                                   };

            DateTime parsedDateTime;
            if (DateTime.TryParseExact(date, standardFormats, cultureInfo, DateTimeStyles.None, out parsedDateTime))
            {
                result = new NhsDate(parsedDateTime);
                result.DateType = (approxIndicatorPresent ? DateType.Approximate : DateType.Exact);
                return true;
            }

            // Check if 'date' is a year and a month
            Regex yearMonthRegEx = BuildMonthYearRegEx(cultureInfo);
            Match yearMonthMatch = yearMonthRegEx.Match(date);

            // Regex doesn't take care of year as zero
            if (yearMonthMatch.Success && NhsDate.ParseYear(yearMonthMatch.Groups["Year"].Value, cultureInfo) > 0)
            {
                int month = GetMonthNumberFromMonthName(yearMonthMatch.Groups["Month"].Value, cultureInfo);
                int year = NhsDate.ParseYear(yearMonthMatch.Groups["Year"].Value, cultureInfo);             
                result = new NhsDate(year, month);
                return true;
            }           

            // Check if 'date' is a year
            Regex yearRegEx = new Regex(@"^(\d{4}|\d{2})$");

            // Regex doesn't take care of year as zero
            if (yearRegEx.IsMatch(date) && NhsDate.ParseYear(date, cultureInfo) > 0)
            {
                result = new NhsDate(NhsDate.ParseYear(date, cultureInfo));
                return true;
            }

            // Check if 'date' is a Null date
            Regex nullDateRegEx = new Regex(@"^Null:(?<Index>-?\d+)$", RegexOptions.IgnoreCase);

            Match match = nullDateRegEx.Match(date);

            if (match.Success)
            {
                // 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))
                {
                    if (nullIndex >= MinimumNullIndex && nullIndex <= MaximumNullIndex)
                    {
                        result = new NhsDate();
                        result.NullIndex = nullIndex;
                        result.DateType = DateType.NullIndex;

                        return true;
                    }
                }
            }

            // try alternative formats
            string[] alternativeFormats = new string[] 
                                   {
                                        "d-MMM-yyyy", "d-M-yyyy", "d-MM-yyyy", "d-MMMM-yyyy",
                                        "d/MMM/yyyy", "d/M/yyyy", "d/MM/yyyy", "d/MMMM/yyyy",
                                        "d MMM yyyy", "d M yyyy", "d MM yyyy", "d MMMM yyyy",
                                        "ddd d-MMM-yyyy", "ddd d-M-yyyy", "ddd d-MM-yyyy", "ddd d-MMMM-yyyy",
                                        "ddd d/MMM/yyyy", "ddd d/M/yyyy", "ddd d/MM/yyyy", "ddd d/MMMM/yyyy",
                                        "ddd d MMM yyyy", "ddd d M yyyy", "ddd d MM yyyy", "ddd d MMMM yyyy",
                                        "d-MMM-yy", "d-M-yy", "d-MM-yy", "d-MMMM-yy",
                                        "d/MMM/yy", "d/M/yy", "d/MM/yy", "d/MMMM/yy",
                                        "d MMM yy", "d M yy", "d MM yy", "d MMMM yy",
                                        "ddd d-MMM-yy", "ddd d-M-yy", "ddd d-MM-yy", "ddd d-MMMM-yy",
                                        "ddd d/MMM/yy", "ddd d/M/yy", "ddd d/MM/yy", "ddd d/MMMM/yy",
                                        "ddd d MMM yy", "ddd d M yy", "ddd d MM yy", "ddd d MMMM yy"
                                  };

            if (DateTime.TryParseExact(date, alternativeFormats, cultureInfo, DateTimeStyles.None, out parsedDateTime))
            {
                result = new NhsDate(parsedDateTime);
                result.DateType = (approxIndicatorPresent ? DateType.Approximate : DateType.Exact);
                return true;
            }

            // no match
            result = null;
            return false;
        }
コード例 #9
0
ファイル: DateTest.cs プロジェクト: rbirkby/mscui
        public void ToStringExactGetRelativeText()
        {
            NhsDate date = new NhsDate(System.DateTime.Today);

            GlobalizationService gc = new GlobalizationService();

            // Today works
            Assert.AreEqual(NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsDateResourcesAccessor.Today, date.ToString(false, false, true, CultureInfo.CurrentCulture), "RelativeText of Today is not working");

            date.DateValue = DateTime.Today.AddDays(1);

            Assert.AreEqual(NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsDateResourcesAccessor.Tomorrow, date.ToString(false, false, true, CultureInfo.CurrentCulture), "RelativeText of Tomorrow is not working");

            date.DateValue = DateTime.Today.AddDays(-1);

            Assert.AreEqual(NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsDateResourcesAccessor.Yesterday, date.ToString(false, false, true, CultureInfo.CurrentCulture), "RelativeText of Yesterday is not working");
            
            // Check the "Who Wins condition when "Show Day of Week" and "Show Relative Text" are both true
            Assert.AreEqual(NhsCui.Toolkit.Test.NhsCui_Toolkit_DateAndTime_Resources_NhsDateResourcesAccessor.Yesterday, date.ToString(true, false, true, CultureInfo.CurrentCulture), "When includeDayOfWeek, showRelativeText are both true, showRelativeText should win. It is not");

            DateTime testDate = new DateTime(1974, 3, 26);

            date.DateValue = testDate;

            Assert.AreEqual(testDate.ToString(gc.ShortDatePattern, CultureInfo.CurrentCulture), date.ToString(), "Check that default for 'include day of week' flag is correct");

            // try with includeDayOfWeek flag

            Assert.AreEqual(testDate.ToString(gc.ShortDatePatternWithDayOfWeek, CultureInfo.CurrentCulture), date.ToString(true), "Check that default for 'include day of week' flag is correct");
        }
コード例 #10
0
ファイル: NhsDate.cs プロジェクト: odnodn/mscui
        /// <summary>
        /// Returns a string representing the date.
        /// </summary>
        /// <param name="includeDayOfWeek">Includes the day of the week in the string. </param>
        /// <param name="approximate">When the <see cref="P:NhsCui.Toolkit.DateAndTime.NhsDate.DateType">DateType</see>
        /// is DateType.Approximate, shows the Approx text indicator.</param>
        /// <param name="showRelativeDayText">If the date is Today, Tomorrow or Yesterday, returns a string rather than the date. </param>
        /// <param name="cultureInfo">The culture that should be used to parse the string. </param>
        /// <returns>The date as a string. </returns>
        public string ToString(bool includeDayOfWeek, bool approximate, bool showRelativeDayText, CultureInfo cultureInfo)
        {
            if (cultureInfo == null)
            {
                throw new ArgumentNullException("cultureInfo");
            }

            if (this.DateType != DateType.Approximate && approximate == true)
            {
                // This is invalid. You cannot make use of the indicateWhenApproximate flag when the
                // Date is not Approximate
                throw new ArgumentOutOfRangeException("approximate", Resources.NhsDateResources.ShowApproxIndicatorInvalidForDateType);
            }

            string formattedDate = null;

            switch (this.dateType)
            {
            case DateType.Exact:
            case DateType.Approximate:

                if (showRelativeDayText)
                {
                    DateTime date = this.DateValue.Date;
                    if (date == System.DateTime.Today)
                    {
                        formattedDate = Resources.NhsDateResources.Today;
                    }
                    else if (date == System.DateTime.Today.AddDays(-1))
                    {
                        formattedDate = Resources.NhsDateResources.Yesterday;
                    }
                    else if (date == System.DateTime.Today.AddDays(1))
                    {
                        formattedDate = Resources.NhsDateResources.Tomorrow;
                    }
                }

                if (formattedDate == null)
                {
                    GlobalizationService gs = new GlobalizationService();
                    formattedDate = this.DateValue.ToString(includeDayOfWeek ? gs.ShortDatePatternWithDayOfWeek : gs.ShortDatePattern, cultureInfo);
                }

                if (this.DateType == DateType.Approximate && approximate)
                {
                    formattedDate = string.Format(
                        cultureInfo,
                        Resources.NhsDateResources.ApproximateDateFormat,
                        Resources.NhsDateResources.Approximate,
                        formattedDate);
                }

                break;

            case DateType.Year:
                formattedDate = this.Year.ToString("0000", cultureInfo);
                break;

            case DateType.YearMonth:
                formattedDate = string.Format(
                    cultureInfo,
                    "{0}-{1}",
                    cultureInfo.DateTimeFormat.MonthNames[this.Month - 1],
                    this.Year.ToString("0000", cultureInfo));
                break;

            case DateType.NullIndex:
                formattedDate = string.Format(cultureInfo, "Null:{0}", this.NullIndex);
                break;

            case DateType.Null:
                formattedDate = string.Empty;
                break;

            default:
                throw new InvalidOperationException();
            }

            return(formattedDate);
        }
コード例 #11
0
ファイル: NhsDate.cs プロジェクト: odnodn/mscui
        /// <summary>
        /// Parses a string that represents a date.
        /// </summary>
        /// <param name="date">A string containing the value to be converted.   </param>
        /// <param name="result">A container for a successfully-parsed date.  </param>
        /// <param name="cultureInfo">The culture that should be used to parse the string.</param>
        /// <returns>True if the date string was successfully parsed; otherwise, false.  </returns>
        /// <remarks>
        ///  If the string can be parsed,
        /// the result parameter is set to an NhsDate object corresponding to
        /// the parsed dateString. If the string cannot be parsed, the result parameter is set to DateTime.MinValue.
        /// </remarks>
        public static bool TryParseExact(string date, out NhsDate result, CultureInfo cultureInfo)
        {
            // check for true null
            if (string.IsNullOrEmpty(date))
            {
                result          = new NhsDate();
                result.DateType = DateType.Null;
                return(true);
            }

            // first check for numeric year month as this can be confused as a date
            Regex numericYearMonthRegEx        = new Regex("^(?<Month>0?[1-9]|10|11|12)[-\\s/](?<Year>\\d{4}|\\d{2})$");
            Match numericYearMonthRegExResults = numericYearMonthRegEx.Match(date);

            if (numericYearMonthRegExResults.Success)
            {
                result          = new NhsDate();
                result.DateType = DateType.YearMonth;
                result.Month    = int.Parse(numericYearMonthRegExResults.Groups["Month"].Value, cultureInfo);
                result.Year     = NhsDate.ParseYear(numericYearMonthRegExResults.Groups["Year"].Value, cultureInfo);

                return(true);
            }

            // check to see if approx indicator is present
            bool approxIndicatorPresent = (date.IndexOf(Resources.NhsDateResources.Approximate, StringComparison.CurrentCultureIgnoreCase) >= 0);

            if (approxIndicatorPresent)
            {
                date = date.Replace(Resources.NhsDateResources.Approximate, string.Empty).Trim();
            }

            // try datetime parse with our standard formats
            GlobalizationService gs = new GlobalizationService();

            string[] standardFormats = new string[]
            {
                gs.ShortDatePattern,
                gs.ShortDatePatternWithDayOfWeek,
            };

            DateTime parsedDateTime;

            if (DateTime.TryParseExact(date, standardFormats, cultureInfo, DateTimeStyles.None, out parsedDateTime))
            {
                result          = new NhsDate(parsedDateTime);
                result.DateType = (approxIndicatorPresent ? DateType.Approximate : DateType.Exact);
                return(true);
            }

            // Check if 'date' is a year and a month
            Regex yearMonthRegEx = BuildMonthYearRegEx(cultureInfo);
            Match yearMonthMatch = yearMonthRegEx.Match(date);

            // Regex doesn't take care of year as zero
            if (yearMonthMatch.Success && NhsDate.ParseYear(yearMonthMatch.Groups["Year"].Value, cultureInfo) > 0)
            {
                int month = GetMonthNumberFromMonthName(yearMonthMatch.Groups["Month"].Value, cultureInfo);
                int year  = NhsDate.ParseYear(yearMonthMatch.Groups["Year"].Value, cultureInfo);
                result = new NhsDate(year, month);
                return(true);
            }

            // Check if 'date' is a year
            Regex yearRegEx = new Regex(@"^(\d{4}|\d{2})$");

            // Regex doesn't take care of year as zero
            if (yearRegEx.IsMatch(date) && NhsDate.ParseYear(date, cultureInfo) > 0)
            {
                result = new NhsDate(NhsDate.ParseYear(date, cultureInfo));
                return(true);
            }

            // Check if 'date' is a Null date
            Regex nullDateRegEx = new Regex(@"^Null:(?<Index>-?\d+)$", RegexOptions.IgnoreCase);

            Match match = nullDateRegEx.Match(date);

            if (match.Success)
            {
                // 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))
                {
                    if (nullIndex >= MinimumNullIndex && nullIndex <= MaximumNullIndex)
                    {
                        result           = new NhsDate();
                        result.NullIndex = nullIndex;
                        result.DateType  = DateType.NullIndex;

                        return(true);
                    }
                }
            }

            // try alternative formats
            string[] alternativeFormats = new string[]
            {
                "d-MMM-yyyy", "d-M-yyyy", "d-MM-yyyy", "d-MMMM-yyyy",
                "d/MMM/yyyy", "d/M/yyyy", "d/MM/yyyy", "d/MMMM/yyyy",
                "d MMM yyyy", "d M yyyy", "d MM yyyy", "d MMMM yyyy",
                "ddd d-MMM-yyyy", "ddd d-M-yyyy", "ddd d-MM-yyyy", "ddd d-MMMM-yyyy",
                "ddd d/MMM/yyyy", "ddd d/M/yyyy", "ddd d/MM/yyyy", "ddd d/MMMM/yyyy",
                "ddd d MMM yyyy", "ddd d M yyyy", "ddd d MM yyyy", "ddd d MMMM yyyy",
                "d-MMM-yy", "d-M-yy", "d-MM-yy", "d-MMMM-yy",
                "d/MMM/yy", "d/M/yy", "d/MM/yy", "d/MMMM/yy",
                "d MMM yy", "d M yy", "d MM yy", "d MMMM yy",
                "ddd d-MMM-yy", "ddd d-M-yy", "ddd d-MM-yy", "ddd d-MMMM-yy",
                "ddd d/MMM/yy", "ddd d/M/yy", "ddd d/MM/yy", "ddd d/MMMM/yy",
                "ddd d MMM yy", "ddd d M yy", "ddd d MM yy", "ddd d MMMM yy"
            };

            if (DateTime.TryParseExact(date, alternativeFormats, cultureInfo, DateTimeStyles.None, out parsedDateTime))
            {
                result          = new NhsDate(parsedDateTime);
                result.DateType = (approxIndicatorPresent ? DateType.Approximate : DateType.Exact);
                return(true);
            }

            // no match
            result = null;
            return(false);
        }
コード例 #12
0
ファイル: NhsTime.cs プロジェクト: odnodn/mscui
        /// <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);
        }
コード例 #13
0
ファイル: TimeTest.cs プロジェクト: rbirkby/mscui
        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");
        }
コード例 #14
0
ファイル: TimeTest.cs プロジェクト: rbirkby/mscui
        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));                                                
        }