Skip to content

Commit

Permalink
imp - prt - Used POSIX time tools to improve date ...
Browse files Browse the repository at this point in the history
...parsing

---

You can now use the official standard date and time representation that is documented in the RFCs.

---

Type: imp
Breaking: False
Doc Required: False
Backport Required: False
Part: 2/2
  • Loading branch information
AptiviCEO committed Sep 10, 2024
1 parent 45e26c0 commit 3f3b513
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 32 deletions.
9 changes: 5 additions & 4 deletions VisualCard.Calendar/Parts/Implementations/DateCreatedInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using System;
using System.Diagnostics;
using VisualCard.Parsers;

namespace VisualCard.Calendar.Parts.Implementations
{
Expand All @@ -31,18 +32,18 @@ public class DateCreatedInfo : BaseCalendarPartInfo, IEquatable<DateCreatedInfo>
/// <summary>
/// The card's revision
/// </summary>
public DateTime? DateCreated { get; }
public DateTime DateCreated { get; }

internal static BaseCalendarPartInfo FromStringVcalendarStatic(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion) =>
new DateCreatedInfo().FromStringVcalendarInternal(value, finalArgs, elementTypes, valueType, cardVersion);

internal override string ToStringVcalendarInternal(Version cardVersion) =>
$"{DateCreated:yyyy-MM-dd HH:mm:ss}";
$"{VcardParserTools.SavePosixDate(DateCreated)}";

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
// Populate the fields
DateTime created = DateTime.Parse(value);
DateTime created = VcardParserTools.ParsePosixDate(value);

// Add the fetched information
DateCreatedInfo _time = new(finalArgs, elementTypes, valueType, created);
Expand Down Expand Up @@ -101,7 +102,7 @@ internal override bool EqualsInternal(BaseCalendarPartInfo source, BaseCalendarP

internal DateCreatedInfo() { }

internal DateCreatedInfo(string[] arguments, string[] elementTypes, string valueType, DateTime? rev) :
internal DateCreatedInfo(string[] arguments, string[] elementTypes, string valueType, DateTime rev) :
base(arguments, elementTypes, valueType)
{
DateCreated = rev;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using System;
using System.Diagnostics;
using VisualCard.Parsers;

namespace VisualCard.Calendar.Parts.Implementations.Event
{
Expand All @@ -31,18 +32,18 @@ public class DateEndInfo : BaseCalendarPartInfo, IEquatable<DateEndInfo>
/// <summary>
/// The card's revision
/// </summary>
public DateTime? DateEnd { get; }
public DateTime DateEnd { get; }

internal static BaseCalendarPartInfo FromStringVcalendarStatic(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion) =>
new DateEndInfo().FromStringVcalendarInternal(value, finalArgs, elementTypes, valueType, cardVersion);

internal override string ToStringVcalendarInternal(Version cardVersion) =>
$"{DateEnd:yyyy-MM-dd HH:mm:ss}";
$"{VcardParserTools.SavePosixDate(DateEnd)}";

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
// Populate the fields
DateTime end = DateTime.Parse(value);
DateTime end = VcardParserTools.ParsePosixDate(value);

// Add the fetched information
DateEndInfo _time = new(finalArgs, elementTypes, valueType, end);
Expand Down Expand Up @@ -101,7 +102,7 @@ internal override bool EqualsInternal(BaseCalendarPartInfo source, BaseCalendarP

internal DateEndInfo() { }

internal DateEndInfo(string[] arguments, string[] elementTypes, string valueType, DateTime? rev) :
internal DateEndInfo(string[] arguments, string[] elementTypes, string valueType, DateTime rev) :
base(arguments, elementTypes, valueType)
{
DateEnd = rev;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using System;
using System.Diagnostics;
using VisualCard.Parsers;

namespace VisualCard.Calendar.Parts.Implementations.Event
{
Expand All @@ -31,18 +32,18 @@ public class DateStampInfo : BaseCalendarPartInfo, IEquatable<DateStampInfo>
/// <summary>
/// The card's revision
/// </summary>
public DateTime? DateStamp { get; }
public DateTime DateStamp { get; }

internal static BaseCalendarPartInfo FromStringVcalendarStatic(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion) =>
new DateStampInfo().FromStringVcalendarInternal(value, finalArgs, elementTypes, valueType, cardVersion);

internal override string ToStringVcalendarInternal(Version cardVersion) =>
$"{DateStamp:yyyy-MM-dd HH:mm:ss}";
$"{VcardParserTools.SavePosixDate(DateStamp)}";

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
// Populate the fields
DateTime stamp = DateTime.Parse(value);
DateTime stamp = VcardParserTools.ParsePosixDate(value);

// Add the fetched information
DateStampInfo _time = new(finalArgs, elementTypes, valueType, stamp);
Expand Down Expand Up @@ -101,7 +102,7 @@ internal override bool EqualsInternal(BaseCalendarPartInfo source, BaseCalendarP

internal DateStampInfo() { }

internal DateStampInfo(string[] arguments, string[] elementTypes, string valueType, DateTime? rev) :
internal DateStampInfo(string[] arguments, string[] elementTypes, string valueType, DateTime rev) :
base(arguments, elementTypes, valueType)
{
DateStamp = rev;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using System;
using System.Diagnostics;
using VisualCard.Parsers;

namespace VisualCard.Calendar.Parts.Implementations.Event
{
Expand All @@ -31,18 +32,18 @@ public class DateStartInfo : BaseCalendarPartInfo, IEquatable<DateStartInfo>
/// <summary>
/// The card's revision
/// </summary>
public DateTime? DateStart { get; }
public DateTime DateStart { get; }

internal static BaseCalendarPartInfo FromStringVcalendarStatic(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion) =>
new DateStartInfo().FromStringVcalendarInternal(value, finalArgs, elementTypes, valueType, cardVersion);

internal override string ToStringVcalendarInternal(Version cardVersion) =>
$"{DateStart:yyyy-MM-dd HH:mm:ss}";
$"{VcardParserTools.SavePosixDate(DateStart)}";

internal override BaseCalendarPartInfo FromStringVcalendarInternal(string value, string[] finalArgs, string[] elementTypes, string valueType, Version cardVersion)
{
// Populate the fields
DateTime start = DateTime.Parse(value);
DateTime start = VcardParserTools.ParsePosixDate(value);

// Add the fetched information
DateStartInfo _time = new(finalArgs, elementTypes, valueType, start);
Expand Down Expand Up @@ -101,7 +102,7 @@ internal override bool EqualsInternal(BaseCalendarPartInfo source, BaseCalendarP

internal DateStartInfo() { }

internal DateStartInfo(string[] arguments, string[] elementTypes, string valueType, DateTime? rev) :
internal DateStartInfo(string[] arguments, string[] elementTypes, string valueType, DateTime rev) :
base(arguments, elementTypes, valueType)
{
DateStart = rev;
Expand Down
4 changes: 2 additions & 2 deletions VisualCard.ShowCalendars/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ static void Main(string[] args)
var starts = Calendar.GetPartsArray<DateStartInfo>();
var ends = Calendar.GetPartsArray<DateEndInfo>();
if (starts.Length > 0)
TextWriterColor.Write("Calendar start date: {0}", starts[0].DateStart ?? new());
TextWriterColor.Write("Calendar start date: {0}", starts[0].DateStart);
if (ends.Length > 0)
TextWriterColor.Write("Calendar end date: {0}", ends[0].DateEnd ?? new());
TextWriterColor.Write("Calendar end date: {0}", ends[0].DateEnd);
TextWriterColor.Write("Calendar product ID: {0}", Calendar.GetString(CalendarStringsEnum.ProductId));
TextWriterColor.Write("Calendar UUID: {0}", Calendar.UniqueId);
TextWriterColor.Write("Calendar events: {0} event(s)", Calendar.Events.Length);
Expand Down
4 changes: 2 additions & 2 deletions VisualCard.ShowContacts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ static void Main(string[] args)
var url = Contact.GetPartsArray<UrlInfo>();
var note = Contact.GetPartsArray<NoteInfo>();
if (birth.Length > 0)
TextWriterColor.Write("Contact birthdate: {0}", birth[0].BirthDate ?? new());
TextWriterColor.Write("Contact birthdate: {0}", birth[0].BirthDate);
if (wedding.Length > 0)
TextWriterColor.Write("Contact wedding date: {0}", wedding[0].Anniversary ?? new());
TextWriterColor.Write("Contact wedding date: {0}", wedding[0].Anniversary);
if (gender.Length > 0)
TextWriterColor.Write("Contact gender {0} [{1}]", gender[0].Gender.ToString(), gender[0].GenderDescription ?? "");
if (url.Length > 0)
Expand Down
8 changes: 4 additions & 4 deletions VisualCard/Parts/Implementations/AnniversaryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public class AnniversaryInfo : BaseCardPartInfo, IEquatable<AnniversaryInfo>
/// <summary>
/// The contact's wedding anniversary date (that is, the day that this contact is married)
/// </summary>
public DateTime? Anniversary { get; }
public DateTime Anniversary { get; }

internal static BaseCardPartInfo FromStringVcardStatic(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion) =>
new AnniversaryInfo().FromStringVcardInternal(value, finalArgs, altId, elementTypes, valueType, cardVersion);

internal override string ToStringVcardInternal(Version cardVersion) =>
$"{Anniversary:yyyyMMdd}";
$"{VcardParserTools.SavePosixDate(Anniversary, true)}";

internal override BaseCardPartInfo FromStringVcardInternal(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion)
{
Expand All @@ -55,7 +55,7 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
anniversary = new DateTime(anniversaryYear, anniversaryMonth, anniversaryDay);
}
else
anniversary = DateTime.Parse(value);
anniversary = VcardParserTools.ParsePosixDate(value);

// Add the fetched information
AnniversaryInfo _time = new(-1, [], [], valueType, anniversary);
Expand Down Expand Up @@ -114,7 +114,7 @@ internal override bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo

internal AnniversaryInfo() { }

internal AnniversaryInfo(int altId, string[] arguments, string[] elementTypes, string valueType, DateTime? anniversary) :
internal AnniversaryInfo(int altId, string[] arguments, string[] elementTypes, string valueType, DateTime anniversary) :
base(arguments, altId, elementTypes, valueType)
{
Anniversary = anniversary;
Expand Down
8 changes: 4 additions & 4 deletions VisualCard/Parts/Implementations/BirthDateInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public class BirthDateInfo : BaseCardPartInfo, IEquatable<BirthDateInfo>
/// <summary>
/// The contact's birth date
/// </summary>
public DateTime? BirthDate { get; }
public DateTime BirthDate { get; }

internal static BaseCardPartInfo FromStringVcardStatic(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion) =>
new BirthDateInfo().FromStringVcardInternal(value, finalArgs, altId, elementTypes, valueType, cardVersion);

internal override string ToStringVcardInternal(Version cardVersion) =>
$"{BirthDate:yyyyMMdd}";
$"{VcardParserTools.SavePosixDate(BirthDate, true)}";

internal override BaseCardPartInfo FromStringVcardInternal(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion)
{
Expand All @@ -55,7 +55,7 @@ internal override BaseCardPartInfo FromStringVcardInternal(string value, string[
bday = new DateTime(birthYear, birthMonth, birthDay);
}
else
bday = DateTime.Parse(value);
bday = VcardParserTools.ParsePosixDate(value);

// Add the fetched information
BirthDateInfo _time = new(altId, finalArgs, elementTypes, valueType, bday);
Expand Down Expand Up @@ -114,7 +114,7 @@ internal override bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo

internal BirthDateInfo() { }

internal BirthDateInfo(int altId, string[] arguments, string[] elementTypes, string valueType, DateTime? birth) :
internal BirthDateInfo(int altId, string[] arguments, string[] elementTypes, string valueType, DateTime birth) :
base(arguments, altId, elementTypes, valueType)
{
BirthDate = birth;
Expand Down
8 changes: 4 additions & 4 deletions VisualCard/Parts/Implementations/RevisionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ public class RevisionInfo : BaseCardPartInfo, IEquatable<RevisionInfo>
/// <summary>
/// The card's revision
/// </summary>
public DateTime? Revision { get; }
public DateTime Revision { get; }

internal static BaseCardPartInfo FromStringVcardStatic(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion) =>
new RevisionInfo().FromStringVcardInternal(value, finalArgs, altId, elementTypes, valueType, cardVersion);

internal override string ToStringVcardInternal(Version cardVersion) =>
$"{Revision:yyyy-MM-dd HH:mm:ss}";
$"{VcardParserTools.SavePosixDate(Revision)}";

internal override BaseCardPartInfo FromStringVcardInternal(string value, string[] finalArgs, int altId, string[] elementTypes, string valueType, Version cardVersion)
{
// Get the value
string revValue = value.Substring(VcardConstants._revSpecifier.Length + 1);

// Populate the fields
DateTime rev = DateTime.Parse(revValue);
DateTime rev = VcardParserTools.ParsePosixDate(revValue);

// Add the fetched information
RevisionInfo _time = new(altId, finalArgs, elementTypes, valueType, rev);
Expand Down Expand Up @@ -105,7 +105,7 @@ internal override bool EqualsInternal(BaseCardPartInfo source, BaseCardPartInfo

internal RevisionInfo() { }

internal RevisionInfo(int altId, string[] arguments, string[] elementTypes, string valueType, DateTime? rev) :
internal RevisionInfo(int altId, string[] arguments, string[] elementTypes, string valueType, DateTime rev) :
base(arguments, altId, elementTypes, valueType)
{
Revision = rev;
Expand Down

0 comments on commit 3f3b513

Please sign in to comment.