asset_extensions.dart 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import 'package:immich_mobile/shared/models/asset.dart';
  2. import 'package:timezone/timezone.dart';
  3. extension TZExtension on Asset {
  4. /// Returns the created time of the asset from the exif info (if available) or from
  5. /// the fileCreatedAt field, adjusted to the timezone value from the exif info along with
  6. /// the timezone offset in [Duration]
  7. (DateTime, Duration) getTZAdjustedTimeAndOffset() {
  8. DateTime dt = fileCreatedAt.toLocal();
  9. if (exifInfo?.dateTimeOriginal != null) {
  10. dt = exifInfo!.dateTimeOriginal!;
  11. if (exifInfo?.timeZone != null) {
  12. dt = dt.toUtc();
  13. try {
  14. final location = getLocation(exifInfo!.timeZone!);
  15. dt = TZDateTime.from(dt, location);
  16. } on LocationNotFoundException {
  17. RegExp re = RegExp(
  18. r'^utc(?:([+-]\d{1,2})(?::(\d{2}))?)?$',
  19. caseSensitive: false,
  20. );
  21. final m = re.firstMatch(exifInfo!.timeZone!);
  22. if (m != null) {
  23. final duration = Duration(
  24. hours: int.parse(m.group(1) ?? '0'),
  25. minutes: int.parse(m.group(2) ?? '0'),
  26. );
  27. dt = dt.add(duration);
  28. return (dt, duration);
  29. }
  30. }
  31. }
  32. }
  33. return (dt, dt.timeZoneOffset);
  34. }
  35. }