Skip to content

Commit b276e7a

Browse files
Merge pull request #98 from nullinside-development-group/feat/history
feat: keeping a history of chat messages
2 parents 4036eff + 9e5f7ce commit b276e7a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Nullinside.Api.Common;
2+
3+
/// <summary>
4+
/// Extension methods for <see cref="DateTime" />.
5+
/// </summary>
6+
public static class DateTimeExtensions {
7+
/// <summary>
8+
/// Converts unix timestamp to a <see cref="DateTime" />.
9+
/// </summary>
10+
/// <param name="unixTimestamp">The unix timestamp.</param>
11+
/// <returns>The DateTime representation of the unix timestamp.</returns>
12+
public static DateTime FromUnixTimestamp(double unixTimestamp) {
13+
// Unix timestamp is seconds past epoch
14+
var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
15+
dateTime = dateTime.AddSeconds(unixTimestamp / 1000d).ToLocalTime();
16+
return dateTime;
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using TwitchLib.Client.Events;
2+
3+
namespace Nullinside.Api.Common.Twitch;
4+
5+
/// <summary>
6+
/// Extensions for the <see cref="OnMessageReceivedArgs" /> class to make working with twitch chat messages easier.
7+
/// </summary>
8+
public static class OnMessageReceivedArgsExtensions {
9+
/// <summary>
10+
/// Gets the timestamp of when the message was sent, in UTC.
11+
/// </summary>
12+
/// <param name="e">The event arguments.</param>
13+
/// <returns>The <see cref="DateTime" /> if successful, null otherwise.</returns>
14+
public static DateTime? GetTimestamp(this OnMessageReceivedArgs e) {
15+
if (double.TryParse(e.ChatMessage.TmiSentTs, out double timestampD)) {
16+
return DateTimeExtensions.FromUnixTimestamp(timestampD);
17+
}
18+
19+
return null;
20+
}
21+
}

0 commit comments

Comments
 (0)