fixes timestamping across timezones
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:kryz_go_flutter/time_formatter.dart';
|
||||
import 'package:timezone/data/latest.dart' as tz;
|
||||
import 'package:timezone/timezone.dart' as tz;
|
||||
|
||||
void main() {
|
||||
setUpAll(() {
|
||||
tz.initializeTimeZones();
|
||||
});
|
||||
|
||||
group('formatStartingTime', () {
|
||||
test('returns local display from source timezone naive server time', () {
|
||||
const String sourceTime = '2026-05-14 20:00:00';
|
||||
const String sourceZone = 'America/Los_Angeles';
|
||||
|
||||
final tz.TZDateTime source = tz.TZDateTime(
|
||||
tz.getLocation(sourceZone),
|
||||
2026,
|
||||
5,
|
||||
14,
|
||||
20,
|
||||
);
|
||||
final String expected = DateFormat('jm').format(
|
||||
DateTime.fromMillisecondsSinceEpoch(
|
||||
source.millisecondsSinceEpoch,
|
||||
isUtc: true,
|
||||
).toLocal(),
|
||||
);
|
||||
|
||||
expect(
|
||||
formatStartingTime(sourceTime, sourceTimezone: sourceZone),
|
||||
expected,
|
||||
);
|
||||
});
|
||||
|
||||
test('falls back to parsed local time when timezone is invalid', () {
|
||||
final DateTime parsed = DateTime.parse('2026-05-14 20:00:00');
|
||||
final String expected = DateFormat('jm').format(parsed);
|
||||
|
||||
expect(
|
||||
formatStartingTime(
|
||||
'2026-05-14 20:00:00',
|
||||
sourceTimezone: 'Invalid/Timezone',
|
||||
),
|
||||
expected,
|
||||
);
|
||||
});
|
||||
|
||||
test('returns placeholder for invalid input', () {
|
||||
expect(formatStartingTime('not-a-time'), '--:--');
|
||||
expect(formatStartingTime(null), '--:--');
|
||||
});
|
||||
|
||||
test('does not double-convert timestamps with explicit UTC designator', () {
|
||||
const String utcStamp = '2026-05-15T03:00:00Z';
|
||||
final String expected = DateFormat('jm').format(DateTime.parse(utcStamp).toLocal());
|
||||
|
||||
expect(
|
||||
formatStartingTime(
|
||||
utcStamp,
|
||||
sourceTimezone: 'America/Los_Angeles',
|
||||
),
|
||||
expected,
|
||||
);
|
||||
});
|
||||
|
||||
test('keeps wall-clock time when local matches source zone for naive input', () {
|
||||
const String sourceTime = '2026-05-14 20:00:00';
|
||||
const String sourceZone = 'America/Los_Angeles';
|
||||
|
||||
final tz.TZDateTime source = tz.TZDateTime(
|
||||
tz.getLocation(sourceZone),
|
||||
2026,
|
||||
5,
|
||||
14,
|
||||
20,
|
||||
);
|
||||
final String expected = DateFormat('jm').format(
|
||||
DateTime.fromMillisecondsSinceEpoch(
|
||||
source.millisecondsSinceEpoch,
|
||||
isUtc: true,
|
||||
).toLocal(),
|
||||
);
|
||||
|
||||
expect(
|
||||
formatStartingTime(sourceTime, sourceTimezone: sourceZone),
|
||||
expected,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('formatTimeWindow', () {
|
||||
test('returns mm:ss style duration from server times', () {
|
||||
expect(
|
||||
formatTimeWindow('2026-05-15 05:18:46.000000', '2026-05-15 05:23:21.000000'),
|
||||
'04:35',
|
||||
);
|
||||
});
|
||||
|
||||
test('returns placeholder for invalid windows', () {
|
||||
expect(formatTimeWindow('nope', '2026-05-15 05:23:21.000000'), '--:--');
|
||||
expect(formatTimeWindow(null, '2026-05-15 05:23:21.000000'), '--:--');
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user