Created
May 12, 2023 16:08
-
-
Save coderbyheart/33ffc41a2023a643da75fdb543a0e6f2 to your computer and use it in GitHub Desktop.
Date Kata SoCraCan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { describe, test } from "node:test"; | |
import assert from "node:assert/strict"; | |
describe("this is a simple test", () => { | |
test("that this asserts", () => assert.equal(1, 1)); | |
test("return range", () => { | |
const twoAmInAustralia = new Date("2023-05-13T02:00:00+10:00"); | |
const { from, until } = createRange(twoAmInAustralia); | |
assert.equal(until.toISOString(), "2023-05-12T23:59:59.999Z"); | |
assert.equal(from.toISOString(), "2023-05-11T23:59:59.999Z"); // "at least 24 hours before until" | |
}); | |
test("another date", () => { | |
const noonInJapan = new Date("2023-05-13T01:00:00+09:00"); | |
const { from, until } = createRange(noonInJapan); | |
assert.equal(until.toISOString(), "2023-05-12T23:59:59.999Z"); | |
assert.equal(from.toISOString(), "2023-05-11T23:59:59.999Z"); | |
}); | |
}); | |
/** | |
* Give a local user date, returns a date range in UTC, where | |
* - until is end of the UTC dat | |
* - from is at least 24 hours before until | |
*/ | |
const createRange = (userDate) => { | |
const until = new Date(userDate); | |
until.setUTCHours(23); | |
until.setUTCMinutes(59); | |
until.setUTCSeconds(59); | |
until.setUTCMilliseconds(999); | |
return { from: new Date(until.getTime() - 24 * 60 * 60 * 1000), until }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment