Convert time to epoch in PHP (#56)

pull/62/head
Varun Patil 2022-09-27 14:36:23 -07:00
parent 664457b17e
commit 373a02fd2c
4 changed files with 71 additions and 24 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -17,7 +17,7 @@ class TimelineQuery {
public function getInfoById(int $id): array { public function getInfoById(int $id): array {
$qb = $this->connection->getQueryBuilder(); $qb = $this->connection->getQueryBuilder();
$qb->select('*') $qb->select('fileid', 'dayid', 'datetaken')
->from('memories') ->from('memories')
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($id, \PDO::PARAM_INT))); ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($id, \PDO::PARAM_INT)));
@ -25,10 +25,16 @@ class TimelineQuery {
$row = $result->fetch(); $row = $result->fetch();
$result->closeCursor(); $result->closeCursor();
$utcTs = 0;
try {
$utcDate = new \DateTime($row['datetaken'], new \DateTimeZone('UTC'));
$utcTs = $utcDate->getTimestamp();
} catch (\Throwable $e) {}
return [ return [
'fileid' => intval($row['fileid']), 'fileid' => intval($row['fileid']),
'dayid' => intval($row['dayid']), 'dayid' => intval($row['dayid']),
'datetaken' => $row['datetaken'], 'datetaken' => $utcTs,
]; ];
} }
} }

View File

@ -160,11 +160,11 @@ export default class EditDate extends Mixins(GlobalMixin) {
const calls = photos.map((p) => async () => { const calls = photos.map((p) => async () => {
try { try {
const res = await axios.get<any>(generateUrl(INFO_API_URL, { id: p.fileid })); const res = await axios.get<any>(generateUrl(INFO_API_URL, { id: p.fileid }));
if (typeof res.data.datetaken !== "string") { if (typeof res.data.datetaken !== "number") {
console.error("Invalid date for", p.fileid); console.error("Invalid date for", p.fileid);
return; return;
} }
p.datetaken = Date.parse(res.data.datetaken + " UTC"); p.datetaken = res.data.datetaken * 1000;
} catch (error) { } catch (error) {
console.error('Failed to get date info for', p.fileid, error); console.error('Failed to get date info for', p.fileid, error);
} finally { } finally {
@ -247,6 +247,8 @@ export default class EditDate extends Mixins(GlobalMixin) {
} catch (e) { } catch (e) {
if (e.response?.data?.message) { if (e.response?.data?.message) {
showError(e.response.data.message); showError(e.response.data.message);
} else {
showError(e);
} }
} finally { } finally {
this.processing = false; this.processing = false;
@ -264,9 +266,18 @@ export default class EditDate extends Mixins(GlobalMixin) {
const diff = date.getTime() - dateLast.getTime(); const diff = date.getTime() - dateLast.getTime();
// Get new difference between newest and oldest date // Get new difference between newest and oldest date
const dateNew = this.getDate(); let dateNew: Date;
const dateLastNew = this.getDateLast(); let dateLastNew: Date;
const diffNew = dateNew.getTime() - dateLastNew.getTime(); let diffNew: number;
try {
dateNew = this.getDate();
dateLastNew = this.getDateLast();
diffNew = dateNew.getTime() - dateLastNew.getTime();
} catch (e) {
showError(e);
return;
}
// Validate if the old is still old // Validate if the old is still old
if (diffNew < 0) { if (diffNew < 0) {
@ -290,6 +301,8 @@ export default class EditDate extends Mixins(GlobalMixin) {
} catch (e) { } catch (e) {
if (e.response?.data?.message) { if (e.response?.data?.message) {
showError(e.response.data.message); showError(e.response.data.message);
} else {
showError(e);
} }
} finally { } finally {
this.photosDone++; this.photosDone++;
@ -328,24 +341,52 @@ export default class EditDate extends Mixins(GlobalMixin) {
public getDate() { public getDate() {
const dateNew = new Date(); const dateNew = new Date();
dateNew.setUTCFullYear(parseInt(this.year)); const year = parseInt(this.year, 10);
dateNew.setUTCMonth(parseInt(this.month) - 1); const month = parseInt(this.month, 10) - 1;
dateNew.setUTCDate(parseInt(this.day)); const day = parseInt(this.day, 10);
dateNew.setUTCHours(parseInt(this.hour)); const hour = parseInt(this.hour, 10);
dateNew.setUTCMinutes(parseInt(this.minute)); const minute = parseInt(this.minute, 10);
dateNew.setUTCSeconds(parseInt(this.second)); const second = parseInt(this.second, 10) || 0;
if (isNaN(year)) throw new Error("Invalid year");
if (isNaN(month)) throw new Error("Invalid month");
if (isNaN(day)) throw new Error("Invalid day");
if (isNaN(hour)) throw new Error("Invalid hour");
if (isNaN(minute)) throw new Error("Invalid minute");
if (isNaN(second)) throw new Error("Invalid second");
dateNew.setUTCFullYear(year);
dateNew.setUTCMonth(month);
dateNew.setUTCDate(day);
dateNew.setUTCHours(hour);
dateNew.setUTCMinutes(minute);
dateNew.setUTCSeconds(second);
return dateNew; return dateNew;
} }
public getDateLast() { public getDateLast() {
const dateLast = new Date(); const dateNew = new Date();
dateLast.setUTCFullYear(parseInt(this.yearLast)); const year = parseInt(this.yearLast, 10);
dateLast.setUTCMonth(parseInt(this.monthLast) - 1); const month = parseInt(this.monthLast, 10) - 1;
dateLast.setUTCDate(parseInt(this.dayLast)); const day = parseInt(this.dayLast, 10);
dateLast.setUTCHours(parseInt(this.hourLast)); const hour = parseInt(this.hourLast, 10);
dateLast.setUTCMinutes(parseInt(this.minuteLast)); const minute = parseInt(this.minuteLast, 10);
dateLast.setUTCSeconds(parseInt(this.secondLast)); const second = parseInt(this.secondLast, 10) || 0;
return dateLast;
if (isNaN(year)) throw new Error("Invalid last year");
if (isNaN(month)) throw new Error("Invalid last month");
if (isNaN(day)) throw new Error("Invalid last day");
if (isNaN(hour)) throw new Error("Invalid last hour");
if (isNaN(minute)) throw new Error("Invalid last minute");
if (isNaN(second)) throw new Error("Invalid last second");
dateNew.setUTCFullYear(year);
dateNew.setUTCMonth(month);
dateNew.setUTCDate(day);
dateNew.setUTCHours(hour);
dateNew.setUTCMinutes(minute);
dateNew.setUTCSeconds(second);
return dateNew;
} }
} }
</script> </script>