mirror of
https://github.com/immich-app/immich.git
synced 2025-07-31 15:08:44 -04:00
add test
This commit is contained in:
parent
d0e0c24690
commit
c580c13e3e
@ -203,24 +203,7 @@ class ActionService {
|
||||
initialDateTime = asset?.localDateTime;
|
||||
initialTimeZone = exif?.timeZone;
|
||||
if (initialDateTime != null && initialTimeZone != null) {
|
||||
try {
|
||||
final location = getLocation(initialTimeZone);
|
||||
initialOffset =
|
||||
TZDateTime.from(initialDateTime, location).timeZoneOffset;
|
||||
} on LocationNotFoundException {
|
||||
RegExp re = RegExp(
|
||||
r'^utc(?:([+-]\d{1,2})(?::(\d{2}))?)?$',
|
||||
caseSensitive: false,
|
||||
);
|
||||
final m = re.firstMatch(initialTimeZone);
|
||||
if (m != null) {
|
||||
final offset = Duration(
|
||||
hours: int.parse(m.group(1) ?? '0'),
|
||||
minutes: int.parse(m.group(2) ?? '0'),
|
||||
);
|
||||
initialOffset = offset;
|
||||
}
|
||||
}
|
||||
initialOffset = getTimeZoneOffset(initialDateTime, initialTimeZone);
|
||||
}
|
||||
}
|
||||
|
||||
@ -276,4 +259,25 @@ class ActionService {
|
||||
Future<List<bool>> downloadAll(List<RemoteAsset> assets) {
|
||||
return _downloadRepository.downloadAllAssets(assets);
|
||||
}
|
||||
|
||||
static Duration? getTimeZoneOffset(DateTime dateTime, String timeZone) {
|
||||
try {
|
||||
final location = getLocation(timeZone);
|
||||
return TZDateTime.from(dateTime, location).timeZoneOffset;
|
||||
} on LocationNotFoundException {
|
||||
final re = RegExp(
|
||||
r'^utc(?:([+-]\d{1,2})(?::(\d{2}))?)?$',
|
||||
caseSensitive: false,
|
||||
);
|
||||
final m = re.firstMatch(timeZone);
|
||||
if (m != null) {
|
||||
final offset = Duration(
|
||||
hours: int.parse(m.group(1) ?? '0'),
|
||||
minutes: int.parse(m.group(2) ?? '0'),
|
||||
);
|
||||
return offset;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
34
mobile/test/modules/extensions/timezone_extensions.test.dart
Normal file
34
mobile/test/modules/extensions/timezone_extensions.test.dart
Normal file
@ -0,0 +1,34 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:timezone/data/latest.dart';
|
||||
import 'package:immich_mobile/services/action.service.dart';
|
||||
|
||||
void main() {
|
||||
setUpAll(() {
|
||||
initializeTimeZones();
|
||||
});
|
||||
|
||||
group("Returns timezone offset", () {
|
||||
final dateTime = DateTime.parse("2025-01-01T00:00:00+0800");
|
||||
|
||||
test('Returns null with invalid timezone', () {
|
||||
const timeZone = "#_#";
|
||||
final timeZoneOffset = ActionService.getTimeZoneOffset(dateTime, timeZone);
|
||||
|
||||
expect(timeZoneOffset, null);
|
||||
});
|
||||
|
||||
test('With timezone as location', () {
|
||||
const timeZone = "Asia/Hong_Kong";
|
||||
final timeZoneOffset = ActionService.getTimeZoneOffset(dateTime, timeZone);
|
||||
|
||||
expect(timeZoneOffset, const Duration(hours: 8));
|
||||
});
|
||||
|
||||
test('With timezone as offset', () {
|
||||
const timeZone = "utc+08:00";
|
||||
final timeZoneOffset = ActionService.getTimeZoneOffset(dateTime, timeZone);
|
||||
|
||||
expect(timeZoneOffset, const Duration(hours: 8));
|
||||
});
|
||||
});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user