Skip to content

Commit

Permalink
Merge pull request #499 from codebude/feature/enhance-datetime-handli…
Browse files Browse the repository at this point in the history
…ng-in-ical-generator

Added support for UTC and DateTimeOffsets in VEVENT generator
  • Loading branch information
codebude authored Apr 24, 2024
2 parents 8742e07 + f370952 commit 8e226f2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
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

0 comments on commit 8e226f2

Please sign in to comment.