index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict'
  2. var DATE_TIME = /(\d{1,})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})(\.\d{1,})?.*?( BC)?$/
  3. var DATE = /^(\d{1,})-(\d{2})-(\d{2})( BC)?$/
  4. var TIME_ZONE = /([Z+-])(\d{2})?:?(\d{2})?:?(\d{2})?/
  5. var INFINITY = /^-?infinity$/
  6. module.exports = function parseDate (isoDate) {
  7. if (INFINITY.test(isoDate)) {
  8. // Capitalize to Infinity before passing to Number
  9. return Number(isoDate.replace('i', 'I'))
  10. }
  11. var matches = DATE_TIME.exec(isoDate)
  12. if (!matches) {
  13. // Force YYYY-MM-DD dates to be parsed as local time
  14. return getDate(isoDate) || null
  15. }
  16. var isBC = !!matches[8]
  17. var year = parseInt(matches[1], 10)
  18. if (isBC) {
  19. year = bcYearToNegativeYear(year)
  20. }
  21. var month = parseInt(matches[2], 10) - 1
  22. var day = matches[3]
  23. var hour = parseInt(matches[4], 10)
  24. var minute = parseInt(matches[5], 10)
  25. var second = parseInt(matches[6], 10)
  26. var ms = matches[7]
  27. ms = ms ? 1000 * parseFloat(ms) : 0
  28. var date
  29. var offset = timeZoneOffset(isoDate)
  30. if (offset != null) {
  31. date = new Date(Date.UTC(year, month, day, hour, minute, second, ms))
  32. // Account for years from 0 to 99 being interpreted as 1900-1999
  33. // by Date.UTC / the multi-argument form of the Date constructor
  34. if (is0To99(year)) {
  35. date.setUTCFullYear(year)
  36. }
  37. if (offset !== 0) {
  38. date.setTime(date.getTime() - offset)
  39. }
  40. } else {
  41. date = new Date(year, month, day, hour, minute, second, ms)
  42. if (is0To99(year)) {
  43. date.setFullYear(year)
  44. }
  45. }
  46. return date
  47. }
  48. function getDate (isoDate) {
  49. var matches = DATE.exec(isoDate)
  50. if (!matches) {
  51. return
  52. }
  53. var year = parseInt(matches[1], 10)
  54. var isBC = !!matches[4]
  55. if (isBC) {
  56. year = bcYearToNegativeYear(year)
  57. }
  58. var month = parseInt(matches[2], 10) - 1
  59. var day = matches[3]
  60. // YYYY-MM-DD will be parsed as local time
  61. var date = new Date(year, month, day)
  62. if (is0To99(year)) {
  63. date.setFullYear(year)
  64. }
  65. return date
  66. }
  67. // match timezones:
  68. // Z (UTC)
  69. // -05
  70. // +06:30
  71. function timeZoneOffset (isoDate) {
  72. if (isoDate.endsWith('+00')) {
  73. return 0
  74. }
  75. var zone = TIME_ZONE.exec(isoDate.split(' ')[1])
  76. if (!zone) return
  77. var type = zone[1]
  78. if (type === 'Z') {
  79. return 0
  80. }
  81. var sign = type === '-' ? -1 : 1
  82. var offset = parseInt(zone[2], 10) * 3600 +
  83. parseInt(zone[3] || 0, 10) * 60 +
  84. parseInt(zone[4] || 0, 10)
  85. return offset * sign * 1000
  86. }
  87. function bcYearToNegativeYear (year) {
  88. // Account for numerical difference between representations of BC years
  89. // See: https://github.com/bendrucker/postgres-date/issues/5
  90. return -(year - 1)
  91. }
  92. function is0To99 (num) {
  93. return num >= 0 && num < 100
  94. }