All files / src/datatypes datetime.coffee

94.07% Statements 333/354
98.09% Branches 205/209
94.12% Functions 64/68
94.22% Lines 310/329

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 9731x 1x       1x       1x   1x 1x   13100x   2x 13098x 13098x 13098x 13098x 13098x 13098x 13098x 11796x 11796x   4x   13094x   13094x     336x 336x   7x     1x 5x   3576x                                       179075x     167138x       1x                                                                               123x       251x 1x       250x 250x         14x         126x 126x   126x     8x   3x       30x 30x   34x 34x   8x 8x   54x 54x   32x 32x   30x 30x   30x 30x             186x 186x 186x 186x 186x 186x             1x 15x 32x       322x   322x 322x                 644x   129x   515x                         220x       220x   220x 220x   61x   60x   4x                           297x 297x 285x 271x 254x 214x 189x 162x     1024x 1024x 1024x                       3586x 5886x   5634x         13x 5634x   252x   252x                                                 192x   192x   183x   172x   153x   104x   73x   40x     153x 153x 153x 153x 153x                                 15x 558x     130x   9x 9x 30x             1x   1x 1x   1041x   1x 1040x 1040x 1040x   4x   1036x   1036x     2256x 2256x       1x                                               7x 1x   66x 66x     12x 12x   12x 12x   8x 8x             1x 15x 32x       9x     128x 128x             256x 256x 256x 256x           256x   39x   217x                 88x       88x   88x 88x   10x   16x                       58x 58x 50x 44x       260x 260x                   729x     1x                     130x   130x   117x   106x                             3x   2x 2x 3x       1x     1x       1x   26x     20x 4x         1x       1x   134x   28x 6x 2x     1x             4x   1870x         541x       45x       47x       1x   1x   104x   99x     18x       32x   7414x         3237x         25x   46x       120x     252x         1x   1x   44x   44x     5x       18x   2598x         728x     312x           32x   1x       116x     45x         1x   1x   44x   48x     5x       18x   2504x         689x     305x           30x   1x       114x     45x         1x   1x   39x   33x     5x       18x   1134x         210x     224x           17x   1x       51x     27x         1x   1x   36x   35x     5x       18x   1264x         149x     304x           24x   1x       51x     27x         1x 3657x 2x       4x 4x     3655x               108x 216x 516x 108x     3655x 3655x 7310x 23307x       216x 444x       1x   16x   30x   24x   24x   20x   12x       1x   16x   30x   24x   24x   20x   12x     1x       30x   1x       1x 1x 1039x       1039x 3117x   1x   1038x     1x 1x 13097x                                                                 13097x 419104x   1x         13096x     1x   13096x     12342x   13096x 12342x   330x   12766x         1x 1x
{ Uncertainty } = require './uncertainty'
{ jsDate,
  normalizeMillisecondsField,
  normalizeMillisecondsFieldInString,
  getTimezoneSeparatorFromString } = require '../util/util'
moment = require 'moment'
 
class DateTime
  @Unit: { YEAR: 'year', MONTH: 'month', WEEK: 'week', DAY: 'day', HOUR: 'hour', MINUTE: 'minute', SECOND: 'second', MILLISECOND: 'millisecond' }
  @FIELDS: [@Unit.YEAR, @Unit.MONTH, @Unit.DAY, @Unit.HOUR, @Unit.MINUTE, @Unit.SECOND, @Unit.MILLISECOND]
 
  @parse: (string) ->
    return null if string is null
 
    matches = /(\d{4})(-(\d{2}))?(-(\d{2}))?(T((\d{2})(\:(\d{2})(\:(\d{2})(\.(\d+))?)?)?)?(Z|(([+-])(\d{2})(\:?(\d{2}))?))?)?/.exec string
 
    return null unless matches?
    years= matches[1]
    months= matches[3]
    days= matches[5]
    hours= matches[8]
    minutes= matches[10]
    seconds= matches[12]
    milliseconds= matches[14]
    milliseconds= normalizeMillisecondsField(milliseconds) if milliseconds?
    string = normalizeMillisecondsFieldInString(string, matches[14]) if milliseconds?
 
    return null if !isValidDateTimeStringFormat(string)
 
    args = [years, months, days, hours, minutes, seconds, milliseconds]
    # convert them all to integers
    args = ((if arg? then parseInt(arg,10)) for arg in args)
    # convert timezone offset to decimal and add it to arguments
    if matches[18]?
      num = parseInt(matches[18],10) + (if matches[20]? then parseInt(matches[20],10) / 60 else 0)
      args.push(if matches[17] is '+' then num else num * -1)
    else if matches[15] == 'Z'
      args.push(0)
    new DateTime(args...)
 
  @fromJSDate: (date, timezoneOffset) -> #This is from a JS Date, not a CQL Date
    if (date instanceof DateTime) then return date
    if timezoneOffset?
      date = new jsDate(date.getTime() + (timezoneOffset * 60 * 60 * 1000))
      new DateTime(
        date.getUTCFullYear(),
        date.getUTCMonth() + 1,
        date.getUTCDate(),
        date.getUTCHours(),
        date.getUTCMinutes(),
        date.getUTCSeconds(),
        date.getUTCMilliseconds(),
        timezoneOffset)
    else
      new DateTime(
        date.getFullYear(),
        date.getMonth() + 1,
        date.getDate(),
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
        date.getMilliseconds())
 
  constructor: (@year=null, @month=null, @day=null, @hour=null, @minute=null, @second=null, @millisecond=null, @timezoneOffset) ->
    # from the spec: If no timezone is specified, the timezone of the evaluation request timestamp is used.
    if not @timezoneOffset?
      @timezoneOffset = (new jsDate()).getTimezoneOffset() / 60 * -1
 
  # Define a simple getter to allow type-checking of this class without instanceof
  # and in a way that survives minification (as opposed to checking constructor.name)
  Object.defineProperties @prototype,
    isDateTime:
      get: -> true
 
  copy: () ->
    new DateTime(@year, @month, @day, @hour, @minute, @second, @millisecond, @timezoneOffset)
 
  successor: () ->
    if @millisecond?
      @add(1,DateTime.Unit.MILLISECOND)
    else if @second?
      @add(1,DateTime.Unit.SECOND)
    else if @minute?
      @add(1,DateTime.Unit.MINUTE)
    else if @hour?
      @add(1,DateTime.Unit.HOUR)
    else if @day?
      @add(1,DateTime.Unit.DAY)
    else if @month?
      @add(1,DateTime.Unit.MONTH)
    else if @year?
      @add(1,DateTime.Unit.YEAR)
 
  predecessor: () ->
    if @millisecond?
      @add(-1,DateTime.Unit.MILLISECOND)
    else if @second?
      @add(-1,DateTime.Unit.SECOND)
    else if @minute?
      @add(-1,DateTime.Unit.MINUTE)
    else if @hour?
      @add(-1,DateTime.Unit.HOUR)
    else if @day?
      @add(-1,DateTime.Unit.DAY)
    else if @month?
      @add(-1,DateTime.Unit.MONTH)
    else if @year?
      @add(-1,DateTime.Unit.YEAR)
 
  convertToTimezoneOffset: (timezoneOffset = 0) ->
    d = DateTime.fromJSDate(@toJSDate(), timezoneOffset)
    d.reducedPrecision(@getPrecision())
 
  differenceBetween: (other, unitField) ->
    other = @_implicitlyConvert(other)
    if not(other instanceof DateTime) then return null
 
    # According to CQL spec, to calculate difference, you can just floor lesser precisions and do a duration
    # Make copies since we'll be flooring values and mucking with timezones
    a = @copy()
    b = other.copy()
    # Use moment.js for day or finer granularity due to the daylight savings time fall back/spring forward
    if unitField == DateTime.Unit.MONTH || unitField == DateTime.Unit.YEAR || unitField == DateTime.Unit.WEEK || unitField == DateTime.Unit.DAY
      # The dates need to agree on where the boundaries are, so we must normalize to the same time zone
      if a.timezoneOffset isnt b.timezoneOffset
        b = b.convertToTimezoneOffset(a.timezoneOffset)
 
      # JS always represents dates in the current locale, but in locales with DST, we want to account for the
      # potential difference in offset from one date to the other.  We try to simulate them being in the same
      # timezone, because we don't want midnight to roll back to 11:00pm since that will mess up day boundaries.
      aJS = a.toJSDate(true)
      bJS = b.toJSDate(true)
      # Set tzDiff to zero if a and b are both UTC (UTC is not subject to DST)
      tzDiff = if a.isUTC() && b.isUTC() then 0 else aJS.getTimezoneOffset() - bJS.getTimezoneOffset()
      if (tzDiff != 0)
        # Since we'll be doing duration later, account for timezone offset by adding to the time (if possible)
        if b.year? and b.month? and b.day? and b.hour? and b.minute? then b = b.add(tzDiff, DateTime.Unit.MINUTE)
        else if b.year? and b.month? and b.day? and b.hour? then b = b.add(tzDiff/60, DateTime.Unit.HOUR)
        else b.timezoneOffset = b.timezoneOffset + (tzDiff/60)
 
    # Now floor lesser precisions before we go on to calculate duration
    if unitField == DateTime.Unit.YEAR
      a = new DateTime(a.year, 1, 1, 12, 0, 0, 0, a.timezoneOffset)
      b = new DateTime(b.year, 1, 1, 12, 0, 0, 0, b.timezoneOffset)
    else if unitField == DateTime.Unit.MONTH
      a = new DateTime(a.year, a.month, 1, 12, 0, 0, 0, a.timezoneOffset)
      b = new DateTime(b.year, b.month, 1, 12, 0, 0, 0, b.timezoneOffset)
    else if unitField == DateTime.Unit.WEEK
      a = @_floorWeek(a)
      b = @_floorWeek(b)
    else if unitField == DateTime.Unit.DAY
      a = new DateTime(a.year, a.month, a.day, 12, 0, 0, 0, a.timezoneOffset)
      b = new DateTime(b.year, b.month, b.day, 12, 0, 0, 0, b.timezoneOffset)
    else if unitField == DateTime.Unit.HOUR
      a = new DateTime(a.year, a.month, a.day, a.hour, 30, 0, 0, a.timezoneOffset)
      b = new DateTime(b.year, b.month, b.day, b.hour, 30, 0, 0, b.timezoneOffset)
    else if unitField == DateTime.Unit.MINUTE
      a = new DateTime(a.year, a.month, a.day, a.hour, a.minute, 0, 0, a.timezoneOffset)
      b = new DateTime(b.year, b.month, b.day, b.hour, b.minute, 0, 0, b.timezoneOffset)
    else if unitField == DateTime.Unit.SECOND
      a = new DateTime(a.year, a.month, a.day, a.hour, a.minute, a.second, 0, a.timezoneOffset)
      b = new DateTime(b.year, b.month, b.day, b.hour, b.minute, b.second, 0, b.timezoneOffset)
 
    # Because moment.js handles years and months differently, use the existing durationBetween for those
    # Finer granularity times can be handled by the DST-aware moment.js library.
    if unitField == DateTime.Unit.YEAR || unitField == DateTime.Unit.MONTH
      a.durationBetween(b, unitField)
    else
      aUncertainty = a.toUncertainty()
      bUncertainty = b.toUncertainty()
      aLowMoment = moment(aUncertainty.low).utc()
      aHighMoment = moment(aUncertainty.high).utc()
      bLowMoment = moment(bUncertainty.low).utc()
      bHighMoment = moment(bUncertainty.high).utc()
      # moment uses the plural form of the unitField
      new Uncertainty(bLowMoment.diff(aHighMoment, unitField + 's'), bHighMoment.diff(aLowMoment, unitField + 's'))
 
  _floorWeek: (d) ->
    # To "floor" a week, we need to go back to the last Sunday (that's when getDay() == 0 in javascript)
    # But if we don't know the day, then just return it as-is
    if (not d.day?) then return d
    floored = new jsDate(d.year, d.month-1, d.day)
    floored.setDate(floored.getDate() - 1) while floored.getDay() > 0
    new DateTime(floored.getFullYear(), floored.getMonth()+1, floored.getDate(), 12, 0, 0, 0, d.timezoneOffset)
 
  durationBetween: (other, unitField) ->
    other = @_implicitlyConvert(other)
    if not(other instanceof DateTime) then return null
    a = @toUncertainty()
    b = other.toUncertainty()
    new Uncertainty(@_durationBetweenDates(a.high, b.low, unitField), @_durationBetweenDates(a.low, b.high, unitField))
 
  # NOTE: a and b are real JS dates -- not DateTimes
  _durationBetweenDates: (a, b, unitField) ->
    # DurationBetween is different than DifferenceBetween in that DurationBetween counts whole elapsed time periods, but
    # DifferenceBetween counts boundaries.  For example:
    # difference in days between @2012-01-01T23:59:59.999 and @2012-01-02T00:00:00.0 calculates to 1 (since it crosses day boundary)
    # days between @2012-01-01T23:59:59.999 and @2012-01-02T00:00:00.0 calculates to 0 (since there are no full days between them)
    msDiff = b.getTime() - a.getTime()
 
    if msDiff == 0 then return 0
    # If it's a negative delta, we need to use ceiling instead of floor when truncating
    truncFunc = if msDiff > 0 then Math.floor else Math.ceil
    # For ms, s, min, hr, day, and week this is trivial
    if unitField == DateTime.Unit.MILLISECOND then msDiff
    else if unitField == DateTime.Unit.SECOND then truncFunc(msDiff / 1000)
    else if unitField == DateTime.Unit.MINUTE then truncFunc(msDiff / (60 * 1000))
    else if unitField == DateTime.Unit.HOUR then truncFunc(msDiff / (60 * 60 * 1000))
    else if unitField == DateTime.Unit.DAY
      truncFunc(msDiff / (24 * 60 * 60 * 1000))
    else if unitField == DateTime.Unit.WEEK
      truncFunc(msDiff / (7 * 24 * 60 * 60 * 1000))
    # Months and years are trickier since months are variable length
    else if unitField == DateTime.Unit.MONTH or unitField == DateTime.Unit.YEAR
      # First get the rough months, essentially counting month "boundaries"
      months = (b.getFullYear() - a.getFullYear()) * 12 + (b.getMonth() - a.getMonth())
      # Now we need to look at the smaller units to see how they compare.  Since we only care about comparing
      # days and below at this point, it's much easier to bring a up to b so it's in the same month, then
      # we can compare on just the remaining units.
      aInMonth = new jsDate(a.getTime())
      # Remember the original timezone offset because if it changes when we bring it up a month, we need to fix it
      aInMonthOriginalOffset = aInMonth.getTimezoneOffset()
      aInMonth.setMonth(a.getMonth() + months)
      if aInMonthOriginalOffset != aInMonth.getTimezoneOffset()
        aInMonth.setMinutes(aInMonth.getMinutes() + (aInMonthOriginalOffset - aInMonth.getTimezoneOffset()))
      # When a is before b, then if a's smaller units are greater than b's, a whole month hasn't elapsed, so adjust
      if msDiff > 0 and aInMonth > b then months = months - 1
      # When b is before a, then if a's smaller units are less than b's, a whole month hasn't elaspsed backwards, so adjust
      else if msDiff < 0 and aInMonth < b then months = months + 1
      # If this is months, just return them, but if it's years, we need to convert
      if unitField == DateTime.Unit.MONTH
        months
      else
        truncFunc(months/12)
    else
      null
 
  isUTC: () ->
    # A timezoneOffset of 0 indicates UTC time.
    !@timezoneOffset
 
  getPrecision: () ->
    result = null
    if @year? then result = DateTime.Unit.YEAR else return result
    if @month? then result = DateTime.Unit.MONTH else return result
    if @day? then result = DateTime.Unit.DAY else return result
    if @hour? then result = DateTime.Unit.HOUR else return result
    if @minute? then result = DateTime.Unit.MINUTE else return result
    if @second? then result = DateTime.Unit.SECOND else return result
    if @millisecond? then result = DateTime.Unit.MILLISECOND
    result
 
  toUncertainty: (ignoreTimezone = false) ->
    low = @toJSDate(ignoreTimezone)
    high = (new DateTime(
      @year,
      @month ? 12,
      # see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate
      @day ? (new jsDate(@year, @month ? 12, 0)).getDate(),
      @hour ? 23,
      @minute ? 59,
      @second ? 59,
      @millisecond ? 999,
      @timezoneOffset)).toJSDate(ignoreTimezone)
    new Uncertainty(low, high)
 
  toJSDate: (ignoreTimezone = false) ->
    [y, mo, d, h, mi, s, ms] = [ @year, (if @month? then @month-1 else 0), @day ? 1, @hour ? 0, @minute ? 0, @second ? 0, @millisecond ? 0 ]
    if @timezoneOffset? and not ignoreTimezone
      date = new jsDate(jsDate.UTC(y, mo, d, h, mi, s, ms) - (@timezoneOffset * 60 * 60 * 1000))
      # TODO: This fixes any case that would not cross the year boundary due to a timezone.
      # Mainly used to solve the issue with the MIN_DATE_VALUE being converted from
      # year 0001 to year 1900 because of strange JSDate behavior between year 0 and 100
      # Also else case below
      if y < 100 then date.setUTCFullYear(y)
      return date
    else
      date = new jsDate(y, mo, d, h, mi, s, ms)
      if y < 100 then date.setFullYear(y)
      return date
 
  toJSON: () ->
    @toString()
 
  _pad: (num) ->
    String("0" + num).slice(-2)
 
  toString: () ->
    if @isTime() then @toStringTime() else @toStringDateTime()
 
  toStringTime: () ->
    str = 'T'
    if @hour?
      str += + @_pad(@hour)
      if @minute?
        str += ':' + @_pad(@minute)
        if @second?
          str += ':' + @_pad(@second)
          if @millisecond?
            str += '.' + String("00" + @millisecond).slice(-3)
 
    str
 
  toStringDateTime: () ->
    str = ''
    if @year?
      str += @year
      if @month?
        str += '-' + @_pad(@month)
        if @day?
          str += '-' + @_pad(@day)
          if @hour?
            str += 'T' + @_pad(@hour)
            if @minute?
              str += ':' + @_pad(@minute)
              if @second?
                str += ':' + @_pad(@second)
                if @millisecond?
                  str += '.' + String("00" + @millisecond).slice(-3)
 
    if str.indexOf('T') != -1 and @timezoneOffset?
      str += if @timezoneOffset < 0 then '-' else '+'
      offsetHours = Math.floor(Math.abs(@timezoneOffset))
      str += @_pad(offsetHours)
      offsetMin = (Math.abs(@timezoneOffset) - offsetHours) * 60
      str += ':' + @_pad(offsetMin)
 
    str
 
  getDateTime: () ->
    @
 
  getDate: () ->
    new Date(@year, @month, @day)
 
  getTime: () ->
    new DateTime(0, 1, 1, @hour, @minute, @second, @millisecond, @timezoneOffset)
 
  isTime: () ->
    @year == 0 && @month == 1 && @day == 1
 
  _implicitlyConvert: (other) ->
    if (other instanceof Date) then return other.getDateTime()
    return other
 
  reducedPrecision: (unitField = DateTime.Unit.MILLISECOND) ->
    reduced = @copy()
    if unitField isnt DateTime.Unit.MILLISECOND
      fieldIndex = DateTime.FIELDS.indexOf unitField
      fieldsToRemove = DateTime.FIELDS.slice(fieldIndex + 1)
      reduced[field] = null for field in fieldsToRemove
    reduced
 
 
 
class Date
  @Unit: { YEAR: 'year', MONTH: 'month', WEEK: 'week', DAY: 'day' }
  @FIELDS: [@Unit.YEAR, @Unit.MONTH, @Unit.DAY]
 
  @parse: (string) ->
    return null if string is null
 
    matches = /(\d{4})(-(\d{2}))?(-(\d{2}))?/.exec string
 
    return null unless matches?
    years= matches[1]
    months= matches[3]
    days= matches[5]
 
    return null if !isValidDateStringFormat(string)
 
    args = [years, months, days]
    # convert them all to integers
    args = ((if arg? then parseInt(arg,10)) for arg in args)
    new Date(args...)
 
  constructor: (@year=null, @month=null, @day=null) ->
    return
 
  # Define a simple getter to allow type-checking of this class without instanceof
  # and in a way that survives minification (as opposed to checking constructor.name)
  Object.defineProperties @prototype,
    isDate:
      get: -> true
 
  copy: () ->
    new Date(@year, @month, @day)
 
  successor: () ->
    if @day?
      @add(1,Date.Unit.DAY)
    else if @month?
      @add(1,Date.Unit.MONTH)
    else if @year?
      @add(1,Date.Unit.YEAR)
 
  predecessor: () ->
    if @day?
      @add(-1,Date.Unit.DAY)
    else if @month?
      @add(-1,Date.Unit.MONTH)
    else if @year?
      @add(-1,Date.Unit.YEAR)
 
  differenceBetween: (other, unitField) ->
    if (other instanceof DateTime) then return this.getDateTime().differenceBetween(other, unitField)
    if not(other instanceof Date) then return null
 
    a = @
    b = other
    # According to CQL spec, to calculate difference, you can just floor lesser precisions and do a duration
    if unitField == Date.Unit.YEAR
      a = new Date(a.year, 1, 1)
      b = new Date(b.year, 1, 1)
    else if unitField == Date.Unit.MONTH
      a = new Date(a.year, a.month, 1)
      b = new Date(b.year, b.month, 1)
    else if unitField == Date.Unit.WEEK
      a = @_floorWeek(a)
      b = @_floorWeek(b)
 
    a.durationBetween(b, unitField)
 
  _floorWeek: (d) ->
    # To "floor" a week, we need to go back to the last Sunday (that's when getDay() == 0 in javascript)
    # But if we don't know the day, then just return it as-is
    if (not d.day?) then return d
    floored = new jsDate(d.year, d.month-1, d.day)
    floored.setDate(floored.getDate() - 1) while floored.getDay() > 0
    new Date(floored.getFullYear(), floored.getMonth()+1, floored.getDate())
 
  durationBetween: (other, unitField) ->
    if (other instanceof DateTime) then return this.getDateTime().durationBetween(other, unitField)
    if not(other instanceof Date) then return null
 
    a = @toUncertainty()
    b = other.toUncertainty()
    new Uncertainty(@_durationBetweenDates(a.high, b.low, unitField), @_durationBetweenDates(a.low, b.high, unitField))
 
  # NOTE: a and b are real JS dates -- not DateTimes. Also this expects time components to be zero!
  _durationBetweenDates: (a, b, unitField) ->
    #we need to fix offsets to match so we dont get any JS DST interference, to avoid crossing day boundaries put it in the middle of the day
    #DST stuff should only be +/- one hour so this should work
    a.setTime(a.getTime() + (12*60*60*1000)); 
    b.setTime(b.getTime() + (12*60*60*1000)); 
    tzdiff = a.getTimezoneOffset() - b.getTimezoneOffset()
    b.setTime(b.getTime() + (tzdiff*60*1000)); 
 
    # DurationBetween is different than DifferenceBetween in that DurationBetween counts whole elapsed time periods, but
    # DifferenceBetween counts boundaries.  For example:
    # difference in days between @2012-01-01T23:59:59.999 and @2012-01-02T00:00:00.0 calculates to 1 (since it crosses day boundary)
    # days between @2012-01-01T23:59:59.999 and @2012-01-02T00:00:00.0 calculates to 0 (since there are no full days between them)
    msDiff = b.getTime() - a.getTime()
 
    if msDiff == 0 then return 0
    # If it's a negative delta, we need to use ceiling instead of floor when truncating
    truncFunc = if msDiff > 0 then Math.floor else Math.ceil
    # For ms, s, min, hr, day, and week this is trivial
    if unitField == Date.Unit.DAY
      truncFunc(msDiff / (24 * 60 * 60 * 1000))
    else if unitField == Date.Unit.WEEK
      truncFunc(msDiff / (7 * 24 * 60 * 60 * 1000))
    # Months and years are trickier since months are variable length
    else if unitField == Date.Unit.MONTH or unitField == Date.Unit.YEAR
      # First get the rough months, essentially counting month "boundaries"
      months = (b.getFullYear() - a.getFullYear()) * 12 + (b.getMonth() - a.getMonth())
      # Now we need to look at the smaller units to see how they compare.  Since we only care about comparing
      # days and below at this point, it's much easier to bring a up to b so it's in the same month, then
      # we can compare on just the remaining units.
      aInMonth = new jsDate(a.getTime())
      # Remember the original timezone offset because if it changes when we bring it up a month, we need to fix it
      aInMonthOriginalOffset = aInMonth.getTimezoneOffset()
      aInMonth.setMonth(a.getMonth() + months)
      if aInMonthOriginalOffset != aInMonth.getTimezoneOffset()
        aInMonth.setMinutes(aInMonth.getMinutes() + (aInMonthOriginalOffset - aInMonth.getTimezoneOffset()))
      # When a is before b, then if a's smaller units are greater than b's, a whole month hasn't elapsed, so adjust
      if msDiff > 0 and aInMonth > b then months = months - 1
      # When b is before a, then if a's smaller units are less than b's, a whole month hasn't elaspsed backwards, so adjust
      else if msDiff < 0 and aInMonth < b then months = months + 1
      # If this is months, just return them, but if it's years, we need to convert
      if unitField == Date.Unit.MONTH
        months
      else
        truncFunc(months/12)
    else
      null
 
  getPrecision: () ->
    result = null
    if @year? then result = Date.Unit.YEAR else return result
    if @month? then result = Date.Unit.MONTH else return result
    if @day? then result = Date.Unit.DAY else return result
    result
 
  toUncertainty: () ->
    low = @toJSDate()
    high = new Date(
      @year,
      @month ? 12,
      # see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate
      @day ? (new jsDate(@year, @month ? 12, 0)).getDate()
    ).toJSDate()
 
    new Uncertainty(low, high)
 
  toJSDate: () ->
    [y, mo, d] = [ @year, (if @month? then @month-1 else 0), @day ? 1 ]
    new jsDate(y, mo, d)
 
  @fromJSDate: (date) ->
    if (date instanceof Date) then return date
    new Date(
      date.getFullYear(),
      date.getMonth() + 1,
      date.getDate())
 
  toJSON: () ->
    @toString()
 
  toString: () ->
    str = ''
    if @year?
      str += @year.toString()
      if @month?
        str += '-' + @month.toString().padStart(2,"0")
        if @day?
          str += '-' + @day.toString().padStart(2,"0")
    str
 
  getDateTime: () ->
    # from the spec: the result will be a DateTime with the time components set to zero, 
    # except for the timezone offset, which will be set to the timezone offset of the evaluation
    # request timestamp. (this last part is acheived by just not passing in timezone offset)
    if @year? and @month? and @day?
      new DateTime(@year, @month, @day, 0, 0, 0, 0)
    # from spec: no component may be specified at a precision below an unspecified precision.
    # For example, hour may be null, but if it is, minute, second, and millisecond must all be null as well.
    else
      new DateTime(@year, @month, @day)
 
  reducedPrecision: (unitField = Date.Unit.DAY) ->
    reduced = @copy()
    if unitField isnt Date.Unit.DAY
      fieldIndex = Date.FIELDS.indexOf unitField
      fieldsToRemove = Date.FIELDS.slice(fieldIndex + 1)
      reduced[field] = null for field in fieldsToRemove
    reduced
 
# Shared Funtions For Date and DateTime
DateTime.prototype.isPrecise = Date.prototype.isPrecise = () ->
    @constructor.FIELDS.every (field) => @[field]?
 
DateTime.prototype.isImprecise = Date.prototype.isImprecise = () ->
    not @isPrecise()
 
# This function can take another Date-ish object, or a precision string (e.g. 'month')
DateTime.prototype.isMorePrecise = Date.prototype.isMorePrecise = (other) ->
    if typeof other is 'string' and other in @constructor.FIELDS
      return false if not @[other]?
 
    else
      for field in @constructor.FIELDS
        if (other[field]? and not @[field]?) then return false
    
    not @isSamePrecision(other)
 
# This function can take another Date-ish object, or a precision string (e.g. 'month')
DateTime.prototype.isLessPrecise = Date.prototype.isLessPrecise = (other) ->
    not @isSamePrecision(other) and not @isMorePrecise(other)
 
# This function can take another Date-ish object, or a precision string (e.g. 'month')
DateTime.prototype.isSamePrecision = Date.prototype.isSamePrecision = (other) ->
    if typeof other is 'string' and other in @constructor.FIELDS
      return other == @getPrecision()
    
    for field in @constructor.FIELDS
      if (@[field]? and not other[field]?) then return false
      if (not @[field]? and other[field]?) then return false
    true
 
DateTime.prototype.equals = Date.prototype.equals = (other) ->
  # leave with false there is a type mismatch
  unless (@isDate and other.isDate) or (@isDateTime and other.isDateTime)
    return false
 
  # make a copy of other in the correct timezone offset if they don't match.
  if (@timezoneOffset != other.timezoneOffset)
    other = other.convertToTimezoneOffset(@timezoneOffset)
 
  for field in @constructor.FIELDS
    # if both have this precision defined
    if @[field]? and other[field]?
      # if they are different then return with false
      if @[field] != other[field]
        return false
 
    # if both dont have this precision, return true
    else if !@[field]? and !other[field]?
      return true
 
    # otherwise they have inconclusive precision, return null
    else
      return null
  # if we made it here, then all fields matched.
  true
 
DateTime.prototype.sameAs = Date.prototype.sameAs = (other, precision) ->
  if not((other.isDate) or (other.isDateTime))
    return null
  else if @.isDate and other.isDateTime
    return @getDateTime().sameAs(other, precision)
  else if @.isDateTime and other.isDate
    other = other.getDateTime()
 
  if precision? && @constructor.FIELDS.indexOf(precision) < 0
    throw new Error("Invalid precision: #{precision}")
 
  # make a copy of other in the correct timezone offset if they don't match.
  if (@timezoneOffset != other.timezoneOffset)
    other = other.convertToTimezoneOffset(@timezoneOffset)
 
  for field in @constructor.FIELDS
    # if both have this precision defined
    if @[field]? and other[field]?
      # if they are different then return with false
      if @[field] != other[field]
        return false
 
    # if both dont have this precision, return true of precision is not defined
    else if !@[field]? and !other[field]?
      if !precision?
        return true
      else # we havent met precision yet
        return null
 
    # otherwise they have inconclusive precision, return null
    else
      return null
 
    # if precision is defined and we have reached expected precision, we can leave the loop
    break if precision? and precision is field
 
  # if we made it here, then all fields matched.
  true
 
DateTime.prototype.sameOrBefore = Date.prototype.sameOrBefore = (other, precision) ->
  if not((other.isDate) or (other.isDateTime))
    return null
  else if @.isDate and other.isDateTime
    return @getDateTime().sameOrBefore(other, precision)
  else if @.isDateTime and other.isDate
    other = other.getDateTime()
 
  if precision? && @constructor.FIELDS.indexOf(precision) < 0
    throw new Error("Invalid precision: #{precision}")
 
  # make a copy of other in the correct timezone offset if they don't match.
  if (@timezoneOffset != other.timezoneOffset)
    other = other.convertToTimezoneOffset(@timezoneOffset)
 
  for field in @constructor.FIELDS
    # if both have this precision defined
    if @[field]? and other[field]?
      # if this value is less than the other return with true. this is before other
      if @[field] < other[field]
        return true
      # if this value is greater than the other return with false. this is after
      else if @[field] > other[field]
        return false
      # execution continues if the values are the same
 
    # if both dont have this precision, return true if precision is not defined
    else if !@[field]? and !other[field]?
      if !precision?
        return true
      else # we havent met precision yet
        return null
 
    # otherwise they have inconclusive precision, return null
    else
      return null
 
    # if precision is defined and we have reached expected precision, we can leave the loop
    break if precision? and precision is field
 
  # if we made it here, then all fields matched and they are same
  true
 
DateTime.prototype.sameOrAfter = Date.prototype.sameOrAfter = (other, precision) ->
  if not((other.isDate) or (other.isDateTime))
    return null
  else if @.isDate and other.isDateTime
    return @getDateTime().sameOrAfter(other, precision)
  else if @.isDateTime and other.isDate
    other = other.getDateTime()
 
  if precision? && @constructor.FIELDS.indexOf(precision) < 0
    throw new Error("Invalid precision: #{precision}")
 
  # make a copy of other in the correct timezone offset if they don't match.
  if (@timezoneOffset != other.timezoneOffset)
    other = other.convertToTimezoneOffset(@timezoneOffset)
 
  for field in @constructor.FIELDS
    # if both have this precision defined
    if @[field]? and other[field]?
      # if this value is greater than the other return with true. this is after other
      if @[field] > other[field]
        return true
      # if this value is greater than the other return with false. this is before
      else if @[field] < other[field]
        return false
      # execution continues if the values are the same
 
    # if both dont have this precision, return true if precision is not defined
    else if !@[field]? and !other[field]?
      if !precision?
        return true
      else # we havent met precision yet
        return null
 
    # otherwise they have inconclusive precision, return null
    else
      return null
 
    # if precision is defined and we have reached expected precision, we can leave the loop
    break if precision? and precision is field
 
  # if we made it here, then all fields matched and they are same
  true
 
DateTime.prototype.before = Date.prototype.before = (other, precision) ->
  if not((other.isDate) or (other.isDateTime))
    return null
  else if @.isDate and other.isDateTime
    return @getDateTime().before(other, precision)
  else if @.isDateTime and other.isDate
    other = other.getDateTime()
 
  if precision? && @constructor.FIELDS.indexOf(precision) < 0
    throw new Error("Invalid precision: #{precision}")
 
  # make a copy of other in the correct timezone offset if they don't match.
  if (@timezoneOffset != other.timezoneOffset)
    other = other.convertToTimezoneOffset(@timezoneOffset)
 
  for field in @constructor.FIELDS
    # if both have this precision defined
    if @[field]? and other[field]?
      # if this value is less than the other return with true. this is before other
      if @[field] < other[field]
        return true
      # if this value is greater than the other return with false. this is after
      else if @[field] > other[field]
        return false
      # execution continues if the values are the same
 
    # if both dont have this precision, return false if precision is not defined
    else if !@[field]? and !other[field]?
      if !precision?
        return false
      else # we havent met precision yet
        return null
 
    # otherwise they have inconclusive precision, return null
    else
      return null
 
    # if precision is defined and we have reached expected precision, we can leave the loop
    break if precision? and precision is field
 
  # if we made it here, then all fields matched and they are same
  false
 
DateTime.prototype.after = Date.prototype.after = (other, precision) ->
  if not((other.isDate) or (other.isDateTime))
    return null
  else if @.isDate and other.isDateTime
    return @getDateTime().after(other, precision)
  else if @.isDateTime and other.isDate
    other = other.getDateTime()
 
  if precision? && @constructor.FIELDS.indexOf(precision) < 0
    throw new Error("Invalid precision: #{precision}")
 
  # make a copy of other in the correct timezone offset if they don't match.
  if (@timezoneOffset != other.timezoneOffset)
    other = other.convertToTimezoneOffset(@timezoneOffset)
 
  for field in @constructor.FIELDS
    # if both have this precision defined
    if @[field]? and other[field]?
      # if this value is greater than the other return with true. this is after other
      if @[field] > other[field]
        return true
      # if this value is greater than the other return with false. this is before
      else if @[field] < other[field]
        return false
      # execution continues if the values are the same
 
    # if both dont have this precision, return false if precision is not defined
    else if !@[field]? and !other[field]?
      if !precision?
        return false
      else # we havent met precision yet
        return null
 
    # otherwise they have inconclusive precision, return null
    else
      return null
 
    # if precision is defined and we have reached expected precision, we can leave the loop
    break if precision? and precision is field
 
  # if we made it here, then all fields matched and they are same
  false
 
DateTime.prototype.add = Date.prototype.add = (offset, field) ->
  result = @copy()
  return result if offset == 0
 
  # If weeks, convert to days
  if field == @constructor.Unit.WEEK
    offset = offset * 7
    field = @constructor.Unit.DAY
 
 
  offsetIsMorePrecise = not result[field]? #whether the quantity we are adding is more precise than @
  # From the spec: "The operation is performed by converting the time-based quantity to the most precise value
  # specified in the date/time (truncating any resulting decimal portion) and then adding it to the date/time value."
  # However, since you can't really convert e.g. days to months,  if @ is less precise than the field being added, we can
  # "floor" UP to the incoming field precision, then add the offset, then reduce back down to original precision.
  # For negative offsets, we use the cieling
  if offsetIsMorePrecise
    result.year = new jsDate().getFullYear() if not @year #in case there is no year, proceed as if in this year, year will be nullified later
    fieldFloorOrCiel = if offset >= 0 then @getFieldFloor else @getFieldCieling
    for f in @constructor.FIELDS # this relies on FIELDS being sorted least to most precise
      result[f] = result[f] ? fieldFloorOrCiel.call(result,f)
      break if result[field]?
 
  # Increment the field, then round-trip to JS date and back for calendar math
  result[field] = result[field] + offset
  normalized = @constructor.fromJSDate(result.toJSDate(), @timezoneOffset)
  for field in @constructor.FIELDS when result[field]?
    result[field] = normalized[field]
 
  # remove any fields we added (go back to original precision)
  if offsetIsMorePrecise
    for f in @constructor.FIELDS
      result[f] = null if not @[f]?
 
  result
 
DateTime.prototype.getFieldFloor = Date.prototype.getFieldFloor = (field) ->
  if field == 'month'
    return 1
  if field == 'day'
    return 1
  if field == 'hour'
    return 0
  if field == 'minute'
    return 0
  if field == 'second'
    return 0
  if field == 'millisecond'
    return 0
  throw new Error('Tried to floor a field that has no floor value: ' + field)
 
 
DateTime.prototype.getFieldCieling = Date.prototype.getFieldCieling = (field) ->
  if field == 'month'
    return 12
  if field == 'day'
    return daysInMonth(@year, @month)
  if field == 'hour'
    return 23
  if field == 'minute'
    return 59
  if field == 'second'
    return 59
  if field == 'millisecond'
    return 999
  throw new Error('Tried to clieling a field that has no cieling value: ' + field)
 
daysInMonth = (year, month) ->
  if not (year? and month?)
    throw new Error('daysInMonth requires year and month as arguments')
  # Month is 1-indexed here because of the 0 day
  return new jsDate(year, month, 0).getDate();
 
normalizeMillisecondsField = (msString) ->
  # fix up milliseconds by padding zeros and/or truncating (5 --> 500, 50 --> 500, 54321 --> 543, etc.)
  msString = (msString + "00").substring(0, 3)
 
isValidDateStringFormat = (string) ->
  return false if typeof string isnt 'string'
  cqlFormats = ['YYYY',
                'YYYY-MM',
                'YYYY-MM-DD']
 
  cqlFormatStringWithLength = {}
  cqlFormatStringWithLength[format.length] = format for format in cqlFormats
 
  return false if !cqlFormatStringWithLength[string.length]?
 
  strict = true
  moment(string, cqlFormatStringWithLength[string.length], strict).isValid()
 
isValidDateTimeStringFormat = (string) ->
  return false if typeof string isnt 'string'
  cqlFormats = ['YYYY',
                'YYYY-MM',
                'YYYY-MM-DD',
                'YYYY-MM-DDTZ',
                'YYYY-MM-DDT+hh',
                'YYYY-MM-DDT+hh:mm',
                'YYYY-MM-DDT-hh',
                'YYYY-MM-DDT-hh:mm',
                'YYYY-MM-DDThh',
                'YYYY-MM-DDThhZ',
                'YYYY-MM-DDThh+hh',
                'YYYY-MM-DDThh+hh:mm',
                'YYYY-MM-DDThh-hh',
                'YYYY-MM-DDThh-hh:mm',
                'YYYY-MM-DDThh:mm',
                'YYYY-MM-DDThh:mmZ',
                'YYYY-MM-DDThh:mm+hh',
                'YYYY-MM-DDThh:mm+hh:mm',
                'YYYY-MM-DDThh:mm-hh',
                'YYYY-MM-DDThh:mm-hh:mm',
                'YYYY-MM-DDThh:mm:ss',
                'YYYY-MM-DDThh:mm:ssZ',
                'YYYY-MM-DDThh:mm:ss+hh',
                'YYYY-MM-DDThh:mm:ss+hh:mm',
                'YYYY-MM-DDThh:mm:ss-hh',
                'YYYY-MM-DDThh:mm:ss-hh:mm',
                'YYYY-MM-DDThh:mm:ss.fff',
                'YYYY-MM-DDThh:mm:ss.fffZ',
                'YYYY-MM-DDThh:mm:ss.fff+hh',
                'YYYY-MM-DDThh:mm:ss.fff+hh:mm',
                'YYYY-MM-DDThh:mm:ss.fff-hh',
                'YYYY-MM-DDThh:mm:ss.fff-hh:mm']
 
  cqlFormatStringWithLength = {}
  cqlFormatStringWithLength[format.length] = format for format in cqlFormats
 
  return false if !cqlFormatStringWithLength[string.length]?
 
  # Moment.js has 2 options for parsing, strict or forgiving.
  # Strict parsing requires that the format and input match exactly, including delimeters.
  # Due to CQL using slightly different delimiters than moment, we need to use forgiving.
  strict = false
  moment(string, cqlFormatStringToMomentFormatString(cqlFormatStringWithLength[string.length]), strict).isValid()
 
cqlFormatStringToMomentFormatString = (string) ->
  # CQL: 'YYYY-MM-DDThh:mm:ss.fff-hh:mm', Moment: 'YYYY-MM-DD[T]hh:mm:ss.SSS[Z]'
  [yearMonthDay, timeAndTimeZoneOffset] = string.split('T')
 
  if timeAndTimeZoneOffset?
    timezoneSeparator = getTimezoneSeparatorFromString(timeAndTimeZoneOffset)
 
  momentString = yearMonthDay
  momentString += '[T]' if string.match(/T/)?
  if !!timezoneSeparator
    momentString += timeAndTimeZoneOffset.substring(0, timeAndTimeZoneOffset.search(timezoneSeparator)) + '[Z]'
  else
    momentString += timeAndTimeZoneOffset
 
  momentString = momentString.replace /f/g, 'S'
 
 
module.exports.DateTime = DateTime
module.exports.Date = Date