Skip to content

Commit 0fd85a3

Browse files
authored
feat: added datetimeoffset support to send_at (#106)
1 parent af98820 commit 0fd85a3

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

Smtpapi/HeaderTests/TestHeader.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void TestSetIpPool()
120120
}
121121

122122
[Test]
123-
public void TestSetSendAt()
123+
public void TestSetSendAt_DateTime()
124124
{
125125
var test = new Header();
126126
var now = DateTime.UtcNow;
@@ -130,13 +130,33 @@ public void TestSetSendAt()
130130
}
131131

132132
[Test]
133-
public void TestSetSendEachAt()
133+
public void TestSetSendAt_DateTimeOffset()
134+
{
135+
var test = new Header();
136+
var now = DateTimeOffset.UtcNow;
137+
test.SetSendAt(now);
138+
string result = test.JsonString();
139+
Assert.AreEqual("{\"send_at\" : " + Utils.DateTimeOffsetToUnixTimestamp(now) + "}", result);
140+
}
141+
142+
[Test]
143+
public void TestSetSendEachAt_DateTime()
134144
{
135145
var test = new Header();
136146
var now = DateTime.UtcNow;
137147
test.SetSendEachAt(new List<DateTime> { now, now.AddSeconds(10) });
138148
string result = test.JsonString();
139149
Assert.AreEqual("{\"send_each_at\" : [" + Utils.DateTimeToUnixTimestamp(now) + "," + Utils.DateTimeToUnixTimestamp(now.AddSeconds(10)) + "]}", result);
140150
}
151+
152+
[Test]
153+
public void TestSetSendEachAt_DateTimeOffset()
154+
{
155+
var test = new Header();
156+
var now = DateTimeOffset.UtcNow;
157+
test.SetSendEachAt(new List<DateTimeOffset> { now, now.AddSeconds(10) });
158+
string result = test.JsonString();
159+
Assert.AreEqual("{\"send_each_at\" : [" + Utils.DateTimeOffsetToUnixTimestamp(now) + "," + Utils.DateTimeOffsetToUnixTimestamp(now.AddSeconds(10)) + "]}", result);
160+
}
141161
}
142-
}
162+
}

Smtpapi/Smtpapi/Header.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public void SetIpPool(string pool)
172172

173173
/// <summary>
174174
/// Schedule the email to be sent in the future. You can find further documentation about scheduled sends here:
175-
/// https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html
175+
/// https://sendgrid.com/docs/for-developers/sending-email/scheduling-parameters/
176176
/// </summary>
177177
/// <param name="sendTime">DateTime representing the time to send the email. See docs for limitations. </param>
178178
public void SetSendAt(DateTime sendTime)
@@ -181,16 +181,37 @@ public void SetSendAt(DateTime sendTime)
181181
_settings.AddSetting(keys, Utils.DateTimeToUnixTimestamp(sendTime));
182182
}
183183

184+
/// <summary>
185+
/// Schedule the email to be sent in the future. You can find further documentation about scheduled sends here:
186+
/// https://sendgrid.com/docs/for-developers/sending-email/scheduling-parameters/
187+
/// </summary>
188+
/// <param name="sendTime">DateTimeOffset representing the time to send the email. See docs for limitations. </param>
189+
public void SetSendAt(DateTimeOffset sendTime)
190+
{
191+
var keys = new List<string> { "send_at" };
192+
_settings.AddSetting(keys, Utils.DateTimeOffsetToUnixTimestamp(sendTime));
193+
}
194+
184195
/// <summary>
185196
/// Schedule each email in your batch to be sent at a specific time in the future. You can find further documentation about scheduled sends here:
186-
/// https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html
197+
/// https://sendgrid.com/docs/for-developers/sending-email/scheduling-parameters/
187198
/// </summary>
188199
/// <param name="sendDateTimes">A collection of DateTimes, with each time corresponding to one recipient in the SMTP API header</param>
189200
public void SetSendEachAt(IEnumerable<DateTime> sendDateTimes)
190201
{
191202
_settings.AddArray(new List<string> { "send_each_at" }, sendDateTimes.Select(Utils.DateTimeToUnixTimestamp).Cast<object>().ToArray());
192203
}
193204

205+
/// <summary>
206+
/// Schedule each email in your batch to be sent at a specific time in the future. You can find further documentation about scheduled sends here:
207+
/// https://sendgrid.com/docs/for-developers/sending-email/scheduling-parameters/
208+
/// </summary>
209+
/// <param name="sendDateTimes">A collection of DateTimeOffsets, with each time corresponding to one recipient in the SMTP API header</param>
210+
public void SetSendEachAt(IEnumerable<DateTimeOffset> sendDateTimes)
211+
{
212+
_settings.AddArray(new List<string> { "send_each_at" }, sendDateTimes.Select(Utils.DateTimeOffsetToUnixTimestamp).Cast<object>().ToArray());
213+
}
214+
194215
#endregion
195216
}
196-
}
217+
}

Smtpapi/Smtpapi/Utils.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,16 @@ public static int DateTimeToUnixTimestamp(DateTime dateTime)
6565
var span = (dateTime - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
6666
return (int)span.TotalSeconds;
6767
}
68+
69+
/// <summary>
70+
/// Convert a DateTimeOffset to a UNIX Epoch Timestamp
71+
/// </summary>
72+
/// <param name="dateTimeOffset">Date to convert to timestamp</param>
73+
/// <returns>Timestamp</returns>
74+
public static int DateTimeOffsetToUnixTimestamp(DateTimeOffset dateTimeOffset)
75+
{
76+
var span = (dateTimeOffset - new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero));
77+
return (int)span.TotalSeconds;
78+
}
6879
}
69-
}
80+
}

0 commit comments

Comments
 (0)