Skip to content

Commit

Permalink
more date stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Oct 23, 2022
1 parent f000979 commit 84e250c
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-10-01
2000-10-01 Local
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-10-01
2000-10-01Local
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-10-01
2000-10-01 +0
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-10-01
2000-10-01+0
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-10-01
2000-10-01 +1:30
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-10-01
2000-10-01+1:30
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-10-01
2000-10-01 Utc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-10-01
2000-10-01Utc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-10-01
2000-10-01Local
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-10-01
2000-10-01+11
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-10-01
2000-10-01+0
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-10-01
2000-10-01Utc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
noTime: 2000-01-01,
withTime: 2000-01-01 01:01:01,
withTimeZeroSeconds: 2000-01-01 01:01,
withTimeMilliSeconds: 2000-01-01 01:01:01.999
noTime: 2000-01-01 +1:30,
withTime: 2000-01-01 01:01:01 +1,
withTimeZeroSeconds: 2000-01-01 01:01 +1,
withTimeMilliSeconds: 2000-01-01 01:01:01.999 +1
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
noTime: 2000-01-01,
withTime: 2000-01-01 01:01:01,
withTimeZeroSeconds: 2000-01-01 01:01,
withTimeMilliSeconds: 2000-01-01 01:01:01.999
noTime: 2000-01-01 Utc,
withTime: 2000-01-01 01:01:01 Utc,
withTimeZeroSeconds: 2000-01-01 01:01 Utc,
withTimeMilliSeconds: 2000-01-01 01:01:01.999 Utc
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
Date: 2020-10-10
Date: 2020-10-10 Utc
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
Date: 2020-10-10
Date: 2020-10-10 Utc
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-01-01 01:01:01
2000-01-01 01:01:01 Utc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-01-01 01:01:01
2000-01-01 01:01:01 Utc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2000-01-01 00:01:01.001
2000-01-01 00:01:01.001 +0
66 changes: 66 additions & 0 deletions src/Verify/Serialization/DateFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
static class DateFormatter
{
public static string ToJsonString(DateTimeOffset value)
{
var result = GetJsonDatePart(value);
result += $" {GetDateOffset(value)}";
return result;
}

static string GetJsonDatePart(DateTimeOffset value)
{
if (value.TimeOfDay == TimeSpan.Zero)
{
Expand All @@ -21,6 +28,14 @@ public static string ToJsonString(DateTimeOffset value)
}

public static string ToParameterString(DateTimeOffset value)
{
var result = GetParameterDatePart(value);
result += GetDateOffset(value);

return result;
}

static string GetParameterDatePart(DateTimeOffset value)
{
if (value.TimeOfDay == TimeSpan.Zero)
{
Expand All @@ -41,6 +56,18 @@ public static string ToParameterString(DateTimeOffset value)
}

public static string ToJsonString(DateTime value)
{
var result = GetJsonDatePart(value);

if (value.Kind != DateTimeKind.Unspecified)
{
result += $" {value.Kind}";
}

return result;
}

static string GetJsonDatePart(DateTime value)
{
if (value.TimeOfDay == TimeSpan.Zero)
{
Expand All @@ -61,6 +88,18 @@ public static string ToJsonString(DateTime value)
}

public static string ToParameterString(DateTime value)
{
var result = GetParameterDatePart(value);

if (value.Kind != DateTimeKind.Unspecified)
{
result += value.Kind;
}

return result;
}

static string GetParameterDatePart(DateTime value)
{
if (value.TimeOfDay == TimeSpan.Zero)
{
Expand All @@ -79,4 +118,31 @@ public static string ToParameterString(DateTime value)

return value.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFF", CultureInfo.InvariantCulture);
}

static string GetDateOffset(DateTimeOffset value)
{
var offset = value.Offset;

if (offset > TimeSpan.Zero)
{
if (offset.Minutes == 0)
{
return $"+{offset.TotalHours:0}";
}

return $"+{offset.Hours:0}:{offset.Minutes:00}";
}

if (offset < TimeSpan.Zero)
{
if (offset.Minutes == 0)
{
return $"-{offset.Hours:0}";
}

return $"-{offset.Hours:0}:{offset.Minutes:00}";
}

return "+0";
}
}

0 comments on commit 84e250c

Please sign in to comment.