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
|
/*
* System.DateTimeOffset.cs
*
* Author(s)
* Stephane Delcroix <stephane@delcroix.org>
* Marek Safar (marek.safar@gmail.com)
*
* Copyright (C) 2007 Novell, Inc (http://www.novell.com)
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#if NET_2_0 // Introduced by .NET 3.5 for 2.0 mscorlib
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Text;
namespace System
{
[Serializable]
[StructLayout (LayoutKind.Auto)]
public struct DateTimeOffset : IComparable, IFormattable, ISerializable, IDeserializationCallback, IComparable<DateTimeOffset>, IEquatable<DateTimeOffset>
{
#if MONOTOUCH
static DateTimeOffset () {
if (MonoTouchAOTHelper.FalseFlag) {
var comparer = new System.Collections.Generic.GenericComparer <DateTimeOffset> ();
var eqcomparer = new System.Collections.Generic.GenericEqualityComparer <DateTimeOffset> ();
}
}
#endif
public static readonly DateTimeOffset MaxValue = new DateTimeOffset (DateTime.MaxValue, TimeSpan.Zero);
public static readonly DateTimeOffset MinValue = new DateTimeOffset (DateTime.MinValue, TimeSpan.Zero);
DateTime dt;
TimeSpan utc_offset;
public DateTimeOffset (DateTime dateTime)
{
dt = dateTime;
if (dateTime.Kind == DateTimeKind.Utc)
utc_offset = TimeSpan.Zero;
else
utc_offset = TimeZone.CurrentTimeZone.GetUtcOffset (dateTime);
if (UtcDateTime < DateTime.MinValue || UtcDateTime > DateTime.MaxValue)
throw new ArgumentOutOfRangeException ("The UTC date and time that results from applying the offset is earlier than MinValue or later than MaxValue.");
}
public DateTimeOffset (DateTime dateTime, TimeSpan offset)
{
if (dateTime.Kind == DateTimeKind.Utc && offset != TimeSpan.Zero)
throw new ArgumentException ("dateTime.Kind equals Utc and offset does not equal zero.");
if (dateTime.Kind == DateTimeKind.Local && offset != TimeZone.CurrentTimeZone.GetUtcOffset (dateTime))
throw new ArgumentException ("dateTime.Kind equals Local and offset does not equal the offset of the system's local time zone.");
if (offset.Ticks % TimeSpan.TicksPerMinute != 0)
throw new ArgumentException ("offset is not specified in whole minutes.");
if (offset < new TimeSpan (-14, 0 ,0) || offset > new TimeSpan (14, 0, 0))
throw new ArgumentOutOfRangeException ("offset is less than -14 hours or greater than 14 hours.");
dt = dateTime;
utc_offset = offset;
if (UtcDateTime < DateTime.MinValue || UtcDateTime > DateTime.MaxValue)
throw new ArgumentOutOfRangeException ("The UtcDateTime property is earlier than MinValue or later than MaxValue.");
}
public DateTimeOffset (long ticks, TimeSpan offset) : this (new DateTime (ticks), offset)
{
}
public DateTimeOffset (int year, int month, int day, int hour, int minute, int second, TimeSpan offset) :
this (new DateTime (year, month, day, hour, minute, second), offset)
{
}
public DateTimeOffset (int year, int month, int day, int hour, int minute, int second, int millisecond, TimeSpan offset) :
this (new DateTime (year, month, day, hour, minute, second, millisecond), offset)
{
}
public DateTimeOffset (int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, TimeSpan offset) :
this (new DateTime (year, month, day, hour, minute, second, millisecond, calendar), offset)
{
}
public DateTimeOffset Add (TimeSpan timeSpan)
{
return new DateTimeOffset (dt.Add (timeSpan), utc_offset);
}
public DateTimeOffset AddDays (double days)
{
return new DateTimeOffset (dt.AddDays (days), utc_offset);
}
public DateTimeOffset AddHours (double hours)
{
return new DateTimeOffset (dt.AddHours (hours), utc_offset);
}
public static DateTimeOffset operator + (DateTimeOffset dateTimeTz, TimeSpan timeSpan)
{
return dateTimeTz.Add (timeSpan);
}
public DateTimeOffset AddMilliseconds (double milliseconds)
{
return new DateTimeOffset (dt.AddMilliseconds (milliseconds), utc_offset);
}
public DateTimeOffset AddMinutes (double minutes)
{
return new DateTimeOffset (dt.AddMinutes (minutes), utc_offset);
}
public DateTimeOffset AddMonths (int months)
{
return new DateTimeOffset (dt.AddMonths (months), utc_offset);
}
public DateTimeOffset AddSeconds (double seconds)
{
return new DateTimeOffset (dt.AddSeconds (seconds), utc_offset);
}
public DateTimeOffset AddTicks (long ticks)
{
return new DateTimeOffset (dt.AddTicks (ticks), utc_offset);
}
public DateTimeOffset AddYears (int years)
{
return new DateTimeOffset (dt.AddYears (years), utc_offset);
}
public static int Compare (DateTimeOffset first, DateTimeOffset second)
{
return first.CompareTo (second);
}
public int CompareTo (DateTimeOffset other)
{
return UtcDateTime.CompareTo (other.UtcDateTime);
}
int IComparable.CompareTo (object obj)
{
return CompareTo ((DateTimeOffset) obj);
}
public static bool operator == (DateTimeOffset left, DateTimeOffset right)
{
return left.Equals (right);
}
public bool Equals (DateTimeOffset other)
{
return UtcDateTime == other.UtcDateTime;
}
public override bool Equals (object obj)
{
if (obj is DateTimeOffset)
return UtcDateTime == ((DateTimeOffset) obj).UtcDateTime;
return false;
}
public static bool Equals (DateTimeOffset first, DateTimeOffset second)
{
return first.Equals (second);
}
public bool EqualsExact (DateTimeOffset other)
{
return dt == other.dt && utc_offset == other.utc_offset;
}
public static DateTimeOffset FromFileTime (long fileTime)
{
if (fileTime < 0 || fileTime > MaxValue.Ticks)
throw new ArgumentOutOfRangeException ("fileTime is less than zero or greater than DateTimeOffset.MaxValue.Ticks.");
return new DateTimeOffset (DateTime.FromFileTime (fileTime), TimeZone.CurrentTimeZone.GetUtcOffset (DateTime.FromFileTime (fileTime)));
}
public override int GetHashCode ()
{
return dt.GetHashCode () ^ utc_offset.GetHashCode ();
}
[System.Security.Permissions.SecurityPermission (System.Security.Permissions.SecurityAction.LinkDemand, SerializationFormatter = true)]
void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
{
if (info == null)
throw new ArgumentNullException ("info");
// An example SOAP serialization on MSFT is the following, so field
// names "DateTime" and "OffsetMinutes":
// <SOAP-ENV:Envelope ...>
// <SOAP-ENV:Body>
// <a1:DateTimeOffset id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/ns/System">
// <DateTime xsi:type="xsd:dateTime">2007-01-02T12:30:50.0000000+00:00</DateTime>
// <OffsetMinutes>0</OffsetMinutes>
// </a1:DateTimeOffset>
// </SOAP-ENV:Body>
// </SOAP-ENV:Envelope>
DateTime dt0 = new DateTime (dt.Ticks).Subtract (utc_offset);
info.AddValue ("DateTime", dt0);
// MSFT BinaryFormatter output contains primitive code 6, i.e. Int16.
info.AddValue ("OffsetMinutes", (Int16)utc_offset.TotalMinutes);
}
[System.Security.Permissions.SecurityPermission (System.Security.Permissions.SecurityAction.LinkDemand, SerializationFormatter = true)]
private DateTimeOffset(SerializationInfo info, StreamingContext context)
{
DateTime dt0 = (DateTime)info.GetValue ("DateTime", typeof(DateTime));
Int16 totalMinutes = info.GetInt16 ("OffsetMinutes");
utc_offset = TimeSpan.FromMinutes(totalMinutes);
dt = dt0.Add(utc_offset);
}
public static bool operator > (DateTimeOffset left, DateTimeOffset right)
{
return left.UtcDateTime > right.UtcDateTime;
}
public static bool operator >= (DateTimeOffset left, DateTimeOffset right)
{
return left.UtcDateTime >= right.UtcDateTime;
}
public static implicit operator DateTimeOffset (DateTime dateTime)
{
return new DateTimeOffset (dateTime);
}
public static bool operator != (DateTimeOffset left, DateTimeOffset right)
{
return left.UtcDateTime != right.UtcDateTime;
}
public static bool operator < (DateTimeOffset left, DateTimeOffset right)
{
return left.UtcDateTime < right.UtcDateTime;
}
public static bool operator <= (DateTimeOffset left, DateTimeOffset right)
{
return left.UtcDateTime <= right.UtcDateTime;
}
[MonoTODO]
void IDeserializationCallback.OnDeserialization (object sender)
{
}
public static DateTimeOffset Parse (string input)
{
return Parse (input, null);
}
public static DateTimeOffset Parse (string input, IFormatProvider formatProvider)
{
return Parse (input, formatProvider, DateTimeStyles.AllowWhiteSpaces);
}
public static DateTimeOffset Parse (string input, IFormatProvider formatProvider, DateTimeStyles styles)
{
if (input == null)
throw new ArgumentNullException ("input");
DateTime d;
DateTimeOffset dto;
Exception exception = null;
if (!DateTime.CoreParse (input, formatProvider, styles, out d, out dto, true, ref exception))
throw exception;
if (d.Ticks != 0 && dto.Ticks == 0)
throw new ArgumentOutOfRangeException ("The UTC representation falls outside the 1-9999 year range");
return dto;
}
public static DateTimeOffset ParseExact (string input, string format, IFormatProvider formatProvider)
{
return ParseExact (input, format, formatProvider, DateTimeStyles.AssumeLocal);
}
public static DateTimeOffset ParseExact (string input, string format, IFormatProvider formatProvider, DateTimeStyles styles)
{
if (format == null)
throw new ArgumentNullException ("format");
if (format == String.Empty)
throw new FormatException ("format is an empty string");
return ParseExact (input, new string [] {format}, formatProvider, styles);
}
public static DateTimeOffset ParseExact (string input, string[] formats, IFormatProvider formatProvider, DateTimeStyles styles)
{
if (input == null)
throw new ArgumentNullException ("input");
if (input == String.Empty)
throw new FormatException ("input is an empty string");
if (formats == null)
throw new ArgumentNullException ("formats");
if (formats.Length == 0)
throw new FormatException ("Invalid format specifier");
if ((styles & DateTimeStyles.AssumeLocal) != 0 && (styles & DateTimeStyles.AssumeUniversal) != 0)
throw new ArgumentException ("styles parameter contains incompatible flags");
DateTimeOffset result;
if (!ParseExact (input, formats, DateTimeFormatInfo.GetInstance (formatProvider), styles, out result))
throw new FormatException ("Invalid format string");
return result;
}
private static bool ParseExact (string input, string [] formats,
DateTimeFormatInfo dfi, DateTimeStyles styles, out DateTimeOffset ret)
{
foreach (string format in formats)
{
if (format == null || format == String.Empty)
throw new FormatException ("Invalid format string");
DateTimeOffset result;
if (DoParse (input, format, false, out result, dfi, styles)) {
ret = result;
return true;
}
}
ret = DateTimeOffset.MinValue;
return false;
}
private static bool DoParse (string input,
string format,
bool exact,
out DateTimeOffset result,
DateTimeFormatInfo dfi,
DateTimeStyles styles)
{
if ((styles & DateTimeStyles.AllowLeadingWhite) != 0) {
format = format.TrimStart (null);
input = input.TrimStart (null);
}
if ((styles & DateTimeStyles.AllowTrailingWhite) != 0) {
format = format.TrimEnd (null);
input = input.TrimEnd (null);
}
bool allow_white_spaces = false;
if ((styles & DateTimeStyles.AllowInnerWhite) != 0)
allow_white_spaces = true;
bool useutc = false, use_invariants = false;
if (format.Length == 1)
format = DateTimeUtils.GetStandardPattern (format[0], dfi, out useutc, out use_invariants, true);
int year = -1;
int month = -1;
int day = -1;
int partial_hour = -1; // for 'hh tt' formats
int hour = -1;
int minute = -1;
int second = -1;
double fraction = -1;
int temp_int = -1;
TimeSpan offset = TimeSpan.MinValue;
result = DateTimeOffset.MinValue;
int fi = 0; //format iterator
int ii = 0; //input iterator
while (fi < format.Length) {
int tokLen;
char ch = format [fi];
switch (ch) {
case 'd':
tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
if (day != -1 || tokLen > 4)
return false;
if (tokLen <= 2)
ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out day);
else
ii += ParseEnum (input, ii, tokLen == 3 ? dfi.AbbreviatedDayNames : dfi.DayNames, allow_white_spaces, out temp_int);
break;
case 'f':
tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
ii += ParseNumber (input, ii, tokLen, true, allow_white_spaces, out temp_int);
if (fraction >= 0 || tokLen > 7 || temp_int == -1)
return false;
fraction = (double)temp_int / Math.Pow (10, tokLen);
break;
case 'F':
tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
int digits;
int read = ParseNumber (input, ii, tokLen, true, allow_white_spaces, out temp_int, out digits);
if (temp_int == -1)
ii += ParseNumber (input, ii, digits, true, allow_white_spaces, out temp_int);
else
ii += read;
if (fraction >= 0 || tokLen > 7 || temp_int == -1)
return false;
fraction = (double)temp_int / Math.Pow (10, digits);
break;
case 'h':
tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
if (hour != -1 || tokLen > 2)
return false;
ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out temp_int);
if (temp_int == -1)
return false;
if (partial_hour == -1)
partial_hour = temp_int;
else
hour = partial_hour + temp_int;
break;
case 'H':
tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
if (hour != -1 || tokLen > 2)
return false;
ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out hour);
break;
case 'm':
tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
if (minute != -1 || tokLen > 2)
return false;
ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out minute);
break;
case 'M':
tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
if (month != -1 || tokLen > 4)
return false;
if (tokLen <= 2)
ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out month);
else {
ii += ParseEnum (input, ii, tokLen == 3 ? dfi.AbbreviatedMonthNames : dfi.MonthNames, allow_white_spaces, out month);
month += 1;
}
break;
case 's':
tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
if (second != -1 || tokLen > 2)
return false;
ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out second);
break;
case 't':
tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
if (hour != -1 || tokLen > 2)
return false;
ii += ParseEnum (input, ii,
tokLen == 1 ? new string[] {new string (dfi.AMDesignator[0], 1), new string (dfi.PMDesignator[0], 0)}
: new string[] {dfi.AMDesignator, dfi.PMDesignator},
allow_white_spaces, out temp_int);
if (temp_int == -1)
return false;
if (partial_hour == -1)
partial_hour = temp_int * 12;
else
hour = partial_hour + temp_int * 12;
break;
case 'y':
if (year != -1)
return false;
tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
if (tokLen <= 2) {
ii += ParseNumber (input, ii, 2, tokLen == 2, allow_white_spaces, out year);
if (year != -1)
year += DateTime.Now.Year - DateTime.Now.Year % 100;
} else if (tokLen <= 4) { // yyy and yyyy accept up to 5 digits with leading 0
int digit_parsed;
ii += ParseNumber (input, ii, 5, false, allow_white_spaces, out year, out digit_parsed);
if (digit_parsed < tokLen || (digit_parsed > tokLen && (year / Math.Pow (10, digit_parsed - 1) < 1)))
return false;
} else
ii += ParseNumber (input, ii, tokLen, true, allow_white_spaces, out year);
break;
case 'z':
tokLen = DateTimeUtils.CountRepeat (format, fi, ch);
if (offset != TimeSpan.MinValue || tokLen > 3)
return false;
int off_h, off_m = 0, sign;
temp_int = 0;
ii += ParseEnum (input, ii, new string [] {"-", "+"}, allow_white_spaces, out sign);
ii += ParseNumber (input, ii, 2, tokLen != 1, false, out off_h);
if (tokLen == 3) {
ii += ParseEnum (input, ii, new string [] {dfi.TimeSeparator}, false, out temp_int);
ii += ParseNumber (input, ii, 2, true, false, out off_m);
}
if (off_h == -1 || off_m == -1 || sign == -1)
return false;
if (sign == 0)
sign = -1;
offset = new TimeSpan (sign * off_h, sign * off_m, 0);
break;
case ':':
tokLen = 1;
ii += ParseEnum (input, ii, new string [] {dfi.TimeSeparator}, false, out temp_int);
if (temp_int == -1)
return false;
break;
case '/':
tokLen = 1;
ii += ParseEnum (input, ii, new string [] {dfi.DateSeparator}, false, out temp_int);
if (temp_int == -1)
return false;
break;
case '%':
tokLen = 1;
if (fi != 0)
return false;
break;
case ' ':
tokLen = 1;
ii += ParseChar (input, ii, ' ', false, out temp_int);
if (temp_int == -1)
return false;
break;
case '\\':
tokLen = 2;
ii += ParseChar (input, ii, format [fi + 1], allow_white_spaces, out temp_int);
if (temp_int == -1)
return false;
break;
default:
//Console.WriteLine ("un-parsed character: {0}", ch);
tokLen = 1;
ii += ParseChar (input, ii, format [fi], allow_white_spaces, out temp_int);
if (temp_int == -1)
return false;
break;
}
fi += tokLen;
}
//Console.WriteLine ("{0}-{1}-{2} {3}:{4} {5}", year, month, day, hour, minute, offset);
if (offset == TimeSpan.MinValue && (styles & DateTimeStyles.AssumeLocal) != 0)
offset = TimeZone.CurrentTimeZone.GetUtcOffset (DateTime.Now);
if (offset == TimeSpan.MinValue && (styles & DateTimeStyles.AssumeUniversal) != 0)
offset = TimeSpan.Zero;
if (hour < 0) hour = 0;
if (minute < 0) minute = 0;
if (second < 0) second = 0;
if (fraction < 0) fraction = 0;
if (year > 0 && month > 0 && day > 0) {
result = new DateTimeOffset (year, month, day, hour, minute, second, 0, offset);
result = result.AddSeconds (fraction);
if ((styles & DateTimeStyles.AdjustToUniversal) != 0)
result = result.ToUniversalTime ();
return true;
}
return false;
}
private static int ParseNumber (string input, int pos, int digits, bool leading_zero, bool allow_leading_white, out int result)
{
int digit_parsed;
return ParseNumber (input, pos, digits, leading_zero, allow_leading_white, out result, out digit_parsed);
}
private static int ParseNumber (string input, int pos, int digits, bool leading_zero, bool allow_leading_white, out int result, out int digit_parsed)
{
int char_parsed = 0;
digit_parsed = 0;
result = 0;
for (; allow_leading_white && pos < input.Length && input[pos] == ' '; pos++)
char_parsed++;
for (; pos < input.Length && Char.IsDigit (input[pos]) && digits > 0; pos ++, char_parsed++, digit_parsed++, digits --)
result = 10 * result + (byte) (input[pos] - '0');
if (leading_zero && digits > 0)
result = -1;
if (digit_parsed == 0)
result = -1;
return char_parsed;
}
private static int ParseEnum (string input, int pos, string [] enums, bool allow_leading_white, out int result)
{
int char_parsed = 0;
result = -1;
for (; allow_leading_white && pos < input.Length && input[pos] == ' '; pos++)
char_parsed ++;
for (int i = 0; i < enums.Length; i++)
if (input.Substring(pos).StartsWith (enums [i])) {
result = i;
break;
}
if (result >= 0)
char_parsed += enums[result].Length;
return char_parsed;
}
private static int ParseChar (string input, int pos, char c, bool allow_leading_white, out int result)
{
int char_parsed = 0;
result = -1;
for (; allow_leading_white && pos < input.Length && input[pos] == ' '; pos++, char_parsed++)
;
if (pos < input.Length && input[pos] == c){
result = (int) c;
char_parsed ++;
}
return char_parsed;
}
public TimeSpan Subtract (DateTimeOffset value)
{
return UtcDateTime - value.UtcDateTime;
}
public DateTimeOffset Subtract (TimeSpan value)
{
return Add (-value);
}
public static TimeSpan operator - (DateTimeOffset left, DateTimeOffset right)
{
return left.Subtract (right);
}
public static DateTimeOffset operator - (DateTimeOffset dateTimeTz, TimeSpan timeSpan)
{
return dateTimeTz.Subtract (timeSpan);
}
public long ToFileTime ()
{
return UtcDateTime.ToFileTime ();
}
public DateTimeOffset ToLocalTime ()
{
return new DateTimeOffset (UtcDateTime.ToLocalTime (), TimeZone.CurrentTimeZone.GetUtcOffset (UtcDateTime.ToLocalTime ()));
}
public DateTimeOffset ToOffset (TimeSpan offset)
{
return new DateTimeOffset (dt - utc_offset + offset, offset);
}
public override string ToString ()
{
return ToString (null, null);
}
public string ToString (IFormatProvider formatProvider)
{
return ToString (null, formatProvider);
}
public string ToString (string format)
{
return ToString (format, null);
}
public string ToString (string format, IFormatProvider formatProvider)
{
DateTimeFormatInfo dfi = DateTimeFormatInfo.GetInstance(formatProvider);
if (format == null || format == String.Empty)
format = dfi.ShortDatePattern + " " + dfi.LongTimePattern + " zzz";
bool to_utc = false, use_invariant = false;
if (format.Length == 1) {
char fchar = format [0];
try {
format = DateTimeUtils.GetStandardPattern (fchar, dfi, out to_utc, out use_invariant, true);
} catch {
format = null;
}
if (format == null)
throw new FormatException ("format is not one of the format specifier characters defined for DateTimeFormatInfo");
}
return to_utc ? DateTimeUtils.ToString (UtcDateTime, TimeSpan.Zero, format, dfi) : DateTimeUtils.ToString (DateTime, Offset, format, dfi);
}
public DateTimeOffset ToUniversalTime ()
{
return new DateTimeOffset (UtcDateTime, TimeSpan.Zero);
}
public static bool TryParse (string input, out DateTimeOffset result)
{
try {
result = Parse (input);
return true;
} catch {
result = MinValue;
return false;
}
}
public static bool TryParse (string input, IFormatProvider formatProvider, DateTimeStyles styles, out DateTimeOffset result)
{
try {
result = Parse (input, formatProvider, styles);
return true;
} catch {
result = MinValue;
return false;
}
}
public static bool TryParseExact (string input, string format, IFormatProvider formatProvider, DateTimeStyles styles, out DateTimeOffset result)
{
try {
result = ParseExact (input, format, formatProvider, styles);
return true;
} catch {
result = MinValue;
return false;
}
}
public static bool TryParseExact (string input, string[] formats, IFormatProvider formatProvider, DateTimeStyles styles, out DateTimeOffset result)
{
try {
result = ParseExact (input, formats, formatProvider, styles);
return true;
} catch {
result = MinValue;
return false;
}
}
public DateTime Date {
get { return DateTime.SpecifyKind (dt.Date, DateTimeKind.Unspecified); }
}
public DateTime DateTime {
get { return DateTime.SpecifyKind (dt, DateTimeKind.Unspecified); }
}
public int Day {
get { return dt.Day; }
}
public DayOfWeek DayOfWeek {
get { return dt.DayOfWeek; }
}
public int DayOfYear {
get { return dt.DayOfYear; }
}
public int Hour {
get { return dt.Hour; }
}
public DateTime LocalDateTime {
get { return UtcDateTime.ToLocalTime (); }
}
public int Millisecond {
get { return dt.Millisecond; }
}
public int Minute {
get { return dt.Minute; }
}
public int Month {
get { return dt.Month; }
}
public static DateTimeOffset Now {
get { return new DateTimeOffset (DateTime.Now);}
}
public TimeSpan Offset {
get { return utc_offset; }
}
public int Second {
get { return dt.Second; }
}
public long Ticks {
get { return dt.Ticks; }
}
public TimeSpan TimeOfDay {
get { return dt.TimeOfDay; }
}
public DateTime UtcDateTime {
get { return DateTime.SpecifyKind (dt - utc_offset, DateTimeKind.Utc); }
}
public static DateTimeOffset UtcNow {
get { return new DateTimeOffset (DateTime.UtcNow); }
}
public long UtcTicks {
get { return UtcDateTime.Ticks; }
}
public int Year {
get { return dt.Year; }
}
}
}
#endif
|