mirror of
https://github.com/samsonjs/immich.git
synced 2026-04-08 11:35:54 +00:00
fix: timezone in timeline bucketing (#25894)
Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
This commit is contained in:
parent
a42c08ed84
commit
2dd3a764ae
3 changed files with 21 additions and 16 deletions
|
|
@ -85,14 +85,14 @@ LIMIT $limit;
|
|||
mergedBucket(:group_by AS INTEGER):
|
||||
SELECT
|
||||
COUNT(*) as asset_count,
|
||||
CASE
|
||||
WHEN :group_by = 0 THEN STRFTIME('%Y-%m-%d', created_at, 'localtime') -- day
|
||||
WHEN :group_by = 1 THEN STRFTIME('%Y-%m', created_at, 'localtime') -- month
|
||||
END AS bucket_date
|
||||
bucket_date
|
||||
FROM
|
||||
(
|
||||
SELECT
|
||||
rae.created_at
|
||||
CASE
|
||||
WHEN :group_by = 0 THEN STRFTIME('%Y-%m-%d', rae.local_date_time)
|
||||
WHEN :group_by = 1 THEN STRFTIME('%Y-%m', rae.local_date_time)
|
||||
END as bucket_date
|
||||
FROM
|
||||
remote_asset_entity rae
|
||||
LEFT JOIN
|
||||
|
|
@ -107,7 +107,10 @@ FROM
|
|||
)
|
||||
UNION ALL
|
||||
SELECT
|
||||
lae.created_at
|
||||
CASE
|
||||
WHEN :group_by = 0 THEN STRFTIME('%Y-%m-%d', lae.created_at, 'localtime')
|
||||
WHEN :group_by = 1 THEN STRFTIME('%Y-%m', lae.created_at, 'localtime')
|
||||
END as bucket_date
|
||||
FROM
|
||||
local_asset_entity lae
|
||||
WHERE NOT EXISTS (
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -126,7 +126,7 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
|||
}
|
||||
|
||||
final assetCountExp = _db.localAssetEntity.id.count();
|
||||
final dateExp = _db.localAssetEntity.createdAt.dateFmt(groupBy);
|
||||
final dateExp = _db.localAssetEntity.createdAt.dateFmt(groupBy, toLocal: true);
|
||||
|
||||
final query =
|
||||
_db.localAssetEntity.selectOnly().join([
|
||||
|
|
@ -203,7 +203,7 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
|||
final album = albums.first;
|
||||
final isAscending = album.order == AlbumAssetOrder.asc;
|
||||
final assetCountExp = _db.remoteAssetEntity.id.count();
|
||||
final dateExp = _db.remoteAssetEntity.createdAt.dateFmt(groupBy);
|
||||
final dateExp = _db.remoteAssetEntity.localDateTime.dateFmt(groupBy);
|
||||
|
||||
final query = _db.remoteAssetEntity.selectOnly()
|
||||
..addColumns([assetCountExp, dateExp])
|
||||
|
|
@ -280,7 +280,8 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
|||
final sorted = List<BaseAsset>.from(assets)..sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
||||
final Map<DateTime, int> bucketCounts = {};
|
||||
for (final asset in sorted) {
|
||||
final date = DateTime(asset.createdAt.year, asset.createdAt.month, asset.createdAt.day);
|
||||
final localTime = asset.createdAt.toLocal();
|
||||
final date = DateTime(localTime.year, localTime.month, localTime.day);
|
||||
bucketCounts[date] = (bucketCounts[date] ?? 0) + 1;
|
||||
}
|
||||
|
||||
|
|
@ -360,7 +361,7 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
|||
}
|
||||
|
||||
final assetCountExp = _db.remoteAssetEntity.id.count();
|
||||
final dateExp = _db.remoteAssetEntity.createdAt.dateFmt(groupBy);
|
||||
final dateExp = _db.remoteAssetEntity.localDateTime.dateFmt(groupBy);
|
||||
|
||||
final query = _db.remoteAssetEntity.selectOnly()
|
||||
..addColumns([assetCountExp, dateExp])
|
||||
|
|
@ -430,7 +431,7 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
|||
}
|
||||
|
||||
final assetCountExp = _db.remoteAssetEntity.id.count();
|
||||
final dateExp = _db.remoteAssetEntity.createdAt.dateFmt(groupBy);
|
||||
final dateExp = _db.remoteAssetEntity.localDateTime.dateFmt(groupBy);
|
||||
|
||||
final query = _db.remoteAssetEntity.selectOnly()
|
||||
..addColumns([assetCountExp, dateExp])
|
||||
|
|
@ -500,7 +501,7 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
|||
}
|
||||
|
||||
final assetCountExp = _db.remoteAssetEntity.id.count();
|
||||
final dateExp = _db.remoteAssetEntity.createdAt.dateFmt(groupBy);
|
||||
final dateExp = _db.remoteAssetEntity.localDateTime.dateFmt(groupBy);
|
||||
|
||||
final query = _db.remoteAssetEntity.selectOnly()
|
||||
..addColumns([assetCountExp, dateExp])
|
||||
|
|
@ -602,7 +603,7 @@ class DriftTimelineRepository extends DriftDatabaseRepository {
|
|||
}
|
||||
|
||||
final assetCountExp = _db.remoteAssetEntity.id.count();
|
||||
final dateExp = _db.remoteAssetEntity.createdAt.dateFmt(groupBy);
|
||||
final dateExp = _db.remoteAssetEntity.localDateTime.dateFmt(groupBy);
|
||||
|
||||
final query = _db.remoteAssetEntity.selectOnly()
|
||||
..addColumns([assetCountExp, dateExp])
|
||||
|
|
@ -664,10 +665,11 @@ List<Bucket> _generateBuckets(int count) {
|
|||
}
|
||||
|
||||
extension on Expression<DateTime> {
|
||||
Expression<String> dateFmt(GroupAssetsBy groupBy) {
|
||||
Expression<String> dateFmt(GroupAssetsBy groupBy, {bool toLocal = false}) {
|
||||
// DateTimes are stored in UTC, so we need to convert them to local time inside the query before formatting
|
||||
// to create the correct time bucket
|
||||
final localTimeExp = modify(const DateTimeModifier.localTime());
|
||||
// to create the correct time bucket when toLocal is true
|
||||
// toLocal is false for remote assets where localDateTime is already in the correct timezone
|
||||
final localTimeExp = toLocal ? modify(const DateTimeModifier.localTime()) : this;
|
||||
return switch (groupBy) {
|
||||
GroupAssetsBy.day || GroupAssetsBy.auto => localTimeExp.date,
|
||||
GroupAssetsBy.month => localTimeExp.strftime("%Y-%m"),
|
||||
|
|
|
|||
Loading…
Reference in a new issue