Skip to content

Commit cff7585

Browse files
committed
feat: include time option for dates
1 parent 2293f45 commit cff7585

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

0 commit comments

Comments
 (0)