Merge branch 'BigIntegerParse'
[mono.git] / mcs / class / corlib / Test / System.Globalization / DateTimeFormatInfoTest.cs
1 //
2 // DateTimeFormatInfo.cs
3 //
4 // Authors:
5 //     Ben Maurer <bmaurer@andrew.cmu.edu>
6 //
7 // Copyright (C) 2005 Novell (http://www.novell.com)
8 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30
31 using NUnit.Framework;
32 using System;
33 using System.Globalization;
34 using System.IO;
35
36 namespace MonoTests.System.Globalization
37 {
38         [TestFixture]
39         public class DateTimeFormatInfoTest
40         {
41                 [Test]
42                 public void GetAllDateTimePatterns_ret_diff_obj ()
43                 {
44                         // We need to return different objects for security reasons
45                         DateTimeFormatInfo dtfi = CultureInfo.InvariantCulture.DateTimeFormat;
46
47                         string [] one = dtfi.GetAllDateTimePatterns ();
48                         string [] two = dtfi.GetAllDateTimePatterns ();
49                         Assert.IsTrue (one != two);
50                 }
51
52                 [Test]
53                 public void EraName ()
54                 {
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");
63                 }
64
65                 [Test] // bug #332553
66                 public void MonthNames ()
67                 {
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");
84
85                         c.DateTimeFormat.MonthNames = c.DateTimeFormat.MonthNames;
86
87                         string [] monthNamesB = c.DateTimeFormat.MonthNames;
88                         Assert.AreEqual (monthNamesA, monthNamesB, "#B1");
89                         Assert.IsFalse (object.ReferenceEquals (monthNamesA, monthNamesB), "#B2");
90                 }
91
92                 [Test]
93                 public void TestSpecificCultureFormats_es_ES ()
94                 {
95                         CultureInfo ci = new CultureInfo ("es-ES");
96                         DateTimeFormatInfo di = ci.DateTimeFormat;
97                         Assert.AreEqual ("dddd, d' de 'MMMM' de 'yyyy", di.LongDatePattern, "#1");
98                         Assert.AreEqual ("H:mm:ss", di.LongTimePattern, "#2");
99                         Assert.AreEqual ("dddd, d' de 'MMMM' de 'yyyy H:mm:ss", di.FullDateTimePattern, "#3");
100                         Assert.AreEqual ("MMMM' de 'yyyy", di.YearMonthPattern, "#4");
101                         Assert.AreEqual ("d' de 'MMMM", di.MonthDayPattern, "#5");
102                 }
103
104                 [Test]
105                 public void Clone ()
106                 {
107                         DateTimeFormatInfo dfi = (DateTimeFormatInfo) DateTimeFormatInfo.InvariantInfo.Clone ();
108                         dfi.MonthNames[0] = "foo";
109                         dfi.AbbreviatedDayNames[0] = "b1";
110                         dfi.AbbreviatedMonthGenitiveNames[0] = "b2";
111
112
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");
117                 }
118
119                 [Test]
120                 public void MonthGenitiveNames ()
121                 {
122                         var dfi = new CultureInfo ("cs-CZ").DateTimeFormat;
123                         Assert.AreEqual ("ledna", dfi.MonthGenitiveNames[0], "#1");
124                         Assert.AreEqual ("1.", dfi.AbbreviatedMonthGenitiveNames[0], "#2");
125                 }
126
127                 [Test]
128                 public void QuoteInValue ()
129                 {
130                         var culture = new CultureInfo("mt-MT");
131                         Assert.AreEqual ("dddd, d' ta\\' 'MMMM yyyy", culture.DateTimeFormat.LongDatePattern);
132                 }
133
134 #if !TARGET_JVM
135                 [Test]
136                 public void Bug78569 ()
137                 {
138                         DateTime dt = DateTime.Now;
139                         CultureInfo ci = new CultureInfo ("en-GB");
140                         string s = dt.ToString (ci);
141                         DateTime dt2 = DateTime.Parse (s, ci);
142                         Assert.AreEqual (dt.Month, dt2.Month);
143                 }
144 #endif
145         }
146 }
147
148