Skip to content

Commit

Permalink
Guess timezone from CreateDate if OffsetTimeDigitized is not present
Browse files Browse the repository at this point in the history
This should help alleviate the problem that
#319 is trying to
solve. Note that it's not foolproof, due to the inherent ambiguity of
expressing times without timezones. Specifically, photos taken during
the hour that occurs twice when we switch from EDT to EST, may not have
the correct timezone applied.

Most of the code in timezone.js came from https://www.codeproject.com/articles/58728/javascript-code-to-determine-when-daylight-savings
  • Loading branch information
josephfrazier committed Dec 8, 2021
1 parent 95b9944 commit e7c0415
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/routes/home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import s from './Home.css';

import SubmissionDetails from '../../components/SubmissionDetails.js';
import { isImage, isVideo } from '../../isImage.js';
import { getNycTimezoneOffset } from '../../timezone.js';

const GOOGLE_MAPS_API_KEY = 'AIzaSyDlwm2ykA0ohTXeVepQYvkcmdjz2M2CKEI';

Expand Down Expand Up @@ -141,6 +142,7 @@ async function extractLocation({ attachmentFile, attachmentArrayBuffer, ext }) {
}

async function extractDate({ attachmentFile, attachmentArrayBuffer, ext }) {
debugger;
try {
if (isVideo({ ext })) {
return extractLocationDateFromVideo({ attachmentArrayBuffer })[1];
Expand All @@ -163,7 +165,8 @@ async function extractDate({ attachmentFile, attachmentArrayBuffer, ext }) {
millisecondsSinceEpoch: CreateDate.getTime(),
offset: OffsetTimeDigitized
? parseInt(OffsetTimeDigitized, 10) * -60
: new Date().getTimezoneOffset(),
: getNycTimezoneOffset(CreateDate)
// : new Date().getTimezoneOffset(),
};
} catch (err) {
console.error(err.stack);
Expand Down
116 changes: 116 additions & 0 deletions src/timezone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
function DisplayDstSwitchDates()
{
var year = new Date().getYear();
if (year < 1000)
year += 1900;

var firstSwitch = 0;
var secondSwitch = 0;
var lastOffset = 99;

// Loop through every month of the current year
for (let i = 0; i < 12; i++)
{
// Fetch the timezone value for the month
var newDate = new Date(Date.UTC(year, i, 0, 0, 0, 0, 0));
var tz = -1 * newDate.getTimezoneOffset() / 60;

// Capture when a timzezone change occurs
if (tz > lastOffset)
firstSwitch = i-1;
else if (tz < lastOffset)
secondSwitch = i-1;

lastOffset = tz;
}

// Go figure out date/time occurrences a minute before
// a DST adjustment occurs
var secondDstDate = FindDstSwitchDate(year, secondSwitch);
var firstDstDate = FindDstSwitchDate(year, firstSwitch);

return {firstDstDate, secondDstDate};
}

function FindDstSwitchDate(year, month)
{
// Set the starting date
var baseDate = new Date(Date.UTC(year, month, 0, 0, 0, 0, 0));
var changeDay = 0;
var changeMinute = -1;
var baseOffset = -1 * baseDate.getTimezoneOffset() / 60;
var dstDate;

// Loop to find the exact day a timezone adjust occurs
for (let day = 0; day < 50; day++)
{
var tmpDate = new Date(Date.UTC(year, month, day, 0, 0, 0, 0));
var tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;

// Check if the timezone changed from one day to the next
if (tmpOffset != baseOffset)
{
var minutes = 0;
changeDay = day;

// Back-up one day and grap the offset
tmpDate = new Date(Date.UTC(year, month, day-1, 0, 0, 0, 0));
tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;

// Count the minutes until a timezone change occurs
while (changeMinute == -1)
{
tmpDate = new Date(Date.UTC(year, month, day-1, 0, minutes, 0, 0));
tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;

// Determine the exact minute a timezone change
// occurs
if (tmpOffset != baseOffset)
{
// Back-up a minute to get the date/time just
// before a timezone change occurs
tmpOffset = new Date(Date.UTC(year, month,
day-1, 0, minutes-1, 0, 0));
changeMinute = minutes;
break;
}
else
minutes++;
}

// Add a month (for display) since JavaScript counts
// months from 0 to 11
dstDate = tmpOffset.getMonth() + 1;

// Pad the month as needed
if (dstDate < 10) dstDate = "0" + dstDate;

// Add the day and year
dstDate += '/' + tmpOffset.getDate() + '/' + year + ' ';

// Capture the time stamp
tmpDate = new Date(Date.UTC(year, month,
day-1, 0, minutes-1, 0, 0));
dstDate += tmpDate.toTimeString().split(' ')[0];
return dstDate;
}
}
}

function isDst(date) {
const result = DisplayDstSwitchDates()
// console.log(result)

const { firstDstDate, secondDstDate } = result
const from = new Date(firstDstDate)
const to = new Date(secondDstDate)
const now = new Date();
const isDst = from.getTime() < now.getTime() && now.getTime() < to.getTime()
// console.log({isDst})

return isDst;
}

export function getNycTimezoneOffset(date) {
return isDst(date) ? -240 : -300
}

0 comments on commit e7c0415

Please sign in to comment.