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 ("A.D.", dtfi.GetEraName (1), "#2");
59 Assert.AreEqual (1, dtfi.GetEra ("A.D."), "#3");
60 Assert.AreEqual (1, dtfi.GetEra ("AD"), "#4");
61 Assert.AreEqual (-1, dtfi.GetEra ("C.E"), "#5");
62 Assert.AreEqual (-1, dtfi.GetEra ("Common Era"), "#6");
66 public void MonthNames ()
68 CultureInfo c = CultureInfo.CreateSpecificCulture ("en");
69 string [] monthNamesA = c.DateTimeFormat.MonthNames;
70 Assert.AreEqual (13, monthNamesA.Length, "#A1");
71 Assert.AreEqual ("January", monthNamesA [0], "#A2");
72 Assert.AreEqual ("February", monthNamesA [1], "#A3");
73 Assert.AreEqual ("March", monthNamesA [2], "#A4");
74 Assert.AreEqual ("April", monthNamesA [3], "#A5");
75 Assert.AreEqual ("May", monthNamesA [4], "#A6");
76 Assert.AreEqual ("June", monthNamesA [5], "#A7");
77 Assert.AreEqual ("July", monthNamesA [6], "#A8");
78 Assert.AreEqual ("August", monthNamesA [7], "#A9");
79 Assert.AreEqual ("September", monthNamesA [8], "#A10");
80 Assert.AreEqual ("October", monthNamesA [9], "#A11");
81 Assert.AreEqual ("November", monthNamesA [10], "#A12");
82 Assert.AreEqual ("December", monthNamesA [11], "#A13");
83 Assert.AreEqual (string.Empty, monthNamesA [12], "#A14");
85 c.DateTimeFormat.MonthNames = c.DateTimeFormat.MonthNames;
87 string [] monthNamesB = c.DateTimeFormat.MonthNames;
88 Assert.AreEqual (monthNamesA, monthNamesB, "#B1");
89 Assert.IsFalse (object.ReferenceEquals (monthNamesA, monthNamesB), "#B2");
93 public void TestSpecificCultureFormats_es_ES ()
95 CultureInfo ci = new CultureInfo ("es-ES");
96 DateTimeFormatInfo di = ci.DateTimeFormat;
97 Assert.AreEqual ("dddd, dd' de 'MMMM' de 'yyyy", di.LongDatePattern, "#1");
98 Assert.AreEqual ("H:mm:ss", di.LongTimePattern, "#2");
99 Assert.AreEqual ("dddd, dd' de 'MMMM' de 'yyyy H:mm:ss", di.FullDateTimePattern, "#3");
100 Assert.AreEqual ("MMMM' de 'yyyy", di.YearMonthPattern, "#4");
101 Assert.AreEqual ("dd MMMM", di.MonthDayPattern, "#5");
107 DateTimeFormatInfo dfi = (DateTimeFormatInfo) DateTimeFormatInfo.InvariantInfo.Clone ();
108 dfi.MonthNames[0] = "foo";
109 dfi.AbbreviatedDayNames[0] = "b1";
110 dfi.AbbreviatedMonthGenitiveNames[0] = "b2";
113 Assert.IsFalse (dfi.IsReadOnly, "#0");
114 Assert.AreEqual ("January", DateTimeFormatInfo.InvariantInfo.MonthNames [0], "#1");
115 Assert.AreEqual ("Sun", DateTimeFormatInfo.InvariantInfo.AbbreviatedDayNames[0], "#2");
116 Assert.AreEqual ("Jan", DateTimeFormatInfo.InvariantInfo.AbbreviatedMonthGenitiveNames[0], "#3");
120 public void MonthGenitiveNames ()
122 var dfi = new CultureInfo ("cs-CZ").DateTimeFormat;
123 Assert.AreEqual ("ledna", dfi.MonthGenitiveNames[0], "#1");
128 public void Bug78569 ()
130 DateTime dt = DateTime.Now;
131 CultureInfo ci = new CultureInfo ("en-GB");
132 string s = dt.ToString (ci);
133 DateTime dt2 = DateTime.Parse (s, ci);
134 Assert.AreEqual (dt.Month, dt2.Month);