KeystrokeEsbNet /Source/ESB/Base/ThirdParty/Mvp.Xml-src-2.0/EXSLT/v2/src/Exslt/ExsltDatesAndTimes.cs

Language C# Lines 2355
MD5 Hash 1806da889beb7cb4a3eb959b889031de Estimated Cost $42,312 (why?)
Repository https://KeystrokeEsbNet.svn.codeplex.com/svn View Raw File
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
#region using

using System;
using System.Globalization;
using System.Xml;
using System.Xml.XPath;
using System.Text;
using System.Text.RegularExpressions;

#endregion

namespace Mvp.Xml.Exslt
{
	/// <summary>
	/// This class implements the EXSLT functions in the http://exslt.org/dates-and-times namespace.
	/// </summary>
	public class ExsltDatesAndTimes
	{
		private CultureInfo ci = new CultureInfo("en-US");

		private class ExsltDateTimeFactory
		{
			/// <summary>
			/// Parse a date and time for format-date() 
			/// </summary>
			/// <param name="d"></param>
			/// <returns></returns>
			public static ExsltDateTime ParseDateTime(string d)
			{
				// First try any of the classes in ParseDate
				try
				{
					return ParseDate(d);
				}
				catch(FormatException)
				{
				}
				
				try
				{
					TimeTZ t = new TimeTZ(d);
					return t;
				}
				catch(FormatException)
				{
				}				

				try
				{
					MonthDay t = new MonthDay(d);
					return t;
				}
				catch(FormatException)
				{
				}				

				try
				{
					Month t = new Month(d);
					return t;
				}
				catch(FormatException)
				{
				}	

				// Finally day -- don't catch the exception
				{
					Day t = new Day(d);
					return t;
				}
			}

			/// <summary>
			/// Initialize the structure with the current date, time and timezone
			/// </summary>
			public static ExsltDateTime ParseDate(string d)
			{
				// Try each potential class, from most specific to least specific.

				// First DateTimeTZ
				try
				{
					DateTimeTZ t = new DateTimeTZ(d);
					return t; 
				}
				catch(FormatException)
				{
				}

				// Next Date
				try
				{
					DateTZ t = new DateTZ(d);
					return t; 
				}
				catch(FormatException)
				{
				}

				// Next YearMonth
				try
				{
					YearMonth t = new YearMonth(d);
					return t; 
				}
				catch(FormatException)
				{
				}

				// Finally Year -- don't catch the exception for the last type
				{	
					YearTZ t = new YearTZ(d);
					return t; 
				}
			}
		}

		internal abstract class ExsltDateTime
		{
			public DateTime	d;
			public TimeSpan	ts = new TimeSpan(TimeSpan.MinValue.Ticks);

			protected CultureInfo ci = new CultureInfo("en-US");

			/// <summary>
			/// Initialize the structure with the current date, time and timezone
			/// </summary>
			public ExsltDateTime()
			{
				d = DateTime.Now;
				TimeZone tz = TimeZone.CurrentTimeZone;
				ts = tz.GetUtcOffset(d);
			}

			/// <summary>
			/// Initialize the DateTimeTZ structure with the date, time and timezone in the string.
			/// </summary>
			/// <param name="inS">An ISO8601 string</param>
			public ExsltDateTime(string inS)
			{
				String s = inS.Trim();
				d = DateTime.ParseExact(s, expectedFormats,	ci, DateTimeStyles.AdjustToUniversal);

				if (s.EndsWith("Z"))
					ts = new TimeSpan(0, 0, 0);
				else if (s.Length > 6)
				{
					String zoneStr = s.Substring(s.Length-6, 6);
					if (zoneStr[3] == ':')
					{
						try
						{
							int	hours = Int32.Parse(zoneStr.Substring(0,3));
							int minutes = Int32.Parse(zoneStr.Substring(4,2));
							if (hours < 0)
								minutes = -minutes;

							ts = new TimeSpan(hours, minutes, 0);
							d = d.Add(ts);	// Adjust to time zone relative time						
						}
						catch(Exception)
						{
						}
					}
				}
			}

			/// <summary>
			/// Exslt Copy constructor
			/// Initialize the structure with the date, time and timezone in the string.
			/// </summary>
			/// <param name="inS">An ExsltDateTime</param>
			public ExsltDateTime(ExsltDateTime inS)
			{
				d = inS.d;
				ts = inS.ts;
			}

			public bool HasTimeZone()
			{
				return !(TimeSpan.MinValue.Ticks == ts.Ticks);
			}

			public DateTime ToUniversalTime()
			{
				if (!HasTimeZone())
					return d;
				else
					return d.Subtract(ts);
			}

			/// <summary>
			/// Output as a standard (ISO8601) string
			/// </summary>
			/// <returns>the date and time as an ISO8601 string.  includes timezone</returns>
			public override string ToString()
			{
				return this.ToString(outputFormat);
			}

			/// <summary>
			/// Output as a formatted string
			/// </summary>
			/// <returns>the date and time as a formatted string.  includes timezone</returns>
			public string ToString(String of)
			{
				StringBuilder retString = new StringBuilder("");
						
				retString.Append(d.ToString(of));
				retString.Append(GetTimeZone());

				return retString.ToString();
			}

			public string GetTimeZone()
			{
				StringBuilder retString = new StringBuilder();

				// if no ts specified, output without ts
				if (HasTimeZone())
				{
					if (0 == ts.Hours && 0 == ts.Minutes)
						retString.Append('Z');
					else if (ts.Hours >= 0 && ts.Minutes >= 0)
					{
						retString.Append('+');
						retString.Append(ts.Hours.ToString().PadLeft(2, '0'));
						retString.Append(':');
						retString.Append(ts.Minutes.ToString().PadLeft(2, '0'));				
					}
					else
					{
						retString.Append('-');
						retString.Append((-ts.Hours).ToString().PadLeft(2, '0'));
						retString.Append(':');
						retString.Append((-ts.Minutes).ToString().PadLeft(2, '0'));
					}
				}
				
				return retString.ToString();
			}

			public string GetGMTOffsetTimeZone()
			{
				StringBuilder retString = new StringBuilder();

				// if no ts specified, output without ts
				if (HasTimeZone())
				{
					retString.Append("GMT");
					if (0 != ts.Hours || 0 != ts.Minutes)
					{
						retString.Append(GetTimeZone());
					}
				}
				
				return retString.ToString();
			}

			public string Get822TimeZone()
			{
				StringBuilder retString = new StringBuilder();

				// if no ts specified, output without ts
				if (HasTimeZone())
				{
					if (0 == ts.Hours && 0 == ts.Minutes)
						retString.Append("GMT");
					else if (ts.Hours >= 0 && ts.Minutes >= 0)
					{
						retString.Append('+');
						retString.Append(ts.Hours.ToString().PadLeft(2, '0'));
						retString.Append(ts.Minutes.ToString().PadLeft(2, '0'));				
					}
					else
					{
						retString.Append('-');
						retString.Append((-ts.Hours).ToString().PadLeft(2, '0'));
						retString.Append((-ts.Minutes).ToString().PadLeft(2, '0'));
					}
				}
				
				return retString.ToString();
			}

			protected abstract string[] expectedFormats {get;}
			protected abstract string outputFormat {get;}
		}

		internal class DateTimeTZ : ExsltDateTime
		{
			public DateTimeTZ() : base(){}
			public DateTimeTZ(string inS) : base(inS){}
			public DateTimeTZ(ExsltDateTime inS) : base(inS){}

			protected override string[] expectedFormats
			{
				get 
				{
					return new string[] {"yyyy-MM-dd\"T\"HH:mm:sszzz", 
											"yyyy-MM-dd\"T\"HH:mm:ssZ", 
											"yyyy-MM-dd\"T\"HH:mm:ss",
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.f"  
};
				}
			}

			protected override string outputFormat
			{
				get
				{
					return "yyyy-MM-dd\"T\"HH:mm:ss";
				}
			}
		}


		internal class DateTZ : ExsltDateTime
		{
			public DateTZ() : base(){}
			public DateTZ(string inS) : base(inS){}
			public DateTZ(ExsltDateTime inS) : base(inS){}

			protected override string[] expectedFormats
			{
				get
				{
					return new string[] {"yyyy-MM-dd\"T\"HH:mm:sszzz", 
											"yyyy-MM-dd\"T\"HH:mm:ssZ", 
											"yyyy-MM-dd\"T\"HH:mm:ss",
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.f",
											"yyyy-MM-ddzzz",
											"yyyy-MM-ddZ",
											"yyyy-MM-dd"};
				}
			}

			protected override string outputFormat
			{
				get
				{
					return "yyyy-MM-dd";
				}
			}
		}

		internal class TimeTZ : ExsltDateTime
		{
			public TimeTZ(string inS) : base(inS){}
			public TimeTZ() : base(){}

			protected override string[] expectedFormats
			{
				get
				{
					return new string[] {"yyyy-MM-dd\"T\"HH:mm:sszzz", 
											"yyyy-MM-dd\"T\"HH:mm:ssZ", 
											"yyyy-MM-dd\"T\"HH:mm:ss",
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.f",
											"HH:mm:sszzz",
											"HH:mm:ssZ",
											"HH:mm:ss"};
				}
			}

			protected override string outputFormat
			{
				get
				{
					return "HH:mm:ss";
				}
			}
		}

		internal class YearMonth : ExsltDateTime
		{
			public YearMonth() : base(){}
			public YearMonth(string inS) : base(inS){}
			public YearMonth(ExsltDateTime inS) : base(inS){}

			protected override string[] expectedFormats
			{
				get
				{
					return new string[] {"yyyy-MM-dd\"T\"HH:mm:sszzz", 
											"yyyy-MM-dd\"T\"HH:mm:ssZ", 
											"yyyy-MM-dd\"T\"HH:mm:ss",
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.f",
											"yyyy-MM-dd",
											"yyyy-MM"};
				}
			}

			protected override string outputFormat
			{
				get
				{
					return "yyyy-MM";
				}
			}
		}

		internal class YearTZ : ExsltDateTime
		{
			public YearTZ() : base(){}
			public YearTZ(string inS) : base(inS){}
			public YearTZ(ExsltDateTime inS) : base(inS){}

			protected override string[] expectedFormats
			{
				get
				{
					return new string[] {"yyyy-MM-dd\"T\"HH:mm:sszzz", 
											"yyyy-MM-dd\"T\"HH:mm:ssZ", 
											"yyyy-MM-dd\"T\"HH:mm:ss",
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.f",
											"yyyy-MM-dd",
											"yyyy-MM",
											"yyyy"};
				}
			}

			protected override string outputFormat
			{
				get
				{
					return "yyyy";
				}
			}
		}

		internal class Month : ExsltDateTime
		{
			public Month() : base(){}
			public Month(string inS) : base(inS){}
			public Month(ExsltDateTime inS) : base(inS){}

			protected override string[] expectedFormats
			{
				get
				{
					return new string[] {"yyyy-MM-dd\"T\"HH:mm:sszzz", 
										 "yyyy-MM-dd\"T\"HH:mm:ssZ", 
										 "yyyy-MM-dd\"T\"HH:mm:ss",
                                         "yyyy-MM-dd\"T\"HH:mm:ss.fffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.f",   
										 "yyyy-MM-dd",
										 "yyyy-MM",
										 "--MM--"};
				}
			}

			protected override string outputFormat
			{
				get
				{
					return "--MM--";
				}
			}
		}

		internal class Day : ExsltDateTime
		{
			public Day() : base(){}
			public Day(string inS) : base(inS){}
			public Day(ExsltDateTime inS) : base(inS){}

			protected override string[] expectedFormats
			{
				get
				{
					return new string[] {"yyyy-MM-dd\"T\"HH:mm:sszzz", 
											"yyyy-MM-dd\"T\"HH:mm:ssZ", 
											"yyyy-MM-dd\"T\"HH:mm:ss",
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.f",
											"yyyy-MM-dd",
											"---dd",
											"--MM-dd"};
				}
			}

			protected override string outputFormat
			{
				get
				{
					return "---dd";
				}
			}
		}

		internal class MonthDay : ExsltDateTime
		{
			public MonthDay() : base(){}
			public MonthDay(string inS) : base(inS){}
			public MonthDay(ExsltDateTime inS) : base(inS){}

			protected override string[] expectedFormats
			{
				get
				{
					return new string[] {"yyyy-MM-dd\"T\"HH:mm:sszzz", 
											"yyyy-MM-dd\"T\"HH:mm:ssZ", 
											"yyyy-MM-dd\"T\"HH:mm:ss",
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ffZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.ff",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fzzz",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.fZ",  
                                            "yyyy-MM-dd\"T\"HH:mm:ss.f",
											"yyyy-MM-dd",
											"--MM-dd"};
				}
			}

			protected override string outputFormat
			{
				get
				{
					return "--MM-dd";
				}
			}
		}


		private string[] dayAbbrevs = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
		private string[] dayNames = {"Sunday", "Monday", "Tuesday", 
										"Wednesday", "Thursday", "Friday", "Saturday"};

		private string[] monthAbbrevs = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
											"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
		private string[] monthNames = {"January", "February", "March", "April", "May", "June",
										  "July", "August", "September", 
										  "October", "November", "December"};

		
		/// <summary>
		/// Implements the following function
		///   string date:date-time()
		/// Output format is ISO 8601 (YYYY-MM-DDThh:mm:ss{Z | {+ | -}zz:zz}).
		/// YYYY - year with century
		/// MM - month in numbers with leading zero
		/// DD - day in numbers with leading zero
		/// T - the letter T
		/// hh - hours in numbers with leading zero (00-23).
		/// mm - minutes in numbers with leading zero (00-59).
		/// ss - seconds in numbers with leading zero (00-59).
		/// +/-zzzz - time zone expressed as hours and minutes from UTC.
		///		If UTC, then this is the letter Z
		///		If east of Greenwich, then -zz:zz (e.g. Pacific standard time is -08:00)
		///		If west of Greenwich, then +zz:zz (e.g. Tokyo is +09:00)
		/// </summary>
		/// <returns>The current time.</returns>		
		public string dateTime()
		{		
			DateTimeTZ	d = new DateTimeTZ();
			return dateTimeImpl(d);
		}
        
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string dateTime_RENAME_ME() 
        {
            return dateTime();
        }    

		/// <summary>
		/// Implements the following function
		///   string date:date-time()
		/// </summary>
		/// <returns>The current date and time or the empty string if the 
		/// date is invalid </returns>        
		public string dateTime(string s){		
			try
			{
				DateTimeTZ d = new DateTimeTZ(s);
				return dateTimeImpl(d);
			}
			catch(FormatException)
			{
				return ""; 
			}
		}
		
		/// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string dateTime_RENAME_ME(string d) 
        {
            return dateTime(d);
        }

		/// <summary>
		/// Internal function to format the date based on a date, rather than a string
		/// </summary>
		/// <returns>The formtted date and time as a ISO8601 string</returns>        

		internal string dateTimeImpl(DateTimeTZ dtz)
		{
			return dtz.ToString();
		}

		/// <summary>
		/// Implements the following function
		///   string date:date()
		/// </summary>
		/// <returns>The current date</returns>        
		public string date()
		{
			DateTZ dtz = new DateTZ();
			return dtz.ToString();
		}

		/// <summary>
		/// Implements the following function
		///   string date:date(string)
		/// </summary>
		/// <returns>The date part of the specified date or the empty string if the 
		/// date is invalid</returns>        
		public string date(string d)
		{
			try
			{
				DateTZ dtz = new DateTZ(d);				
				return dtz.ToString(); 
			}
			catch(FormatException)
			{
				return ""; 
			}
		}

		/// <summary>
		/// Implements the following function
		///   string date:time()
		/// </summary>
		/// <returns>The current time</returns>        
		public string time()
		{
			TimeTZ t = new TimeTZ();
			return t.ToString();
		}

		/// <summary>
		/// Implements the following function
		///   string date:time(string)
		/// </summary>
		/// <returns>The time part of the specified date or the empty string if the 
		/// date is invalid</returns>        
		public string time(string d)
		{
			try
			{
				TimeTZ t = new TimeTZ(d);
				return t.ToString(); 
			}
			catch(FormatException)
			{
				return ""; 
			}
		}
		

		/// <summary>
		/// Implements the following function
		///   number date:year()
		/// </summary>
		/// <returns>The current year</returns>        
		public double year()
		{
			return DateTime.Now.Year;
		}

		/// <summary>
		/// Implements the following function
		///   number date:year(string)
		/// </summary>
		/// <returns>The year part of the specified date or the empty string if the 
		/// date is invalid</returns>
		/// <remarks>Does not support dates in the format of the xs:yearMonth or 
		/// xs:gYear types</remarks>        
		public double year(string d)
		{
			try	
			{
				YearTZ date = new YearTZ(d);
				return date.d.Year; 
			}
			catch(FormatException)
			{
				return System.Double.NaN; 
			}
		}

		/// <summary>
		/// Helper method for calculating whether a year is a leap year. Algorithm 
		/// obtained from http://mindprod.com/jglossleapyear.html
		/// </summary>        
		private static bool IsLeapYear (int year) 
		{ 	
            try 
            {
                return CultureInfo.CurrentCulture.Calendar.IsLeapYear(year); 
            } 
            catch 
            {
                return false;
            }
		}


		/// <summary>
		/// Implements the following function
		///   boolean date:leap-year()
		/// </summary>
		/// <returns>True if the current year is a leap year.</returns>        
		public bool leapYear()
		{
			return IsLeapYear((int) year());
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public bool leapYear_RENAME_ME() 
        {
            return leapYear();
        }

		/// <summary>
		/// Implements the following function
		///   boolean date:leap-year(string)
		/// </summary>
		/// <returns>True if the specified year is a leap year</returns>
		/// <remarks>Note that the spec says we should return NaN for a badly formatted input
		/// string.  This is impossible; we return false for a badly formatted input string.
		/// </remarks>        
		public bool leapYear(string d)
		{
			double y = year(d); 

			if (y == System.Double.NaN)
				return false;
			else
				return IsLeapYear((int)y);
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public bool leapYear_RENAME_ME(string d) 
        {
            return leapYear(d);
        }

		/// <summary>
		/// Implements the following function
		///   number date:month-in-year()
		/// </summary>
		/// <returns>The current month</returns>        
		public double monthInYear()
		{
			return DateTime.Now.Month;
		}
        
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double monthInYear_RENAME_ME() 
        {
            return monthInYear();
        }

		/// <summary>
		/// Implements the following function
		///   number date:month-in-year(string)
		/// </summary>
		/// <returns>The month part of the specified date or the empty string if the 
		/// date is invalid</returns>
		/// <remarks>Does not support dates in the format of xs:gYear</remarks>        
		public double monthInYear(string d)
		{
			try
			{
				Month date = new Month(d);
				return date.d.Month; 
			}
			catch(FormatException)
			{
				return System.Double.NaN; 
			}
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double monthInYear_RENAME_ME(string d) 
        {
            return monthInYear(d);
        }

		/// <summary>
		/// Helper funcitno to calculate the week number
		/// </summary>
		/// <returns>
		/// Returns the week in the year.  Obeys EXSLT spec, which specifies that counting follows 
		/// ISO 8601: week 1 in a year is the week containing the first Thursday of the year, with 
		/// new weeks beginning on a Monday
		/// </returns>
		/// <param name="d">The date for which we want to find the week</param>
		private double weekInYear(DateTime d)
		{
		    return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(d, 
										  System.Globalization.CalendarWeekRule.FirstFourDayWeek, 
										  System.DayOfWeek.Monday);
		}		        

		/// <summary>
		/// Implements the following function
		///   number date:week-in-year()
		/// </summary>
		/// <returns>
		/// The current week. Obeys EXSLT spec, which specifies that counting follows 
		/// ISO 8601: week 1 in a year is the week containing the first Thursday of the year, with 
		/// new weeks beginning on a Monday
		/// </returns>        
		public double weekInYear()
		{
			return this.weekInYear(DateTime.Now);
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double weekInYear_RENAME_ME() 
        {
            return weekInYear();
        }

		/// <summary>
		/// Implements the following function
		///   number date:week-in-year(string)
		/// </summary>
		/// <returns>The week part of the specified date or the empty string if the 
		/// date is invalid</returns>
		/// <remarks>Does not support dates in the format of the xs:yearMonth or 
		/// xs:gYear types. This method uses the Calendar.GetWeekOfYear() method 
		/// with the CalendarWeekRule and FirstDayOfWeek of the current culture.
		/// THE RESULTS OF CALLING THIS FUNCTION VARIES ACROSS CULTURES</remarks>        
		public double weekInYear(string d)
		{
			try
			{
				DateTZ dtz = new DateTZ(d);
				return weekInYear(dtz.d);
			}
			catch(FormatException)
			{
				return System.Double.NaN; 
			}
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double weekInYear_RENAME_ME(string d) 
        {
            return weekInYear(d);
        }

        /// <summary>
        /// Helper method. 
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        private double weekInMonth(DateTime d)
		{
			//
			// mon = 1
			// tue = 2
			// sun = 7
			// week = ceil(((date-day) / 7)) + 1

			double offset = (d.DayOfWeek == DayOfWeek.Sunday) ? 7 : (double)d.DayOfWeek;
			return System.Math.Ceiling((d.Day-offset) / 7)+1;
        }

        /// <summary>
        /// Implements the following function
        ///   number date:week-in-month()
        /// </summary>
		/// <remarks>
		/// The current week in month as a number.  For the purposes of numbering, the first 
		/// day of the month is in week 1 and new weeks begin on a Monday (so the first and 
		/// last weeks in a month will often have less than 7 days in them). 
		/// </remarks>        
		public double weekInMonth()
        {
            return this.weekInMonth(DateTime.Now);
        }
        
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double weekInMonth_RENAME_ME() 
        {
            return weekInMonth();
        }

        /// <summary>
        /// Implements the following function
        ///   number date:week-in-month(string)
        /// </summary>
        /// <returns>The week in month of the specified date or NaN if the 
        /// date is invalid</returns>
		/// <remarks>
		/// The current week in month as a number.  For the purposes of numbering, the first 
		/// day of the month is in week 1 and new weeks begin on a Monday (so the first and 
		/// last weeks in a month will often have less than 7 days in them). 
		/// </remarks>        
        public double weekInMonth(string d)
        {
            try
            {
                DateTZ date = new DateTZ(d); 
                return this.weekInMonth(date.d); 
            }
            catch(FormatException)
            {
                return System.Double.NaN; 
            }
        }
        
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double weekInMonth_RENAME_ME(string d) 
        {
            return weekInMonth(d);
        }


		/// <summary>
		/// Implements the following function
		///   number date:day-in-year()
		/// </summary>
		/// <returns>The current day. </returns>        
		public double dayInYear()
		{
			return DateTime.Now.DayOfYear;
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double dayInYear_RENAME_ME() 
        {
            return dayInYear();
        }

		/// <summary>
		/// Implements the following function
		///   number date:day-in-year(string)
		/// </summary>
		/// <returns>
		/// The day part of the specified date or NaN if the date is invalid
		/// </returns>        
		public double dayInYear(string d)
		{
			try
			{
				DateTZ date = new DateTZ(d); 
				return date.d.DayOfYear; 
			}
			catch(FormatException)
			{
				return System.Double.NaN; 
			}
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double dayInYear_RENAME_ME(string d) 
        {
            return dayInYear(d);
        }

		/// <summary>
		/// Implements the following function
		///   number date:day-in-week()
		/// </summary>
		/// <returns>The current day in the week. 1=Sunday, 2=Monday,...,7=Saturday</returns>        
		public double dayInWeek()
		{
			return ((int) DateTime.Now.DayOfWeek) + 1;
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double dayInWeek_RENAME_ME() 
        {
            return dayInWeek();
        }

		/// <summary>
		/// Implements the following function
		///   number date:day-in-week(string)
		/// </summary>
		/// <returns>The day in the week of the specified date or NaN if the 
		/// date is invalid. The current day in the week. 1=Sunday, 2=Monday,...,7=Saturday
		/// </returns>        
		public double dayInWeek(string d){
			try
			{
				DateTZ date = new DateTZ(d); 
				return ((int)date.d.DayOfWeek) + 1; 
			}
			catch(FormatException)
			{
				return System.Double.NaN; 
			}
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double dayInWeek_RENAME_ME(string d) 
        {
            return dayInWeek(d);
        }


		/// <summary>
		/// Implements the following function
		///   number date:day-in-month()
		/// </summary>
		/// <returns>The current day. </returns>        
		public double dayInMonth()
		{
			return DateTime.Now.Day;
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double dayInMonth_RENAME_ME() 
        {
            return dayInMonth();
        }

		/// <summary>
		/// Implements the following function
		///   number date:day-in-month(string)
		/// </summary>
		/// <returns>The day part of the specified date or the empty string if the 
		/// date is invalid</returns>
		public double dayInMonth(string d)
		{
			try
			{
				Day date = new Day(d);
				return date.d.Day; 
			}
			catch(FormatException)
			{
				return System.Double.NaN; 
			}
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double dayInMonth_RENAME_ME(string d) 
        {
            return dayInMonth(d);
        }

		/// <summary>
		/// Helper method.
		/// </summary>
		/// <param name="day"></param>
		/// <returns></returns>
		private double dayOfWeekInMonth(int day)
		{
			// day of week in month = floor(((date-1) / 7)) + 1
			return ((day-1)/7) + 1;
		}

		/// <summary>
		/// Implements the following function
		///   number date:day-of-week-in-month()
		/// </summary>
		/// <returns>The current day of week in the month as a number. For instance 
		/// the third Friday of the month returns 3</returns>        
		public double dayOfWeekInMonth()
		{
			return this.dayOfWeekInMonth(DateTime.Now.Day);
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double dayOfWeekInMonth_RENAME_ME() 
        {
            return dayOfWeekInMonth();
        }

		/// <summary>
		/// Implements the following function
		///   number date:day-of-week-in-month(string)
		/// </summary>
		/// <returns>The day part of the specified date or NaN if the 
		/// date is invalid</returns>        
		public double dayOfWeekInMonth(string d)
		{
			try
			{
				DateTZ date = new DateTZ(d); 
				return this.dayOfWeekInMonth(date.d.Day); 
			}
			catch(FormatException)
			{
				return System.Double.NaN; 
			}
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double dayOfWeekInMonth_RENAME_ME(string d) 
        {
            return dayOfWeekInMonth(d);
        }
	
		/// <summary>
		/// Implements the following function
		///   number date:hour-in-day()
		/// </summary>
		/// <returns>The current hour of the day as a number.</returns>        
		public double hourInDay()
		{
			return DateTime.Now.Hour;
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double hourInDay_RENAME_ME() 
        {
            return hourInDay();
        }

		/// <summary>
		/// Implements the following function
		///   number date:hour-in-day(string)
		/// </summary>
		/// <returns>The current hour of the specified time or NaN if the 
		/// date is invalid</returns>        
		public double hourInDay(string d)
		{
			try
			{
				TimeTZ date = new TimeTZ(d); 
				return date.d.Hour; 
			}
			catch(FormatException)
			{
				return System.Double.NaN; 
			}
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double hourInDay_RENAME_ME(string d) 
        {
            return hourInDay(d);
        }

		/// <summary>
		/// Implements the following function
		///   number date:minute-in-hour()
		/// </summary>
		/// <returns>The minute of the current hour as a number. </returns>        
		public double minuteInHour()
		{
			return DateTime.Now.Minute;
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double minuteInHour_RENAME_ME() 
        {
            return minuteInHour();
        }

		/// <summary>
		/// Implements the following function
		///   number date:minute-in-hour(string)
		/// </summary>
		/// <returns>The minute of the hour of the specified time or NaN if the 
		/// date is invalid</returns>        
		public double minuteInHour(string d)
		{
			try
			{
				TimeTZ date = new TimeTZ(d); 
				return date.d.Minute; 
			}
			catch(FormatException)
			{
				return System.Double.NaN; 
			}
		}

        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double minuteInHour_RENAME_ME(string d) 
        {
            return minuteInHour(d);
        }

		/// <summary>
		/// Implements the following function
		///   number date:second-in-minute()
		/// </summary>
		/// <returns>The seconds of the current minute as a number. </returns>        
		public double secondInMinute()
		{
			return DateTime.Now.Second;
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double secondInMinute_RENAME_ME() 
        {
            return secondInMinute();
        }		

		/// <summary>
		/// Implements the following function
		///   number date:second-in-minute(string)
		/// </summary>
		/// <returns>The seconds of the minute of the specified time or NaN if the 
		/// date is invalid</returns>        
		public double secondInMinute(string d)
		{
			try
			{
				TimeTZ date = new TimeTZ(d); 
				return date.d.Second; 
			}
			catch(FormatException)
			{
				return System.Double.NaN; 
			}
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public double secondInMinute_RENAME_ME(string d) 
        {
            return secondInMinute(d);
        }

		/// <summary>
		/// Helper function for 
		///   string date:day-name()
		/// </summary>
		/// <returns>The Engish name of the current day</returns>        
		private string dayName(int dow)
		{
            if (dow <0 || dow >= dayNames.Length)
                return String.Empty;
			return dayNames[dow];
		}

		/// <summary>
		/// Implements the following function
		///   string date:day-name()
		/// </summary>
		/// <returns>The Engish name of the current day</returns>        
		public string dayName()
		{
			return dayName((int) DateTime.Now.DayOfWeek);
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string dayName_RENAME_ME() 
        {
            return dayName();
        }

		/// <summary>
		/// Implements the following function
		///   string date:day-name(string)
		/// </summary>
		/// <returns>The English name of the day of the specified date or the empty string if the 
		/// date is invalid</returns>        
		public string dayName(string d)
		{
			try
			{
				DateTZ date = new DateTZ(d); 
				return dayName((int) date.d.DayOfWeek);
			}
			catch(FormatException)
			{
				return ""; 
			}
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string dayName_RENAME_ME(string d) 
        {
            return dayName(d);
        }

		/// <summary>
		/// Helper function for 
		///   string date:day-abbreviation()
		/// </summary>
		/// <returns>The abbreviated English name of the current day</returns>        
		private string dayAbbreviation(int dow)
		{
            if (dow < 0 || dow >= dayAbbrevs.Length)
                return String.Empty;
			return dayAbbrevs[dow];
		}


		/// <summary>
		/// Implements the following function
		///   string date:day-abbreviation()
		/// </summary>
		/// <returns>The abbreviated English name of the current day</returns>        
		public string dayAbbreviation()
		{
			return dayAbbreviation((int)DateTime.Now.DayOfWeek);
		}

        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string dayAbbreviation_RENAME_ME() 
        {
            return dayAbbreviation();
        }

		/// <summary>
		/// Implements the following function
		///   string date:day-abbreviation(string)
		/// </summary>
		/// <returns>The abbreviated English name of the day of the specified date or the 
		/// empty string if the input date is invalid</returns>        
		public string dayAbbreviation(string d)
		{
			try
			{
				DateTZ date = new DateTZ(d); 
				return dayAbbreviation((int)date.d.DayOfWeek);
			}
			catch(FormatException)
			{
				return ""; 
			}
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string dayAbbreviation_RENAME_ME(string d) 
        {
            return dayAbbreviation(d);
        }

		/// <summary>
		/// Helper Function for 
		///   string date:month-name()
		/// </summary>
		/// <returns>The name of the current month</returns>        
		private string monthName(int month)
		{
            if (month < 1 || month > monthNames.Length)
                return String.Empty;
			return monthNames[month-1];
		}

		/// <summary>
		/// Implements the following function
		///   string date:month-name()
		/// </summary>
		/// <returns>The name of the current month</returns>        
		public string monthName()
		{
			return monthName((int)monthInYear());
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string monthName_RENAME_ME() 
        {
            return monthName();
        }

		/// <summary>
		/// Implements the following function
		///   string date:month-name(string)
		/// </summary>
		/// <returns>The name of the month of the specified date or the empty string if the 
		/// date is invalid</returns>
		/// <remarks>Does not support dates in the format of xs:gYear types</remarks>        
		public string monthName(string d)
		{
			double month = monthInYear(d);
			if (month == System.Double.NaN)
				return "";
			else
				return monthName((int)month);
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string monthName_RENAME_ME(string d) 
        {
            return monthName(d);
        }

		/// <summary>
		/// Helper function for 
		///   string date:month-abbreviation()
		/// </summary>
		/// <returns>The abbreviated name of the current month</returns>        
		private string monthAbbreviation(int month)
		{
            if (month < 1 || month > monthAbbrevs.Length)
                return String.Empty;
			return monthAbbrevs[month-1];
		}

		/// <summary>
		/// Implements the following function
		///   string date:month-abbreviation()
		/// </summary>
		/// <returns>The abbreviated name of the current month</returns>        
		public string monthAbbreviation()
		{
			return monthAbbreviation((int)monthInYear());
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string monthAbbreviation_RENAME_ME() 
        {
            return monthAbbreviation();
        }

		/// <summary>
		/// Implements the following function
		///   string date:month-abbreviation(string)
		/// </summary>
		/// <returns>The abbreviated name of the month of the specified date or the empty string if the 
		/// date is invalid</returns>
		/// <remarks>Does not support dates in the format of the xs:yearMonth or 
		/// xs:gYear types</remarks>        
		public string monthAbbreviation(string d)
		{
			double month = monthInYear(d);
			if (month == System.Double.NaN)
				return "";
			else
				return monthAbbreviation((int)month);
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string monthAbbreviation_RENAME_ME(string d) 
        {
            return monthAbbreviation(d);
        }

		/// <summary>
		/// Implements the following function
		///   string date:format-date(string, string)
		/// </summary>
		/// <param name="d">The date to format</param>
		/// <param name="format">One of the format strings understood by the 
		/// Java 1.1 SimpleDateFormat method:
		/// 
		///  Symbol   Meaning                 Presentation        Example
		///------   -------                 ------------        -------
		///G        era designator          (Text)              AD
		///y        year                    (Number)            1996
		///M        month in year           (Text &amp; Number)     July &amp; 07
		///d        day in month            (Number)            10
		///h        hour in am/pm (1~12)    (Number)            12
		///H        hour in day (0~23)      (Number)            0
		///m        minute in hour          (Number)            30
		///s        second in minute        (Number)            55
		///S        millisecond             (Number)            978
		///E        day in week             (Text)              Tuesday
		///D        day in year             (Number)            189
		///F        day of week in month    (Number)            2 (2nd Wed in July)
		///w        week in year            (Number)            27
		///W        week in month           (Number)            2
		///a        am/pm marker            (Text)              PM
		///k        hour in day (1~24)      (Number)            24
		///K        hour in am/pm (0~11)    (Number)            0
		///z        time zone               (Text)              Pacific Standard Time
		///'        escape for text         (Delimiter)
		///''       single quote            (Literal)           '
		///</param>
		/// <returns>The formated date</returns>        
		public string formatDate(string d, string format)
		{
			try
			{
				ExsltDateTime oDate = ExsltDateTimeFactory.ParseDateTime(d);
				StringBuilder retString = new StringBuilder("");

				for (int i=0; i < format.Length;)
				{
					int s = i;
					switch(format[i])
					{
						case 'G'://        era designator          (Text)              AD
							while (i < format.Length && format[i]=='G'){i++;}

							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								if (oDate.d.Year < 0)
								{
									retString.Append("BC");
								}
								else
								{
									retString.Append("AD");
								}
							}
							break;

						case 'y'://        year                    (Number)            1996
							while (i < format.Length && format[i]=='y'){i++;}
							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								if (i-s == 2)
								{
									retString.Append((oDate.d.Year % 100).ToString().PadLeft(i-s, '0'));

								}
								else
								{
									retString.Append(oDate.d.Year.ToString().PadLeft(i-s, '0'));
								}
							}
							break;
						case 'M'://        month in year           (Text &amp; Number)     July &amp; 07
							while (i < format.Length && format[i]=='M'){i++;}

							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(Month)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(MonthDay)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								if (i-s <= 2)
									retString.Append(oDate.d.Month.ToString().PadLeft(i-s, '0'));
								else if (i-s == 3)
									retString.Append(monthAbbreviation(oDate.d.Month));
								else
									retString.Append(monthName(oDate.d.Month));
							}
							break;
						case 'd'://        day in month            (Number)            10
							while (i < format.Length && format[i]=='d'){i++;}

							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(MonthDay)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(Day)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								retString.Append(oDate.d.Day.ToString().PadLeft(i-s, '0'));
							}
							break;
						case 'h'://        hour in am/pm (1~12)    (Number)            12
							while (i < format.Length && format[i]=='h'){i++;}
							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(TimeTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								int hour = oDate.d.Hour % 12;
								if (0 == hour)
									hour = 12;
								retString.Append(hour.ToString().PadLeft(i-s, '0'));
							}
							break;
						case 'H'://        hour in day (0~23)      (Number)            0
							while (i < format.Length && format[i]=='H'){i++;}
							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(TimeTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								retString.Append(oDate.d.Hour.ToString().PadLeft(i-s, '0'));
							}
							break;
						case 'm'://        minute in hour          (Number)            30
							while (i < format.Length && format[i]=='m'){i++;}
							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(TimeTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								retString.Append(oDate.d.Minute.ToString().PadLeft(i-s, '0'));
							}
							break;
						case 's'://        second in minute        (Number)            55
							while (i < format.Length && format[i]=='s'){i++;}
							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(TimeTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								retString.Append(oDate.d.Second.ToString().PadLeft(i-s, '0'));
							}
							break;
						case 'S'://        millisecond             (Number)            978
							while (i < format.Length && format[i]=='S'){i++;}
                            if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) ||
                                Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
                                Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
                                Object.ReferenceEquals(oDate.GetType(), typeof(TimeTZ)) ||
                                Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
                            {                                
                                retString.Append(oDate.d.Millisecond.ToString().PadLeft(i-s, '0'));
                            }							
							break;
						case 'E'://        day in week             (Text)              Tuesday
							while (i < format.Length && format[i]=='E'){i++;}

							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								if (i-s <= 3)
								{
									retString.Append(dayAbbreviation((int)oDate.d.DayOfWeek));
								}
								else
								{
									retString.Append(dayName((int)oDate.d.DayOfWeek));
								}
							}
							break;
						case 'D'://        day in year             (Number)            189
							while (i < format.Length && format[i]=='D'){i++;}
							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								retString.Append(oDate.d.DayOfYear.ToString().PadLeft(i-s, '0'));
							}
							break;
						case 'F'://        day of week in month    (Number)            2 (2nd Wed in July)
							while (i < format.Length && format[i]=='F'){i++;}
							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(MonthDay)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(Day)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								retString.Append(dayOfWeekInMonth(oDate.d.Day).ToString().PadLeft(i-s, '0'));
							}
							break;
						case 'w'://        week in year            (Number)            27
							while (i < format.Length && format[i]=='w'){i++;}
							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								retString.Append(weekInYear(oDate.d));
							}
							break;
						case 'W'://        week in month           (Number)            2
							while (i < format.Length && format[i]=='W'){i++;}
							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								retString.Append(weekInMonth(oDate.d));
							}
							break;
						case 'a'://        am/pm marker            (Text)              PM
							while (i < format.Length && format[i]=='a'){i++;}
							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(TimeTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								if (oDate.d.Hour < 12)
									retString.Append("AM");
								else
									retString.Append("PM");
							}
							break;
						case 'k'://        hour in day (1~24)      (Number)            24
							while (i < format.Length && format[i]=='k'){i++;}
							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(TimeTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								int hour = oDate.d.Hour + 1;
								retString.Append(hour.ToString().PadLeft(i-s, '0'));
							}
							break;
						case 'K'://        hour in am/pm (0~11)    (Number)            0
							while (i < format.Length && format[i]=='K'){i++;}
							if (Object.ReferenceEquals(oDate.GetType(), typeof(DateTimeTZ)) || 
								Object.ReferenceEquals(oDate.GetType(), typeof(DateTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearMonth)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(TimeTZ)) ||
								Object.ReferenceEquals(oDate.GetType(), typeof(YearTZ)))
							{
								int hour = oDate.d.Hour % 12;
								retString.Append(hour.ToString().PadLeft(i-s, '0'));
							}
							break;
						case 'z'://        time zone               (Text)              Pacific Standard Time
							while (i < format.Length && format[i]=='z'){i++;}
							//
							// BUGBUG: Need to convert to full timezone names or timezone abbrevs
							// if they are available.  Now cheating by using GMT offsets.
							retString.Append(oDate.GetGMTOffsetTimeZone());
							break;
						case 'Z'://			rfc 822 time zone
							while (i < format.Length && format[i]=='Z'){i++;}
							retString.Append(oDate.Get822TimeZone());
							break;
						case '\''://        escape for text         (Delimiter)
							if (i < format.Length && format[i+1] == '\'')
							{
								i++;
								while (i < format.Length && format[i]=='\''){i++;}
								retString.Append("'");
							}
							else
							{
								i++;
								while (i < format.Length && format[i]!='\'' && i <= format.Length){retString.Append(format.Substring(i++, 1));}
								if (i >= format.Length)return "";
								i++;
							}
							break;
						default:
							retString.Append(format[i]);
							i++;
							break;
					}
				}
	
				return retString.ToString();
			}

		
			catch(FormatException)
			{
				return ""; 
			}
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string formatDate_RENAME_ME(string d, string format) 
        {
            return formatDate(d, format);
        }


		/// <summary>
		/// Implements the following function
		///   string date:parse-date(string, string)
		/// BUGBUG: should use Java formatting strings, not Windows.
		/// </summary>
		/// <param name="d">The date to parse</param>
		/// <param name="format">One of the format strings understood by the 
		/// DateTime.ToString(string) method.</param>
		/// <returns>The parsed date</returns>        
		public string parseDate(string d, string format){
			try{
				DateTime date = DateTime.ParseExact(d, format, CultureInfo.CurrentCulture); 
				return XmlConvert.ToString(date, XmlDateTimeSerializationMode.RoundtripKind);
			}catch(FormatException){
				return ""; 
			}
		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string parseDate_RENAME_ME(string d, string format) 
        {
            return parseDate(d, format);
        }
	
		/// <summary>
		/// Implements the following function 
		///    string:date:difference(string, string)
		/// </summary>
		/// <param name="start">The start date</param>
		/// <param name="end">The end date</param>
		/// <returns>A positive difference if start is before end otherwise a negative
		/// difference. The difference is in the ISO 8601 date difference format as either
		/// [-]P[yY][mM]
		/// or
		/// [-]P[dD][T][hH][mM][sS]
		/// P means a difference and is required
		/// At least one of Y M D H M S is required
		/// If a higher order component is 0, it is suppressed (for example, if there are 0 years and
		/// 1 month, then Y is suppressed.  If there are 1 years and 0 months, M is not suppressed)
		/// If the input format is yyyy-MM or yyyy, then the output is [-]P[yY][mM]
		/// If the input format includes days, hours, minutes or seconds, then the output is
		/// [-]P[dD][T][hH][mM][sS].
		/// If there H M and S are all 0, the T is suppressed.
		/// </returns>        
		public string difference(string start, string end)
		{		
			try
			{
				ExsltDateTime startdate = ExsltDateTimeFactory.ParseDate(start); 
				ExsltDateTime enddate   = ExsltDateTimeFactory.ParseDate(end); 

				// The rules are pretty tricky.  basically, interpret both strings as the least-
				// specific format
				if (Object.ReferenceEquals(startdate.GetType(), typeof(YearTZ)) || 
					Object.ReferenceEquals(enddate.GetType(), typeof(YearTZ)))
				{
					StringBuilder retString = new StringBuilder("");

					int	yearDiff = enddate.d.Year - startdate.d.Year;

					if (yearDiff < 0)
						retString.Append('-');

					retString.Append('P');
					retString.Append(Math.Abs(yearDiff));
					retString.Append('Y');

					return retString.ToString();
				}					
				else if (Object.ReferenceEquals(startdate.GetType(), typeof(YearMonth)) || 
					Object.ReferenceEquals(enddate.GetType(), typeof(YearMonth)))
				{
					StringBuilder retString = new StringBuilder("");

					int	yearDiff = enddate.d.Year - startdate.d.Year;
					int monthDiff = enddate.d.Month - startdate.d.Month;

					// Borrow from the year if necessary
					if ((yearDiff > 0) && (Math.Sign(monthDiff) == -1))
					{
						yearDiff--;
						monthDiff += 12;
					} 
					else if ((yearDiff < 0) && (Math.Sign(monthDiff) == 1))
					{
						yearDiff++;
						monthDiff -= 12;
					}


					if ((yearDiff < 0) || ((yearDiff == 0) && (monthDiff < 0)))
					{
						retString.Append('-');						
					}
					retString.Append('P');
					if (yearDiff != 0)
					{
						retString.Append(Math.Abs(yearDiff));
						retString.Append('Y');
					}
					retString.Append(Math.Abs(monthDiff));
					retString.Append('M');
	
					return retString.ToString();
				}
				else
				{
					// Simulate casting to the most truncated format.  i.e. if one 
					// Arg is DateTZ and the other is DateTimeTZ, get rid of the time
					// for both.
					if (Object.ReferenceEquals(startdate.GetType(), typeof(DateTZ)) || 
						Object.ReferenceEquals(enddate.GetType(), typeof(DateTZ)))
					{
						startdate = new DateTZ(startdate.d.ToString("yyyy-MM-dd"));
						enddate = new DateTZ(enddate.d.ToString("yyyy-MM-dd"));
					}

					TimeSpan ts = enddate.d.Subtract(startdate.d);
					return XmlConvert.ToString(ts);
				}
			}
			catch(FormatException)
			{
				return ""; 
			}
		}

		/// <summary>
		/// Implements the following function
		///    date:add(string, string)
		/// </summary>
		/// <param name="datetime">An ISO8601 date/time</param>
		/// <param name="duration">the duration to add</param>
		/// <returns>The new time</returns>        
		public string add(string datetime, string duration)
		{			
			try
			{
				ExsltDateTime date = ExsltDateTimeFactory.ParseDate(datetime); 
				//TimeSpan timespan = System.Xml.XmlConvert.ToTimeSpan(duration); 

				Regex durationRE = new Regex(
					@"^(-)?" +				// May begin with a - sign
					@"P" +					// Must contain P as first or 2nd char
					@"(?=\d+|(?:T\d+))" +		// Must contain at least one digit after P or after PT
					@"(?:(\d+)Y)?" +		// May contain digits plus Y for year
					@"(?:(\d+)M)?" +		// May contain digits plus M for month
					@"(?:(\d+)D)?" +		// May contain digits plus D for day
					@"(?=T\d+)?" +			// If there is a T here, must be digits afterwards
					@"T?" +					// May contain a T
					@"(?:(\d+)H)?" +		// May contain digits plus H for hours
					@"(?:(\d+)M)?" +		// May contain digits plus M for minutes
					@"(?:(\d+)S)?" +		// May contain digits plus S for seconds
					@"$", 
					RegexOptions.IgnoreCase | RegexOptions.Singleline
					);

				Match m = durationRE.Match(duration);

				int negation = 1, years = 0, months = 0, days = 0,
					hours = 0, minutes = 0, seconds = 0;

				if (m.Success)
				{
					//date.d = date.d.Add(timespan);
					// According to the XML datetime spec at 
					// http://www.w3.org/TR/xmlschema-2/#adding-durations-to-dateTimes, 
					// we need to first add the year/month part, then we can add the 
					// day/hour/minute/second part

					if (CultureInfo.InvariantCulture.CompareInfo.Compare(m.Groups[1].Value, "-") == 0)
						negation = -1;

					if (m.Groups[2].Length > 0)
						years = negation * int.Parse(m.Groups[2].Value);

					if (m.Groups[3].Length > 0)
						months = negation * int.Parse(m.Groups[3].Value);

					if (m.Groups[4].Length > 0)
						days = negation * int.Parse(m.Groups[4].Value);

					if (m.Groups[5].Length > 0)
						hours = negation * int.Parse(m.Groups[5].Value);

					if (m.Groups[6].Length > 0)
						minutes = negation * int.Parse(m.Groups[6].Value);

					if (m.Groups[7].Length > 0)
						seconds = negation * int.Parse(m.Groups[7].Value);

					date.d = date.d.AddYears(years);
					date.d = date.d.AddMonths(months);
					date.d = date.d.AddDays(days);
					date.d = date.d.AddHours(hours);
					date.d = date.d.AddMinutes(minutes);
					date.d = date.d.AddSeconds(seconds);

					// we should return the same format as passed in

					// return date.ToString("yyyy-MM-dd\"T\"HH:mm:ss");			
					return date.ToString();
				}
				else
				{
					return "";
				}
			}
			catch(FormatException)	
			{
				return ""; 
			}
		}


		/// <summary>
		/// Implements the following function
		///    string:date:add-duration(string, string)
		/// </summary>
        /// <param name="duration1">Initial duration</param>
        /// <param name="duration2">the duration to add</param>
		/// <returns>The new time</returns>        
		public string addDuration(string duration1, string duration2)
		{			
			try
			{
				TimeSpan timespan1 = XmlConvert.ToTimeSpan(duration1);
				TimeSpan timespan2 = XmlConvert.ToTimeSpan(duration2); 
				return XmlConvert.ToString(timespan1.Add(timespan2));
			}
			catch(FormatException)
			{
				return ""; 
			}

		}
		
        /// <summary>
        /// This wrapper method will be renamed during custom build 
        /// to provide conformant EXSLT function name.
        /// </summary>
        public string addDuration_RENAME_ME(string duration1, string duration2) 
        {
            return addDuration(duration1, duration2);
        }

		/// <summary>
		/// Helper method for date:seconds() that takes an ExsltDateTime. 
		/// </summary>
		/// <param name="d"></param>
		/// <returns>difference in seconds between the specified date and the
		/// epoch (1970-01-01T00:00:00Z)</returns>
		private double seconds(ExsltDateTime d)
		{
			DateTime epoch = new DateTime(1970, 1, 1, 0,0,0,0, CultureInfo.InvariantCulture.Calendar);
			return d.ToUniversalTime().Subtract(epoch).TotalSeconds;
		}

		/// <summary>
		/// Implements the following function
		///		number date:seconds()
		/// </summary>
		/// <returns>The amount of seconds since the epoch (1970-01-01T00:00:00Z)</returns>        
		public double seconds()
		{		 
			return seconds(new DateTimeTZ());
		}


		/// <summary>
		/// Implements the following function
		///		number date:seconds(string)
		/// </summary>
		/// <returns>If date passed in, the amount of seconds between the specified date and the 
		/// epoch (1970-01-01T00:00:00Z).  If timespan passed in, returns the number of seconds
		/// in the timespan.</returns>
		public double seconds(string datetime)
		{		 
			try
			{
				return seconds(ExsltDateTimeFactory.ParseDate(datetime));
			}
			catch(FormatException){ ; } //might be a duration

			try
			{
				TimeSpan duration = XmlConvert.ToTimeSpan(datetime); 
				return duration.TotalSeconds;
			}
			catch(FormatException)
			{
				return System.Double.NaN;
			}
		}

		/// <summary>
		/// Implements the following function 
		///		string date:sum(node-set)
		/// </summary>
		/// <param name="iterator">Nodeset of timespans</param>
		/// <returns>The sum of the timespans within a node set.</returns>        
		public string sum(XPathNodeIterator iterator)
		{
			
			TimeSpan sum = new TimeSpan(0,0,0,0); 
 
			if(iterator.Count == 0)
			{
				return ""; 
			}

			try
			{ 
				while(iterator.MoveNext())
				{
					sum = XmlConvert.ToTimeSpan(iterator.Current.Value).Add(sum);
				}
				
			}
			catch(FormatException)
			{
				return ""; 
			}

			return XmlConvert.ToString(sum) ; //XmlConvert.ToString(sum);
		}

		/// <summary>
		/// Implements the following function 
		///    string date:duration()
		/// </summary>
		/// <returns>seconds since the beginning of the epoch until now</returns>        
		public string duration()
		{
			return duration(seconds()); 
		}

		/// <summary>
		/// Implements the following function 
		///    string date:duration(number)
		/// </summary>
		/// <param name="seconds"></param>
		/// <returns></returns>        
		public string duration(double seconds)
		{
			return XmlConvert.ToString(TimeSpan.FromSeconds(seconds)); 
		}	
	}
}
Back to Top