2 // DateTimeFormatInfo.cs
5 // Ben Maurer <bmaurer@andrew.cmu.edu>
7 // Copyright (C) 2005 Novell (http://www.novell.com)
8 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using NUnit.Framework;
33 using System.Globalization;
36 namespace MonoTests.System.Globalization
39 public class DateTimeFormatInfoTest
42 public void GetAllDateTimePatterns_ret_diff_obj ()
44 // We need to return different objects for security reasons
45 DateTimeFormatInfo dtfi = CultureInfo.InvariantCulture.DateTimeFormat;
47 string [] one = dtfi.GetAllDateTimePatterns ();
48 string [] two = dtfi.GetAllDateTimePatterns ();
49 Assert.IsTrue (one != two);
53 public void EraName ()
55 CultureInfo en_US = new CultureInfo ("en-US");
56 DateTimeFormatInfo dtfi = en_US.DateTimeFormat;
57 Assert.AreEqual ("AD", dtfi.GetAbbreviatedEraName (0), "#1");
58 Assert.AreEqual ("AD", dtfi.GetAbbreviatedEraName (1), "#7");
59 Assert.AreEqual ("A.D.", dtfi.GetEraName (0), "#8");
60 Assert.AreEqual ("A.D.", dtfi.GetEraName (1), "#2");
61 Assert.AreEqual (1, dtfi.GetEra ("A.D."), "#3");
62 Assert.AreEqual (1, dtfi.GetEra ("AD"), "#4");
63 Assert.AreEqual (-1, dtfi.GetEra ("C.E"), "#5");
64 Assert.AreEqual (-1, dtfi.GetEra ("Common Era"), "#6");
68 public void MonthNames ()
70 CultureInfo c = CultureInfo.CreateSpecificCulture ("en");
71 string [] monthNamesA = c.DateTimeFormat.MonthNames;
72 Assert.AreEqual (13, monthNamesA.Length, "#A1");
73 Assert.AreEqual ("January", monthNamesA [0], "#A2");
74 Assert.AreEqual ("February", monthNamesA [1], "#A3");
75 Assert.AreEqual ("March", monthNamesA [2], "#A4");
76 Assert.AreEqual ("April", monthNamesA [3], "#A5");
77 Assert.AreEqual ("May", monthNamesA [4], "#A6");
78 Assert.AreEqual ("June", monthNamesA [5], "#A7");
79 Assert.AreEqual ("July", monthNamesA [6], "#A8");
80 Assert.AreEqual ("August", monthNamesA [7], "#A9");
81 Assert.AreEqual ("September", monthNamesA [8], "#A10");
82 Assert.AreEqual ("October", monthNamesA [9], "#A11");
83 Assert.AreEqual ("November", monthNamesA [10], "#A12");
84 Assert.AreEqual ("December", monthNamesA [11], "#A13");
85 Assert.AreEqual (string.Empty, monthNamesA [12], "#A14");
87 c.DateTimeFormat.MonthNames = c.DateTimeFormat.MonthNames;
89 string [] monthNamesB = c.DateTimeFormat.MonthNames;
90 Assert.AreEqual (monthNamesA, monthNamesB, "#B1");
91 Assert.IsFalse (object.ReferenceEquals (monthNamesA, monthNamesB), "#B2");
95 public void TestSpecificCultureFormats_es_ES ()
97 CultureInfo ci = new CultureInfo ("es-ES");
98 DateTimeFormatInfo di = ci.DateTimeFormat;
99 Assert.AreEqual ("dddd, d' de 'MMMM' de 'yyyy", di.LongDatePattern, "#1");
100 Assert.AreEqual ("H:mm:ss", di.LongTimePattern, "#2");
101 Assert.AreEqual ("dddd, d' de 'MMMM' de 'yyyy H:mm:ss", di.FullDateTimePattern, "#3");
102 Assert.AreEqual ("MMMM' de 'yyyy", di.YearMonthPattern, "#4");
103 Assert.AreEqual ("d' de 'MMMM", di.MonthDayPattern, "#5");
109 DateTimeFormatInfo dfi = (DateTimeFormatInfo) DateTimeFormatInfo.InvariantInfo.Clone ();
110 dfi.MonthNames[0] = "foo";
111 dfi.AbbreviatedDayNames[0] = "b1";
112 dfi.AbbreviatedMonthGenitiveNames[0] = "b2";
115 Assert.IsFalse (dfi.IsReadOnly, "#0");
116 Assert.AreEqual ("January", DateTimeFormatInfo.InvariantInfo.MonthNames [0], "#1");
117 Assert.AreEqual ("Sun", DateTimeFormatInfo.InvariantInfo.AbbreviatedDayNames[0], "#2");
118 Assert.AreEqual ("Jan", DateTimeFormatInfo.InvariantInfo.AbbreviatedMonthGenitiveNames[0], "#3");
122 public void MonthGenitiveNames ()
124 var dfi = new CultureInfo ("cs-CZ").DateTimeFormat;
125 Assert.AreEqual ("ledna", dfi.MonthGenitiveNames[0], "#1");
126 Assert.AreEqual ("led", dfi.AbbreviatedMonthGenitiveNames[0], "#2");
130 public void QuoteInValue ()
132 var culture = new CultureInfo("mt-MT");
133 Assert.AreEqual ("dddd, d' ta\\' 'MMMM yyyy", culture.DateTimeFormat.LongDatePattern);
137 public void Bug78569 ()
139 DateTime dt = DateTime.Now;
140 CultureInfo ci = new CultureInfo ("en-GB");
141 string s = dt.ToString (ci);
142 DateTime dt2 = DateTime.Parse (s, ci);
143 Assert.AreEqual (dt.Month, dt2.Month);