Add a third AOT location, alongside ~/.mono/aot-cache and the assembly dir
[mono.git] / mcs / class / System.Core / System / TimeZoneInfo.Serialization.cs
1 /*
2  * System.TimeZoneInfo.Serialization
3  *
4  * Author(s)
5  *      Sasha Kotlyar <sasha@arktronic.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  * 
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27 #if (INSIDE_CORLIB && NET_4_0) || (!INSIDE_CORLIB && (NET_3_5 && !NET_4_0 && !MOBILE))
28
29 using System.Collections.Generic;
30 using System.Globalization;
31 using System.Runtime.Serialization;
32 using System.Text;
33
34 namespace System
35 {
36         public partial class TimeZoneInfo
37         {
38                 public static TimeZoneInfo FromSerializedString (string source)
39                 {
40                         var input = new StringBuilder (source);
41                         var tzId = DeserializeString (ref input);
42                         var offset = DeserializeInt (ref input);
43                         var displayName = DeserializeString (ref input);
44                         var standardName = DeserializeString (ref input);
45                         var daylightName = DeserializeString (ref input);
46                         var rules = new List<TimeZoneInfo.AdjustmentRule> ();
47                         while (input [0] != ';') {
48                                 rules.Add (DeserializeAdjustmentRule (ref input));
49                         }
50                         var offsetSpan = TimeSpan.FromMinutes (offset);
51                         return TimeZoneInfo.CreateCustomTimeZone (tzId, offsetSpan, displayName, standardName, daylightName, rules.ToArray ());
52                 }
53
54                 public string ToSerializedString ()
55                 {
56                         var stb = new StringBuilder ();
57                         var daylightName = (string.IsNullOrEmpty(this.DaylightName) ? this.StandardName : this.DaylightName);
58                         stb.AppendFormat ("{0};{1};{2};{3};{4};", EscapeForSerialization (this.Id), (int)this.BaseUtcOffset.TotalMinutes,
59                                 EscapeForSerialization (this.DisplayName), EscapeForSerialization (this.StandardName), EscapeForSerialization (daylightName));
60
61                         if (this.SupportsDaylightSavingTime) {
62                                 foreach (var rule in this.GetAdjustmentRules()) {
63                                         var start = rule.DateStart.ToString ("MM:dd:yyyy", CultureInfo.InvariantCulture);
64                                         var end = rule.DateEnd.ToString ("MM:dd:yyyy", CultureInfo.InvariantCulture);
65                                         var delta = (int)rule.DaylightDelta.TotalMinutes;
66                                         var transitionStart = SerializeTransitionTime (rule.DaylightTransitionStart);
67                                         var transitionEnd = SerializeTransitionTime (rule.DaylightTransitionEnd);
68                                         stb.AppendFormat ("[{0};{1};{2};{3};{4};]", start, end, delta,
69                                                 transitionStart, transitionEnd);
70                                 }
71                         }
72
73                         stb.Append (";");
74                         return stb.ToString ();
75                 }
76
77                 private static TimeZoneInfo.AdjustmentRule DeserializeAdjustmentRule (ref StringBuilder input)
78                 {
79                         // Similar to: [01:01:0001;12:31:9999;60;[0;01:00:00;3;5;0;];[0;02:00:00;10;5;0;];]
80                         if (input [0] != '[')
81                                 throw new SerializationException ();
82                         input.Remove (0, 1); // [
83                         var dateStart = DeserializeDate (ref input);
84                         var dateEnd = DeserializeDate (ref input);
85                         var delta = DeserializeInt (ref input);
86                         var transitionStart = DeserializeTransitionTime (ref input);
87                         var transitionEnd = DeserializeTransitionTime (ref input);
88                         input.Remove (0, 1); // ]
89                         var deltaSpan = TimeSpan.FromMinutes (delta);
90                         return TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (dateStart, dateEnd, deltaSpan,
91                                 transitionStart, transitionEnd);
92                 }
93
94                 private static TimeZoneInfo.TransitionTime DeserializeTransitionTime (ref StringBuilder input)
95                 {
96                         if (input [0] != '[' || (input [1] != '0' && input [1] != '1') || input [2] != ';')
97                                 throw new SerializationException ();
98                         var rule = input [1];
99                         input.Remove (0, 3); // [#;
100                         var timeOfDay = DeserializeTime (ref input);
101                         var month = DeserializeInt (ref input);
102                         if (rule == '0') {
103                                 // Floating rule such as: [0;01:00:00;3;5;0;];
104                                 var week = DeserializeInt (ref input);
105                                 var dayOfWeek = DeserializeInt (ref input);
106                                 input.Remove (0, 2); // ];
107                                 return TimeZoneInfo.TransitionTime.CreateFloatingDateRule (timeOfDay, month, week, (DayOfWeek)dayOfWeek);
108                         }
109
110                         // Fixed rule such as: [1;02:15:59.999;6;2;];
111                         var day = DeserializeInt (ref input);
112                         input.Remove (0, 2); // ];
113                         return TimeZoneInfo.TransitionTime.CreateFixedDateRule (timeOfDay, month, day);
114                 }
115
116                 private static string DeserializeString (ref StringBuilder input)
117                 {
118                         var stb = new StringBuilder ();
119                         var isEscaped = false;
120                         int charCount;
121                         for (charCount = 0; charCount < input.Length; charCount++) {
122                                 var inChar = input [charCount];
123                                 if (isEscaped) {
124                                         isEscaped = false;
125                                         stb.Append (inChar);
126                                 } else if (inChar == '\\') {
127                                         isEscaped = true;
128                                         continue;
129                                 } else if (inChar == ';') {
130                                         break;
131                                 } else {
132                                         stb.Append (inChar);
133                                 }
134                         }
135                         input.Remove (0, charCount + 1);
136                         return stb.ToString ();
137                 }
138
139                 private static int DeserializeInt(ref StringBuilder input)
140                 {
141                         int charCount = 0;
142                         while(charCount++ < input.Length)
143                         {
144                                 if (input[charCount] == ';')
145                                         break;
146                         }
147                         int result;
148                         if(!int.TryParse(input.ToString(0, charCount), NumberStyles.Integer, CultureInfo.InvariantCulture, out result))
149                                 throw new SerializationException();
150                         input.Remove(0, charCount + 1);
151                         return result;
152                 }
153
154                 private static DateTime DeserializeDate (ref StringBuilder input)
155                 {
156                         var inChars = new char[11];
157                         input.CopyTo (0, inChars, 0, inChars.Length);
158                         DateTime result;
159                         if (!DateTime.TryParseExact (new string (inChars), "MM:dd:yyyy;", CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
160                                 throw new SerializationException ();
161                         input.Remove (0, inChars.Length);
162                         return result;
163                 }
164
165                 private static DateTime DeserializeTime (ref StringBuilder input)
166                 {
167                         if (input [8] == ';') {
168                                 // Without milliseconds
169                                 var inChars = new char[9];
170                                 input.CopyTo (0, inChars, 0, inChars.Length);
171                                 DateTime result;
172                                 if (!DateTime.TryParseExact (new string (inChars), "HH:mm:ss;", CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out result))
173                                         throw new SerializationException ();
174                                 input.Remove (0, inChars.Length);
175                                 return result;
176                         } else if (input [12] == ';') {
177                                 // With milliseconds
178                                 char[] inChars = new char[13];
179                                 input.CopyTo (0, inChars, 0, inChars.Length);
180                                 var inString = new string (inChars);
181                                 DateTime result;
182                                 if (!DateTime.TryParseExact (inString, "HH:mm:ss.fff;", CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out result))
183                                         throw new SerializationException ();
184                                 input.Remove (0, inChars.Length);
185                                 return result;
186                         }
187                         throw new SerializationException ();
188                 }
189
190                 private static string EscapeForSerialization (string unescaped)
191                 {
192                         return unescaped.Replace (@"\", @"\\").Replace (";", "\\;");
193                 }
194
195                 private static string SerializeTransitionTime (TimeZoneInfo.TransitionTime transition)
196                 {
197                         string timeOfDay;
198                         if (transition.TimeOfDay.Millisecond > 0)
199                                 timeOfDay = transition.TimeOfDay.ToString ("HH:mm:ss.fff");
200                         else
201                                 timeOfDay = transition.TimeOfDay.ToString ("HH:mm:ss");
202
203                         if (transition.IsFixedDateRule) {
204                                 return string.Format ("[1;{0};{1};{2};]", timeOfDay, transition.Month, transition.Day);
205                         }
206
207                         return string.Format ("[0;{0};{1};{2};{3};]", timeOfDay, transition.Month,
208                                 transition.Week, (int)transition.DayOfWeek);
209                 }
210         }
211 }
212
213 #endif