Skip to content

Commit

Permalink
Refactor of #170 in 120df19
Browse files Browse the repository at this point in the history
  • Loading branch information
marnusw committed Mar 31, 2022
1 parent 120df19 commit 44f494e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
11 changes: 11 additions & 0 deletions src/_lib/newDateUTC/index.js
@@ -0,0 +1,11 @@
/**
* Use instead of `new Date(Date.UTC(...))` to support years below 100 which doesn't work
* otherwise due to the nature of the
* [`Date` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years.
*/
export default function newDateUTC(fullYear, month, day, hour, minute, second, millisecond) {
var utcDate = new Date(0)
utcDate.setUTCFullYear(fullYear, month, day)
utcDate.setUTCHours(hour, minute, second, millisecond)
return utcDate
}
25 changes: 12 additions & 13 deletions src/_lib/tzParseTimezone/index.js
@@ -1,4 +1,5 @@
import tzTokenizeDate from '../tzTokenizeDate/index.js'
import newDateUTC from '../newDateUTC/index.js'

var MILLISECONDS_IN_HOUR = 3600000
var MILLISECONDS_IN_MINUTE = 60000
Expand Down Expand Up @@ -70,7 +71,7 @@ export default function tzParseTimezone(timezoneString, date, isUtcDate) {
}

function toUtcDate(date) {
return newDateDateUTC(
return newDateUTC(
date.getFullYear(),
date.getMonth(),
date.getDate(),
Expand All @@ -81,21 +82,19 @@ function toUtcDate(date) {
)
}

function newDateDateUTC(fullYear, month, day, hour, minute, second, millisecond) {
var utcDate = new Date(0)
utcDate.setUTCFullYear(fullYear, month, day)
utcDate.setUTCHours(hour, minute, second, millisecond)
return utcDate
}

function DateUTC(fullYear, month, day, hour, minute, second, millisecond) {
return newDateDateUTC(fullYear, month, day, hour, minute, second, millisecond).getTime()
}

function calcOffset(date, timezoneString) {
var tokens = tzTokenizeDate(date, timezoneString)

var asUTC = DateUTC(tokens[0], tokens[1] - 1, tokens[2], tokens[3] % 24, tokens[4], tokens[5], 0)
// ms dropped because it's not provided by tzTokenizeDate
var asUTC = newDateUTC(
tokens[0],
tokens[1] - 1,
tokens[2],
tokens[3] % 24,
tokens[4],
tokens[5],
0
).getTime()

var asTS = date.getTime()
var over = asTS % 1000
Expand Down
15 changes: 10 additions & 5 deletions src/zonedTimeToUtc/index.js
Expand Up @@ -2,6 +2,7 @@ import cloneObject from 'date-fns/_lib/cloneObject/index.js'
import toDate from '../toDate/index.js'
import tzPattern from '../_lib/tzPattern/index.js'
import tzParseTimezone from '../_lib/tzParseTimezone/index.js'
import newDateUTC from '../_lib/newDateUTC'

/**
* @name zonedTimeToUtc
Expand Down Expand Up @@ -36,11 +37,15 @@ export default function zonedTimeToUtc(date, timeZone, options) {

var d = toDate(date, options)

var tmp = new Date(0)
tmp.setUTCFullYear(d.getFullYear(), d.getMonth(), d.getDate())
tmp.setUTCHours(d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds())

var utc = tmp.getTime()
var utc = newDateUTC(
d.getFullYear(),
d.getMonth(),
d.getDate(),
d.getHours(),
d.getMinutes(),
d.getSeconds(),
d.getMilliseconds()
).getTime()

var offsetMilliseconds = tzParseTimezone(timeZone, new Date(utc))

Expand Down

0 comments on commit 44f494e

Please sign in to comment.