1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00

Feature/timestamp use2 (#7)

* effective storage of offset

* added comments to searchmanager.ts fixed linting error in utils
This commit is contained in:
grasdk 2024-02-21 23:38:07 +01:00 committed by GitHub
parent 6009ac649f
commit e0d9bdf2b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 11 deletions

View File

@ -364,7 +364,7 @@ export class SearchManager {
for (const sort of sortings) { for (const sort of sortings) {
switch (sort.method) { switch (sort.method) {
case SortByTypes.Date: case SortByTypes.Date:
query.addOrderBy('media.metadata.creationDate', sort.ascending ? 'ASC' : 'DESC'); query.addOrderBy('media.metadata.creationDate', sort.ascending ? 'ASC' : 'DESC'); //If media.metadata.creationDateOffset is defined, it is an offset of minutes (+/-). If taken into account, it will alter the sort order. Probably should not be done.
break; break;
case SortByTypes.Rating: case SortByTypes.Rating:
query.addOrderBy('media.metadata.rating', sort.ascending ? 'ASC' : 'DESC'); query.addOrderBy('media.metadata.rating', sort.ascending ? 'ASC' : 'DESC');
@ -563,7 +563,12 @@ export class SearchManager {
const textParam: { [key: string]: unknown } = {}; const textParam: { [key: string]: unknown } = {};
textParam['from' + queryId] = (query as FromDateSearch).value; textParam['from' + queryId] = (query as FromDateSearch).value;
q.where( q.where(
`media.metadata.creationDate ${relation} :from${queryId}`, `media.metadata.creationDate ${relation} :from${queryId}`, //TODO: If media.metadata.creationDateOffset is defined, it is an offset of minutes (+/-).
//Example: -600 means in the database UTC-10:00. The time 20:00 in the evening in the UTC-10 timezone, is actually 06:00 the next morning
//in UTC+00:00. To make search take that into account, one can subtract the offset from the creationDate to "pretend" the photo is taken
//in UTC time. Subtracting -600 minutes (because it's the -10:00 timezone), corresponds to adding 10 hours to the photo's timestamp, thus
//bringing it into the next day as if it was taken at UTC+00:00. Similarly subtracting a positive timezone from a timestamp will "pretend"
//the photo is taken earlier in time (e.g. subtracting 300 from the UTC+05:00 timezone).
textParam textParam
); );
@ -585,7 +590,7 @@ export class SearchManager {
const textParam: { [key: string]: unknown } = {}; const textParam: { [key: string]: unknown } = {};
textParam['to' + queryId] = (query as ToDateSearch).value; textParam['to' + queryId] = (query as ToDateSearch).value;
q.where( q.where(
`media.metadata.creationDate ${relation} :to${queryId}`, `media.metadata.creationDate ${relation} :to${queryId}`, //TODO: If media.metadata.creationDateOffset is defined, it is an offset of minutes (+/-). See explanation above.
textParam textParam
); );
@ -790,15 +795,15 @@ export class SearchManager {
if (tq.negate) { if (tq.negate) {
q.where( q.where(
`media.metadata.creationDate >= :to${queryId}`, `media.metadata.creationDate >= :to${queryId}`, //TODO: If media.metadata.creationDateOffset is defined, it is an offset of minutes (+/-). See explanation above.
textParam textParam
).orWhere(`media.metadata.creationDate < :from${queryId}`, ).orWhere(`media.metadata.creationDate < :from${queryId}`, //TODO: If media.metadata.creationDateOffset is defined, it is an offset of minutes (+/-). See explanation above.
textParam); textParam);
} else { } else {
q.where( q.where(
`media.metadata.creationDate < :to${queryId}`, `media.metadata.creationDate < :to${queryId}`, //TODO: If media.metadata.creationDateOffset is defined, it is an offset of minutes (+/-). See explanation above.
textParam textParam
).andWhere(`media.metadata.creationDate >= :from${queryId}`, ).andWhere(`media.metadata.creationDate >= :from${queryId}`, //TODO: If media.metadata.creationDateOffset is defined, it is an offset of minutes (+/-). See explanation above.
textParam); textParam);
} }
@ -821,10 +826,12 @@ export class SearchManager {
if (Config.Database.type === DatabaseType.sqlite) { if (Config.Database.type === DatabaseType.sqlite) {
if (tq.daysLength == 0) { if (tq.daysLength == 0) {
q.where( q.where(
//TODO: If media.metadata.creationDateOffset is defined, it is an offset of minutes (+/-). See explanation above.
`CAST(strftime('${duration}',media.metadataCreationDate/1000, 'unixepoch') AS INTEGER) ${relationEql} CAST(strftime('${duration}','now') AS INTEGER)` `CAST(strftime('${duration}',media.metadataCreationDate/1000, 'unixepoch') AS INTEGER) ${relationEql} CAST(strftime('${duration}','now') AS INTEGER)`
); );
} else { } else {
q.where( q.where(
//TODO: If media.metadata.creationDateOffset is defined, it is an offset of minutes (+/-). See explanation above.
`CAST(strftime('${duration}',media.metadataCreationDate/1000, 'unixepoch') AS INTEGER) ${relationTop} CAST(strftime('${duration}','now') AS INTEGER)` `CAST(strftime('${duration}',media.metadataCreationDate/1000, 'unixepoch') AS INTEGER) ${relationTop} CAST(strftime('${duration}','now') AS INTEGER)`
)[whereFN](`CAST(strftime('${duration}',media.metadataCreationDate/1000, 'unixepoch') AS INTEGER) ${relationBottom} CAST(strftime('${duration}','now','-:diff${queryId} day') AS INTEGER)`, )[whereFN](`CAST(strftime('${duration}',media.metadataCreationDate/1000, 'unixepoch') AS INTEGER) ${relationBottom} CAST(strftime('${duration}','now','-:diff${queryId} day') AS INTEGER)`,
textParam); textParam);
@ -832,10 +839,12 @@ export class SearchManager {
} else { } else {
if (tq.daysLength == 0) { if (tq.daysLength == 0) {
q.where( q.where(
//TODO: If media.metadata.creationDateOffset is defined, it is an offset of minutes (+/-). See explanation above.
`CAST(FROM_UNIXTIME(media.metadataCreationDate/1000, '${duration}') AS SIGNED) ${relationEql} CAST(DATE_FORMAT(CURDATE(),'${duration}') AS SIGNED)` `CAST(FROM_UNIXTIME(media.metadataCreationDate/1000, '${duration}') AS SIGNED) ${relationEql} CAST(DATE_FORMAT(CURDATE(),'${duration}') AS SIGNED)`
); );
} else { } else {
q.where( q.where(
//TODO: If media.metadata.creationDateOffset is defined, it is an offset of minutes (+/-). See explanation above.
`CAST(FROM_UNIXTIME(media.metadataCreationDate/1000, '${duration}') AS SIGNED) ${relationTop} CAST(DATE_FORMAT(CURDATE(),'${duration}') AS SIGNED)` `CAST(FROM_UNIXTIME(media.metadataCreationDate/1000, '${duration}') AS SIGNED) ${relationTop} CAST(DATE_FORMAT(CURDATE(),'${duration}') AS SIGNED)`
)[whereFN](`CAST(FROM_UNIXTIME(media.metadataCreationDate/1000, '${duration}') AS SIGNED) ${relationBottom} CAST(DATE_FORMAT((DATE_ADD(curdate(), INTERVAL -:diff${queryId} DAY)),'${duration}') AS SIGNED)`, )[whereFN](`CAST(FROM_UNIXTIME(media.metadataCreationDate/1000, '${duration}') AS SIGNED) ${relationBottom} CAST(DATE_FORMAT((DATE_ADD(curdate(), INTERVAL -:diff${queryId} DAY)),'${duration}') AS SIGNED)`,
textParam); textParam);

View File

@ -136,11 +136,11 @@ export class Utils {
} }
static getOffsetMinutes(offsetString: string) { //Convert offset string (+HH:MM or -HH:MM) into a minute value static getOffsetMinutes(offsetString: string) { //Convert offset string (+HH:MM or -HH:MM) into a minute value
const regex = /^([+\-](0[0-9]|1[0-4]):[0-5][0-9])$/; //checks if offset is between -14:00 and +14:00. const regex = /^([+-](0[0-9]|1[0-4]):[0-5][0-9])$/; //checks if offset is between -14:00 and +14:00.
//-12:00 is the lowest valid UTC-offset, but we allow down to -14 for efficiency //-12:00 is the lowest valid UTC-offset, but we allow down to -14 for efficiency
if (regex.test(offsetString)) { if (regex.test(offsetString)) {
let hhmm = offsetString.split(":"); const hhmm = offsetString.split(":");
let hours = parseInt(hhmm[0]); const hours = parseInt(hhmm[0]);
return hours < 0 ? ((hours*60) - parseInt(hhmm[1])) : ((hours*60) + parseInt(hhmm[1])); return hours < 0 ? ((hours*60) - parseInt(hhmm[1])) : ((hours*60) + parseInt(hhmm[1]));
} else { } else {
return undefined; return undefined;