In .:
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Metadata.W3cXsd2001 / SoapDuration.cs
1 //
2 // System.Runtime.Remoting.Metadata.W3cXsd2001.SoapDuration
3 //
4 // Authors:
5 //      Martin Willemoes Hansen (mwh@sysrq.dk)
6 //      Lluis Sanchez Gual (lluis@ximian.com)
7 //
8 // (C) 2003 Martin Willemoes Hansen
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Text;
36 using System.Globalization;
37
38 namespace System.Runtime.Remoting.Metadata.W3cXsd2001 
39 {
40         public sealed class SoapDuration
41         {
42                 public SoapDuration()
43                 {
44                 }
45                 
46                 public static string XsdType {
47                         get { return "duration"; }
48                 }
49
50                 public static TimeSpan Parse (string s)
51                 {
52                         if (s.Length == 0)
53                                 throw new ArgumentException ("Invalid format string for duration schema datatype.");
54
55                         int start = 0;
56                         if (s [0] == '-')
57                                 start = 1;
58                         bool minusValue = (start == 1);
59
60                         if (s [start] != 'P')
61                                 throw new ArgumentException ("Invalid format string for duration schema datatype.");
62                         start++;
63
64                         int parseStep = 0;
65                         int days = 0;
66                         bool isTime = false;
67                         int hours = 0;
68                         int minutes = 0;
69                         double seconds = 0;
70
71                         bool error = false;
72
73                         int i = start;
74                         while (i < s.Length) {
75                                 if (s [i] == 'T') {
76                                         isTime = true;
77                                         parseStep = 4;
78                                         i++;
79                                         start = i;
80                                         continue;
81                                 }
82                                 bool isIntegerValue = true;
83                                 int dotOccurence = 0;
84                                 for (; i < s.Length; i++) 
85                                 {
86                                         if (!Char.IsDigit (s [i]))
87                                         {
88                                                 //check if it is a non integer value.
89                                                 if (s[i] == '.') 
90                                                 {
91                                                         isIntegerValue = false;
92                                                         dotOccurence++;
93                                                         //if there is more than one dot in the number 
94                                                         //than its an error
95                                                         if (dotOccurence > 1 )
96                                                         {
97                                                                 error = true;
98                                                                 break;
99                                                         }
100                                                 }
101                                                 else
102                                                         break;
103                                         }
104                                 }
105
106                                 int intValue = -1;
107                                 double doubleValue = -1;
108                                 if (isIntegerValue)
109                                         intValue = int.Parse (s.Substring (start, i - start));
110                                 else
111                                         doubleValue = double.Parse (s.Substring (start, i - start), CultureInfo.InvariantCulture);
112                                 switch (s [i]) {
113                                 case 'Y':
114                                         days += intValue * 365;
115                                         if (parseStep > 0 || !isIntegerValue)
116                                                 error = true;
117                                         else
118                                                 parseStep = 1;
119                                         break;
120                                 case 'M':
121                                         if (parseStep < 2 && isIntegerValue) {
122                                                 days += 365 * (intValue / 12) + 30 * (intValue % 12);
123                                                 parseStep = 2;
124                                         } else if (isTime && parseStep < 6 && isIntegerValue) {
125                                                 minutes = intValue;
126                                                 parseStep = 6;
127                                         }
128                                         else
129                                                 error = true;
130                                         break;
131                                 case 'D':
132                                         days += intValue;
133                                         if (parseStep > 2 || !isIntegerValue)
134                                                 error = true;
135                                         else
136                                                 parseStep = 3;
137                                         break;
138                                 case 'H':
139                                         hours = intValue;
140                                         if (!isTime || parseStep > 4 || !isIntegerValue)
141                                                 error = true;
142                                         else
143                                                 parseStep = 5;
144                                         break;
145                                 case 'S':
146                                         if (isIntegerValue)
147                                                 seconds = intValue;
148                                         else
149                                                 seconds = doubleValue;
150                                         if (!isTime || parseStep > 6)
151                                                 error = true;
152                                         else
153                                                 parseStep = 7;
154                                         break;
155                                 default:
156                                         error = true;
157                                         break;
158                                 }
159                                 if (error)
160                                         break;
161                                 ++i;
162                                 start = i;
163                         }
164                         if (error)
165                                 throw new ArgumentException ("Invalid format string for duration schema datatype.");
166                         TimeSpan ts = new TimeSpan (days, hours, minutes, 0) + TimeSpan.FromSeconds (seconds);
167                         return minusValue ? -ts : ts;
168                 }
169
170                 public static string ToString (TimeSpan value)
171                 {
172                         StringBuilder builder = new StringBuilder();
173                         if (value.Ticks < 0) {
174                                 builder.Append('-');
175                                 value = value.Negate();
176                         }
177                         builder.Append('P');
178                         if (value.Days > 0) builder.Append(value.Days).Append('D');
179                         if (value.Days > 0 || value.Minutes > 0 || value.Seconds > 0 || value.Milliseconds > 0) {
180                                 builder.Append('T');
181                                 if (value.Hours > 0) builder.Append(value.Hours).Append('H');
182                                 if (value.Minutes > 0) builder.Append(value.Minutes).Append('M');
183                                 if (value.Seconds > 0 || value.Milliseconds > 0) {
184                                         double secs = (double) value.Seconds;
185                                         if (value.Milliseconds > 0)
186                                                 secs += ((double)value.Milliseconds) / 1000.0;
187                                         builder.Append(String.Format(CultureInfo.InvariantCulture, "{0:0.0000000}", secs));
188                                         builder.Append('S');
189                                 }
190                         }
191                         return builder.ToString();
192                 }
193         }
194 }