Skip to content

Commit 248bcb2

Browse files
committed
feat: include time option for dates
1 parent 8fc2848 commit 248bcb2

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

Src/Notion.Client/Models/PropertyValue/DatePropertyValue.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class DatePropertyValue : PropertyValue
2020
/// <summary>
2121
/// Date value object.
2222
/// </summary>
23+
[JsonConverter(typeof(DateCustomConverter))]
2324
public class Date
2425
{
2526
/// <summary>
@@ -40,5 +41,94 @@ public class Date
4041
/// </summary>
4142
[JsonProperty("time_zone")]
4243
public string TimeZone { get; set; }
44+
45+
/// <summary>
46+
/// Whether to include time
47+
/// </summary>
48+
public bool IncludeTime { get; set; } = true;
49+
}
50+
51+
public class DateJsonObject
52+
{
53+
[JsonProperty("start")]
54+
public string Start { get; set; }
55+
56+
[JsonProperty("end")]
57+
public string End { get; set; }
58+
59+
[JsonProperty("time_zone")]
60+
public string TimeZone { get; set; }
61+
}
62+
63+
public class DateCustomConverter : JsonConverter<Date>
64+
{
65+
public override Date ReadJson(JsonReader reader, Type objectType, Date existingValue, bool hasExistingValue,
66+
JsonSerializer serializer)
67+
{
68+
var jsonObject = serializer.Deserialize<DateJsonObject>(reader);
69+
70+
if (jsonObject == null)
71+
{
72+
return null;
73+
}
74+
75+
var date = new Date
76+
{
77+
Start = ParseDateTime(jsonObject.Start, out bool includeTime),
78+
End = ParseDateTime(jsonObject.End, out _),
79+
TimeZone = jsonObject.TimeZone,
80+
IncludeTime = includeTime,
81+
};
82+
83+
return date;
84+
}
85+
86+
public override void WriteJson(JsonWriter writer, Date value, JsonSerializer serializer)
87+
{
88+
if (value is null)
89+
{
90+
writer.WriteNull();
91+
92+
return;
93+
}
94+
95+
writer.WriteStartObject();
96+
97+
if (value.Start.HasValue)
98+
{
99+
string startFormat = value.IncludeTime ? "yyyy-MM-ddTHH:mm:ss" : "yyyy-MM-dd";
100+
writer.WritePropertyName("start");
101+
writer.WriteValue(value.Start.Value.ToString(startFormat));
102+
}
103+
104+
if (value.End.HasValue)
105+
{
106+
string endFormat = value.IncludeTime ? "yyyy-MM-ddTHH:mm:ss" : "yyyy-MM-dd";
107+
writer.WritePropertyName("end");
108+
writer.WriteValue(value.End.Value.ToString(endFormat));
109+
}
110+
111+
if (!string.IsNullOrEmpty(value.TimeZone))
112+
{
113+
writer.WritePropertyName("time_zone");
114+
writer.WriteValue(value.TimeZone);
115+
}
116+
117+
writer.WriteEndObject();
118+
}
119+
120+
private static DateTime? ParseDateTime(string dateTimeString, out bool includeTime)
121+
{
122+
includeTime = false;
123+
124+
if (string.IsNullOrEmpty(dateTimeString))
125+
{
126+
return null;
127+
}
128+
129+
includeTime = dateTimeString.Contains("T") || dateTimeString.Contains(" ");
130+
131+
return DateTimeOffset.Parse(dateTimeString).UtcDateTime;
132+
}
43133
}
44134
}

0 commit comments

Comments
 (0)