Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added timezone support to events #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/src/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import 'utils.dart' as utils;
class IEvent extends ICalendarElement with EventToDo {
IEventStatus status;
DateTime start;
String? startTimeZone;
DateTime? end;
String? endTimeZone;
Duration? duration;
ITimeTransparency? transparency = ITimeTransparency.OPAQUE;

Expand All @@ -23,7 +25,9 @@ class IEvent extends ICalendarElement with EventToDo {
String? uid,
this.status = IEventStatus.CONFIRMED,
required this.start,
this.startTimeZone,
this.end,
this.endTimeZone,
this.duration,
String? summary,
String? description,
Expand Down Expand Up @@ -61,11 +65,13 @@ class IEvent extends ICalendarElement with EventToDo {
if ((end == null && duration == null)) {
out.writecrlf('DTSTART;VALUE=DATE:${utils.formatDate(start)}');
} else {
out.writecrlf('DTSTART:${utils.formatDateTime(start)}');
out.writecrlf(
'DTSTART${utils.injectTimeZone(start, startTimeZone)}:${utils.formatDateTime(start)}');
}

if (end != null) {
out.writecrlf('DTEND:${utils.formatDateTime(end!)}');
out.writecrlf(
'DTEND${utils.injectTimeZone(end!, endTimeZone)}:${utils.formatDateTime(end!)}');
}
if (duration != null) {
out.writecrlf('DURATION:${utils.formatDuration(duration!)}');
Expand Down
1 change: 0 additions & 1 deletion lib/src/timezone.dart

This file was deleted.

21 changes: 19 additions & 2 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import 'package:intl/intl.dart';

DateFormat utcDateTimeFormatter = DateFormat("yyyyMMdd'T'HHmmss'Z'");
DateFormat floatingDateTimeFormatter = DateFormat("yyyyMMdd'T'HHmmss");

formatDateTime(DateTime dt) => dt.isUtc
? DateFormat("yyyyMMdd'T'HHmmss'Z'").format(dt)
: DateFormat("yyyyMMdd'T'HHmmss").format(dt);
? utcDateTimeFormatter.format(dt)
: floatingDateTimeFormatter.format(dt);

/// If this DateTime is a UTC date time or the time zone is null this returns an empty string.
/// If a time zone string is passed, returns a TZID parameter.
/// It is assumed that the time zone string is from a unique database therefore it is prepended with a /
///
/// see https://icalendar.org/iCalendar-RFC-5545/3-2-19-time-zone-identifier.html
String injectTimeZone(DateTime dt, String? timeZone) {
if (dt.isUtc || timeZone == null) {
return "";
} else {
return ";TZID=/$timeZone";
}
}

formatDate(DateTime dt) => DateFormat("yyyyMMdd").format(dt);
formatDuration(Duration d) {
String out = (d.isNegative ? '-' : '+') + 'P';
Expand Down
11 changes: 11 additions & 0 deletions test/event_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ main() {
print(e.serialize());
print(e2.serialize());
});

test("with time zone", () {
IEvent event = IEvent(
start: DateTime(2022, 2, 2, 10),
startTimeZone: "Europe/Berlin",
end: DateTime(2022, 2, 2, 19),
endTimeZone: "Europe/Berlin",
);
String result = event.serialize();
print(result);
});
// TODO create tests
});
}
15 changes: 15 additions & 0 deletions test/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ main() {
expect(utils.formatDateTime(DateTime(1948, 12, 30, 12, 45, 33)),
'19481230T124533');
});
test('injectTimeZone', () {
expect(utils.injectTimeZone(DateTime.utc(2000, 2, 1, 23, 1), null), '');
expect(utils.injectTimeZone(DateTime(2000, 2, 1, 23, 1), null), '');
expect(
utils.injectTimeZone(
DateTime(1948, 12, 30, 12, 45, 33), "Europe/Berlin"),
';TZID=/Europe/Berlin');
});
test('formatDateTime with UTC time', () {
expect(utils.formatDateTime(DateTime.utc(2000, 2, 1, 23, 1)),
'20000201T230100Z');
expect(utils.formatDateTime(DateTime.utc(1948, 12, 30, 12, 45, 33)),
'19481230T124533Z');
});

test('formatDuration', () {
expect(
utils.formatDuration(
Expand Down