Skip to content

Added support for UTC and DateTimeOffsets in VEVENT generator #499

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

Merged
merged 2 commits into from
Apr 24, 2024
Merged
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
28 changes: 25 additions & 3 deletions QRCoder/PayloadGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,20 @@ public class CalendarEvent : Payload
private readonly string subject, description, location, start, end;
private readonly EventEncoding encoding;

/// <summary>
/// Generates a calender entry/event payload.
/// </summary>
/// <param name="subject">Subject/title of the calender event</param>
/// <param name="description">Description of the event</param>
/// <param name="location">Location (lat:long or address) of the event</param>
/// <param name="start">Start time (incl. UTC offset) of the event</param>
/// <param name="end">End time (incl. UTC offset) of the event</param>
/// <param name="allDayEvent">Is it a full day event?</param>
/// <param name="encoding">Type of encoding (universal or iCal)</param>
public CalendarEvent(string subject, string description, string location, DateTimeOffset start, DateTimeOffset end, bool allDayEvent, EventEncoding encoding = EventEncoding.Universal) : this(subject, description, location, start.UtcDateTime, end.UtcDateTime, allDayEvent, encoding)
{
}

/// <summary>
/// Generates a calender entry/event payload.
/// </summary>
Expand All @@ -1893,9 +1907,17 @@ public CalendarEvent(string subject, string description, string location, DateTi
this.description = description;
this.location = location;
this.encoding = encoding;
string dtFormat = allDayEvent ? "yyyyMMdd" : "yyyyMMddTHHmmss";
this.start = start.ToString(dtFormat);
this.end = end.ToString(dtFormat);
string dtFormatStart = "yyyyMMdd", dtFormatEnd = "yyyyMMdd";
if (!allDayEvent)
{
dtFormatStart = dtFormatEnd = "yyyyMMddTHHmmss";
if (start.Kind == DateTimeKind.Utc)
dtFormatStart = "yyyyMMddTHHmmssZ";
if (end.Kind == DateTimeKind.Utc)
dtFormatEnd = "yyyyMMddTHHmmssZ";
}
this.start = start.ToString(dtFormatStart);
this.end = end.ToString(dtFormatEnd);
}

public override string ToString()
Expand Down
38 changes: 37 additions & 1 deletion QRCoderTests/PayloadGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ public void calendarevent_should_build_universal()
var location = "Programmer's paradise, Beachtown, Paradise";
var alldayEvent = false;
var begin = new DateTime(2016, 01, 03, 12, 00, 00);
var end = new DateTime(2016, 01, 03, 14, 30, 0);
var end = new DateTime(2016, 01, 03, 14, 30, 00);
var encoding = PayloadGenerator.CalendarEvent.EventEncoding.Universal;

var generator = new PayloadGenerator.CalendarEvent(subject, description, location, begin, end, alldayEvent, encoding);
Expand All @@ -843,6 +843,42 @@ public void calendarevent_should_build_ical()
}


[Fact]
[Category("PayloadGenerator/CalendarEvent")]
public void calendarevent_should_build_with_utc_datetime()
{
var subject = "Release party";
var description = "A small party for the new QRCoder. Bring some beer!";
var location = "Programmer's paradise, Beachtown, Paradise";
var alldayEvent = false;
var begin = new DateTime(2016, 01, 03, 12, 00, 00, DateTimeKind.Utc);
var end = new DateTime(2016, 01, 03, 14, 30, 00, DateTimeKind.Utc);
var encoding = PayloadGenerator.CalendarEvent.EventEncoding.Universal;

var generator = new PayloadGenerator.CalendarEvent(subject, description, location, begin, end, alldayEvent, encoding);

generator.ToString().ShouldBe($"BEGIN:VEVENT{Environment.NewLine}SUMMARY:Release party{Environment.NewLine}DESCRIPTION:A small party for the new QRCoder. Bring some beer!{Environment.NewLine}LOCATION:Programmer's paradise, Beachtown, Paradise{Environment.NewLine}DTSTART:20160103T120000Z{Environment.NewLine}DTEND:20160103T143000Z{Environment.NewLine}END:VEVENT");
}


[Fact]
[Category("PayloadGenerator/CalendarEvent")]
public void calendarevent_should_build_with_utc_offset()
{
var subject = "Release party";
var description = "A small party for the new QRCoder. Bring some beer!";
var location = "Programmer's paradise, Beachtown, Paradise";
var alldayEvent = false;
var begin = new DateTimeOffset(2016, 01, 03, 12, 00, 00, new TimeSpan(3, 0, 0));
var end = new DateTimeOffset(2016, 01, 03, 14, 30, 00, new TimeSpan(3, 0, 0));
var encoding = PayloadGenerator.CalendarEvent.EventEncoding.Universal;

var generator = new PayloadGenerator.CalendarEvent(subject, description, location, begin, end, alldayEvent, encoding);

generator.ToString().ShouldBe($"BEGIN:VEVENT{Environment.NewLine}SUMMARY:Release party{Environment.NewLine}DESCRIPTION:A small party for the new QRCoder. Bring some beer!{Environment.NewLine}LOCATION:Programmer's paradise, Beachtown, Paradise{Environment.NewLine}DTSTART:20160103T090000Z{Environment.NewLine}DTEND:20160103T113000Z{Environment.NewLine}END:VEVENT");
}


[Fact]
[Category("PayloadGenerator/CalendarEvent")]
public void calendarevent_should_build_allday()
Expand Down