Skip to content

Commit

Permalink
Update for #208: use improved ValidTimezoneOffsets. Use new leastBy
Browse files Browse the repository at this point in the history
  • Loading branch information
mceachen committed Sep 22, 2024
1 parent b2b75d5 commit 6c9c4a8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
15 changes: 13 additions & 2 deletions src/Timezones.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ describe("Timezones", () => {
})

describe("extractTzOffsetFromUTCOffset", () => {
it("with DateTimeUTC offset by 4 hours and preceding by one second from DateTimeOriginal", () => {
expect(
extractTzOffsetFromUTCOffset({
DateTimeOriginal: "2024:09:14 12:00:00",
DateTimeUTC: "2024:09:14 15:59:00", // from a prior GPS fix?
})
).to.eql({
tz: "UTC-4",
src: "offset between DateTimeOriginal and DateTimeUTC",
})
})
it("with DateTimeUTC offset by 4 hours and lagging by one second from DateTimeOriginal", () => {
expect(
extractTzOffsetFromUTCOffset({
Expand Down Expand Up @@ -211,14 +222,14 @@ describe("Timezones", () => {
src: "offset between CreateDate and " + tagname,
})
})
it(`with lagging ${tagname} and SubSecCreateDate in positive half-hour offset`, () => {
it(`with lagging ${tagname} and SubSecCreateDate in positive :20 offset`, () => {
const obj: Tags = {
SubSecCreateDate: "2016:07:18 09:54:03",
}
obj[tagname] = "2016:07:18 04:16:01"

expect(extractTzOffsetFromUTCOffset(obj)).to.eql({
tz: "UTC+5:30",
tz: "UTC+5:45",
src: "offset between SubSecCreateDate and " + tagname,
})
})
Expand Down
26 changes: 10 additions & 16 deletions src/Timezones.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FixedOffsetZone, Info, Zone } from "luxon"
import { leastBy } from "./Array"
import { BinaryField } from "./BinaryField"
import { CapturedAtTagNames } from "./CapturedAtTagNames"
import { ExifDate } from "./ExifDate"
Expand Down Expand Up @@ -38,7 +39,6 @@ const ValidTimezoneOffsets = [
// "-00:25:21", // Ireland 1880-1916 https://en.wikipedia.org/wiki/UTC%E2%88%9200:25:21
"+00:00",
// "+00:20", // used by Netherlands until 1940

// "+00:30", // used by Switzerland until 1936
"+01:00",
// "+01:24", // used by Warsaw until 1915
Expand All @@ -53,7 +53,7 @@ const ValidTimezoneOffsets = [
"+05:00",
"+05:30",
// "+05:40", // used by Nepal until 1920
"+05:45",
"+05:45", // Nepal
"+06:00",
"+06:30",
"+07:00",
Expand Down Expand Up @@ -409,20 +409,14 @@ export function extractTzOffsetFromDatestamps(
// old, this can be spurious. We get less mistakes with a larger multiple, so
// we're using 30 minutes instead of 15. See
// https://www.timeanddate.com/time/time-zones-interesting.html
const LikelyOffsetMinutes = ValidTimezoneOffsets
.filter((offset) => offset.endsWith(":00") || offset.endsWith(":30"))
.map(offsetToMinutes)

export function inferLikelyOffsetMinutes(deltaMinutes: number): number {
// More then a day away? nothing is likely
if (Math.abs(deltaMinutes) > (24 * 60)) return deltaMinutes

return LikelyOffsetMinutes
.reduce((prev, curr) =>
Math.abs(curr - deltaMinutes) < Math.abs(prev - deltaMinutes)
? curr
: prev
)
const LikelyOffsetMinutes = ValidTimezoneOffsets.map(offsetToMinutes)

export function inferLikelyOffsetMinutes(deltaMinutes: number): Maybe<number> {
const nearest = leastBy(LikelyOffsetMinutes, (ea) =>
Math.abs(ea - deltaMinutes)
)!
// Reject timezone offsets more than 30 minutes away from the nearest:
return Math.abs(nearest - deltaMinutes) < 30 ? nearest : undefined
}

/**
Expand Down

0 comments on commit 6c9c4a8

Please sign in to comment.