public void EraName()
        {
            CultureInfo        en_US = new CultureInfo("en-US");
            DateTimeFormatInfo dtfi  = en_US.DateTimeFormat;

            Assert.AreEqual("AD", dtfi.GetAbbreviatedEraName(0), "#1");
            Assert.AreEqual("AD", dtfi.GetAbbreviatedEraName(1), "#7");
            Assert.AreEqual("A.D.", dtfi.GetEraName(0), "#8");
            Assert.AreEqual("A.D.", dtfi.GetEraName(1), "#2");
            Assert.AreEqual(1, dtfi.GetEra("A.D."), "#3");
            Assert.AreEqual(1, dtfi.GetEra("AD"), "#4");
            Assert.AreEqual(-1, dtfi.GetEra("C.E"), "#5");
            Assert.AreEqual(-1, dtfi.GetEra("Common Era"), "#6");
        }
Example #2
0
                                   object> eraData(DateTimeFormatInfo date)
        {
            int[] eras = date.Calendar.Eras;
            Dictionary <string,
                        object> dictionary = new Dictionary <string,
                                                             object>();

            foreach (int era in eras)
            {
                dictionary.Add(era.ToString(), date.GetAbbreviatedEraName(era));
            }
            return(dictionary);
        }
Example #3
0
    public static void Main()
    {
        DateTime date1 = new DateTime(2011, 8, 28);
        Calendar cal   = new JapaneseLunisolarCalendar();

        Console.WriteLine("{0} {1:d4}/{2:d2}/{3:d2}",
                          cal.GetEra(date1),
                          cal.GetYear(date1),
                          cal.GetMonth(date1),
                          cal.GetDayOfMonth(date1));

        // Display eras
        CultureInfo        culture = CultureInfo.CreateSpecificCulture("ja-JP");
        DateTimeFormatInfo dtfi    = culture.DateTimeFormat;

        dtfi.Calendar = new JapaneseCalendar();

        Console.WriteLine("{0} {1:d4}/{2:d2}/{3:d2}",
                          dtfi.GetAbbreviatedEraName(cal.GetEra(date1)),
                          cal.GetYear(date1),
                          cal.GetMonth(date1),
                          cal.GetDayOfMonth(date1));
    }
Example #4
0
        private void ValidateDTFI(CultureInfo ci)
        {
            DateTimeFormatInfo dtfi = ci.DateTimeFormat;
            Calendar           cal  = dtfi.Calendar;
            int calId = GetCalendarId(cal);

            Assert.Equal(GetDayNames(ci, calId, CAL_SABBREVDAYNAME1), dtfi.AbbreviatedDayNames);
            Assert.Equal(GetDayNames(ci, calId, CAL_SDAYNAME1), dtfi.DayNames);
            Assert.Equal(GetMonthNames(ci, calId, CAL_SMONTHNAME1), dtfi.MonthNames);
            Assert.Equal(GetMonthNames(ci, calId, CAL_SABBREVMONTHNAME1), dtfi.AbbreviatedMonthNames);
            Assert.Equal(GetMonthNames(ci, calId, CAL_SMONTHNAME1 | LOCALE_RETURN_GENITIVE_NAMES), dtfi.MonthGenitiveNames);
            Assert.Equal(GetMonthNames(ci, calId, CAL_SABBREVMONTHNAME1 | LOCALE_RETURN_GENITIVE_NAMES), dtfi.AbbreviatedMonthGenitiveNames);
            Assert.Equal(GetDayNames(ci, calId, CAL_SSHORTESTDAYNAME1), dtfi.ShortestDayNames);
            Assert.Equal(GetLocaleInfo(ci, LOCALE_S1159), dtfi.AMDesignator, StringComparer.OrdinalIgnoreCase);
            Assert.Equal(GetLocaleInfo(ci, LOCALE_S2359), dtfi.PMDesignator, StringComparer.OrdinalIgnoreCase);

            Assert.Equal(calId, GetDefaultcalendar(ci));

            Assert.Equal((int)dtfi.FirstDayOfWeek, ConvertFirstDayOfWeekMonToSun(GetLocaleInfoAsInt(ci, LOCALE_IFIRSTDAYOFWEEK)));
            Assert.Equal((int)dtfi.CalendarWeekRule, GetLocaleInfoAsInt(ci, LOCALE_IFIRSTWEEKOFYEAR));
            Assert.Equal(dtfi.MonthDayPattern, GetCalendarInfo(ci, calId, CAL_SMONTHDAY, true));
            Assert.Equal("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'", dtfi.RFC1123Pattern);
            Assert.Equal("yyyy'-'MM'-'dd'T'HH':'mm':'ss", dtfi.SortableDateTimePattern);
            Assert.Equal("yyyy'-'MM'-'dd HH':'mm':'ss'Z'", dtfi.UniversalSortableDateTimePattern);

            string longDatePattern1     = GetCalendarInfo(ci, calId, CAL_SLONGDATE)[0];
            string longDatePattern2     = ReescapeWin32String(GetLocaleInfo(ci, LOCALE_SLONGDATE));
            string longTimePattern1     = GetTimeFormats(ci, 0)[0];
            string longTimePattern2     = ReescapeWin32String(GetLocaleInfo(ci, LOCALE_STIMEFORMAT));
            string fullDateTimePattern  = longDatePattern1 + " " + longTimePattern1;
            string fullDateTimePattern1 = longDatePattern2 + " " + longTimePattern2;

            Assert.Contains(dtfi.FullDateTimePattern, new[] { fullDateTimePattern, fullDateTimePattern1 });
            Assert.Contains(dtfi.LongDatePattern, new[] { longDatePattern1, longDatePattern2 });
            Assert.Contains(dtfi.LongTimePattern, new[] { longTimePattern1, longTimePattern2 });

            Assert.Contains(dtfi.ShortTimePattern, new[] { GetTimeFormats(ci, TIME_NOSECONDS)[0], ReescapeWin32String(GetLocaleInfo(ci, LOCALE_SSHORTTIME)) });
            Assert.Contains(dtfi.ShortDatePattern, new[] { GetCalendarInfo(ci, calId, CAL_SSHORTDATE)[0], ReescapeWin32String(GetLocaleInfo(ci, LOCALE_SSHORTDATE)) });

            Assert.Contains(dtfi.YearMonthPattern, new[] { GetCalendarInfo(ci, calId, CAL_SYEARMONTH)[0], ReescapeWin32String(GetLocaleInfo(ci, LOCALE_SYEARMONTH)) });

            int eraNameIndex = 1;

            Assert.All(GetCalendarInfo(ci, calId, CAL_SERASTRING), eraName => Assert.Equal(dtfi.GetEraName(eraNameIndex++), eraName, StringComparer.OrdinalIgnoreCase));
            eraNameIndex = 1;
            Assert.All(GetCalendarInfo(ci, calId, CAL_SABBREVERASTRING), eraName => Assert.Equal(dtfi.GetAbbreviatedEraName(eraNameIndex++), eraName, StringComparer.OrdinalIgnoreCase));
        }
        private void VerificationHelper(DateTimeFormatInfo info, int era, string expected)
        {
            string actual = info.GetAbbreviatedEraName(era);

            Assert.Equal(expected, actual);
        }
 public void GetAbbreviatedEraName_Invoke_ReturnsExpected(DateTimeFormatInfo format, int era, string expected)
 {
     Assert.Equal(expected, format.GetAbbreviatedEraName(era));
 }
Example #7
0
        private void ValidateDTFI(CultureInfo ci)
        {
            DateTimeFormatInfo dtfi = ci.DateTimeFormat;
            Calendar           cal  = dtfi.Calendar;
            int calId = GetCalendarId(cal);

            Assert.Equal <string>(GetDayNames(ci, calId, CAL_SABBREVDAYNAME1), dtfi.AbbreviatedDayNames);
            Assert.Equal <string>(GetDayNames(ci, calId, CAL_SDAYNAME1), dtfi.DayNames);
            Assert.Equal <string>(GetMonthNames(ci, calId, CAL_SMONTHNAME1), dtfi.MonthNames);
            Assert.Equal <string>(GetMonthNames(ci, calId, CAL_SABBREVMONTHNAME1), dtfi.AbbreviatedMonthNames);
            Assert.Equal <string>(GetMonthNames(ci, calId, CAL_SMONTHNAME1 | LOCALE_RETURN_GENITIVE_NAMES), dtfi.MonthGenitiveNames);
            Assert.Equal <string>(GetMonthNames(ci, calId, CAL_SABBREVMONTHNAME1 | LOCALE_RETURN_GENITIVE_NAMES), dtfi.AbbreviatedMonthGenitiveNames);
            Assert.Equal <string>(GetDayNames(ci, calId, CAL_SSHORTESTDAYNAME1), dtfi.ShortestDayNames);

            Assert.True(GetLocaleInfo(ci, LOCALE_S1159).Equals(dtfi.AMDesignator, StringComparison.OrdinalIgnoreCase), String.Format("Failed with AMDesignator for culture {0}", ci));
            Assert.True(GetLocaleInfo(ci, LOCALE_S2359).Equals(dtfi.PMDesignator, StringComparison.OrdinalIgnoreCase), String.Format("Failed with PMDesignator for culture {0}", ci));

            Assert.True(GetDefaultcalendar(ci) == calId, String.Format("Default calendar not matching for culture {0}. we got {1} and expected {2}", ci, GetDefaultcalendar(ci), calId));

            int dayOfWeek = ConvertFirstDayOfWeekMonToSun(GetLocaleInfoAsInt(ci, LOCALE_IFIRSTDAYOFWEEK));

            Assert.True(dayOfWeek == (int)dtfi.FirstDayOfWeek, String.Format("FirstDayOfWeek is not matching for culture {0}. we got {1} and expected {2}", ci, dayOfWeek, dtfi.FirstDayOfWeek));
            Assert.True(GetLocaleInfoAsInt(ci, LOCALE_IFIRSTWEEKOFYEAR) == (int)dtfi.CalendarWeekRule, String.Format("CalendarWeekRule is not matching for culture {0}. we got {1} and expected {2}", ci, GetLocaleInfoAsInt(ci, LOCALE_IFIRSTWEEKOFYEAR), dtfi.CalendarWeekRule));
            string monthDay = GetCalendarInfo(ci, calId, CAL_SMONTHDAY, true);

            Assert.True(monthDay == dtfi.MonthDayPattern, String.Format("MonthDayPattern is not matching for culture {0}. we got '{1}' and expected '{2}'", ci, monthDay, dtfi.MonthDayPattern));
            string rfc1123Pattern = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";

            Assert.True(rfc1123Pattern == dtfi.RFC1123Pattern, String.Format("RFC1123Pattern is not matching for culture {0}. we got '{1}' and expected '{2}'", ci, dtfi.RFC1123Pattern, rfc1123Pattern));
            string sortableDateTimePattern = "yyyy'-'MM'-'dd'T'HH':'mm':'ss";

            Assert.True(sortableDateTimePattern == dtfi.SortableDateTimePattern, String.Format("SortableDateTimePattern is not matching for culture {0}. we got '{1}' and expected '{2}'", ci, dtfi.SortableDateTimePattern, sortableDateTimePattern));
            string universalSortableDateTimePattern = "yyyy'-'MM'-'dd HH':'mm':'ss'Z'";

            Assert.True(universalSortableDateTimePattern == dtfi.UniversalSortableDateTimePattern, String.Format("SortableDateTimePattern is not matching for culture {0}. we got '{1}' and expected '{2}'", ci, dtfi.UniversalSortableDateTimePattern, universalSortableDateTimePattern));

            string longDatePattern1     = GetCalendarInfo(ci, calId, CAL_SLONGDATE)[0];
            string longDatePattern2     = ReescapeWin32String(GetLocaleInfo(ci, LOCALE_SLONGDATE));
            string longTimePattern1     = GetTimeFormats(ci, 0)[0];
            string longTimePattern2     = ReescapeWin32String(GetLocaleInfo(ci, LOCALE_STIMEFORMAT));
            string fullDateTimePattern  = longDatePattern1 + " " + longTimePattern1;
            string fullDateTimePattern1 = longDatePattern2 + " " + longTimePattern2;

            Assert.True(fullDateTimePattern == dtfi.FullDateTimePattern || fullDateTimePattern1 == dtfi.FullDateTimePattern, String.Format("FullDateTimePattern is not matching for culture {0}. we got '{1}' or '{2}' and expected '{3}'", ci, fullDateTimePattern, fullDateTimePattern1, dtfi.FullDateTimePattern));
            Assert.True(longDatePattern1 == dtfi.LongDatePattern || longDatePattern2 == dtfi.LongDatePattern, String.Format("LongDatePattern is not matching for culture {0}. we got '{1}' or '{2}' and expected '{3}'", ci, longDatePattern1, longDatePattern2, dtfi.LongDatePattern));
            Assert.True(longTimePattern1 == dtfi.LongTimePattern || longTimePattern2 == dtfi.LongTimePattern, String.Format("LongTimePattern is not matching for culture {0}. we got '{1}' or '{2}' and expected '{3}'", ci, longTimePattern1, longTimePattern1, dtfi.LongTimePattern));

            string shortTimePattern1 = GetTimeFormats(ci, TIME_NOSECONDS)[0];
            string shortTimePattern2 = ReescapeWin32String(GetLocaleInfo(ci, LOCALE_SSHORTTIME));

            Assert.True(shortTimePattern1 == dtfi.ShortTimePattern || shortTimePattern2 == dtfi.ShortTimePattern, String.Format("ShortTimePattern is not matching for culture {0}. we got '{1}' or '{2}' and expected '{3}'", ci, shortTimePattern1, shortTimePattern1, dtfi.ShortTimePattern));

            string shortDatePattern1 = GetCalendarInfo(ci, calId, CAL_SSHORTDATE)[0];
            string shortDatePattern2 = ReescapeWin32String(GetLocaleInfo(ci, LOCALE_SSHORTDATE));

            Assert.True(shortDatePattern1 == dtfi.ShortDatePattern || shortDatePattern2 == dtfi.ShortDatePattern, String.Format("LongDatePattern is not matching for culture {0}. we got '{1}' or '{2}' and expected '{3}'", ci, shortDatePattern1, shortDatePattern2, dtfi.ShortDatePattern));

            string yearMonthPattern1 = GetCalendarInfo(ci, calId, CAL_SYEARMONTH)[0];
            string yearMonthPattern2 = ReescapeWin32String(GetLocaleInfo(ci, LOCALE_SYEARMONTH));

            Assert.True(yearMonthPattern1 == dtfi.YearMonthPattern || yearMonthPattern2 == dtfi.YearMonthPattern, String.Format("YearMonthPattern is not matching for culture {0}. we got '{1}' or '{2}' and expected '{3}'", ci, yearMonthPattern1, yearMonthPattern2, dtfi.YearMonthPattern));

            string[] eraNames = GetCalendarInfo(ci, calId, CAL_SERASTRING);
            Assert.True(eraNames[0].Equals(dtfi.GetEraName(1), StringComparison.OrdinalIgnoreCase), String.Format("Era 1 Name with culture {0} and calendar {1} is wrong. got {2} and expected {3}", ci, cal, dtfi.GetEraName(1), eraNames[0]));

            if (cal is JapaneseCalendar)
            {
                Assert.True(eraNames[1].Equals(dtfi.GetEraName(2), StringComparison.OrdinalIgnoreCase), String.Format("Era 2 Name with culture {0} and calendar {1} is wrong. got {2} and expected {3}", ci, cal, dtfi.GetEraName(2), eraNames[1]));
                Assert.True(eraNames[2].Equals(dtfi.GetEraName(3), StringComparison.OrdinalIgnoreCase), String.Format("Era 3 Name with culture {0} and calendar {1} is wrong. got {2} and expected {3}", ci, cal, dtfi.GetEraName(3), eraNames[2]));
                Assert.True(eraNames[3].Equals(dtfi.GetEraName(4), StringComparison.OrdinalIgnoreCase), String.Format("Era 4 Name with culture {0} and calendar {1} is wrong. got {2} and expected {3}", ci, cal, dtfi.GetEraName(4), eraNames[3]));
            }

            string[] abbreviatedEraNames = GetCalendarInfo(ci, calId, CAL_SABBREVERASTRING);
            Assert.True(abbreviatedEraNames[0].Equals(dtfi.GetAbbreviatedEraName(1), StringComparison.OrdinalIgnoreCase), String.Format("Abbreviated Era 1 Name with culture {0} and calendar {1} is wrong. got {2} and expected {3}", ci, cal, dtfi.GetAbbreviatedEraName(1), abbreviatedEraNames[0]));

            if (cal is JapaneseCalendar)
            {
                Assert.True(abbreviatedEraNames[1].Equals(dtfi.GetAbbreviatedEraName(2), StringComparison.OrdinalIgnoreCase), String.Format("Abbreviated Era 1 Name with culture {0} and calendar {1} is wrong. got {2} and expected {3}", ci, cal, dtfi.GetAbbreviatedEraName(2), abbreviatedEraNames[1]));
                Assert.True(abbreviatedEraNames[2].Equals(dtfi.GetAbbreviatedEraName(3), StringComparison.OrdinalIgnoreCase), String.Format("Abbreviated Era 1 Name with culture {0} and calendar {1} is wrong. got {2} and expected {3}", ci, cal, dtfi.GetAbbreviatedEraName(3), abbreviatedEraNames[2]));
                Assert.True(abbreviatedEraNames[3].Equals(dtfi.GetAbbreviatedEraName(4), StringComparison.OrdinalIgnoreCase), String.Format("Abbreviated Era 1 Name with culture {0} and calendar {1} is wrong. got {2} and expected {3}", ci, cal, dtfi.GetAbbreviatedEraName(4), abbreviatedEraNames[3]));
            }
        }
 public void GetAbbreviatedEraName(DateTimeFormatInfo format, int era, string expected)
 {
     Assert.Equal(expected, format.GetAbbreviatedEraName(era));
 }