Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Nullinside.Api.Common/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Nullinside.Api.Common;

/// <summary>
/// Extension methods for <see cref="DateTime" />.
/// </summary>
public static class DateTimeExtensions {
/// <summary>
/// Converts unix timestamp to a <see cref="DateTime" />.
/// </summary>
/// <param name="unixTimestamp">The unix timestamp.</param>
/// <returns>The DateTime representation of the unix timestamp.</returns>
public static DateTime FromUnixTimestamp(double unixTimestamp) {
// Unix timestamp is seconds past epoch
var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTime = dateTime.AddSeconds(unixTimestamp / 1000d).ToLocalTime();
return dateTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using TwitchLib.Client.Events;

namespace Nullinside.Api.Common.Twitch;

/// <summary>
/// Extensions for the <see cref="OnMessageReceivedArgs" /> class to make working with twitch chat messages easier.
/// </summary>
public static class OnMessageReceivedArgsExtensions {
/// <summary>
/// Gets the timestamp of when the message was sent, in UTC.
/// </summary>
/// <param name="e">The event arguments.</param>
/// <returns>The <see cref="DateTime" /> if successful, null otherwise.</returns>
public static DateTime? GetTimestamp(this OnMessageReceivedArgs e) {
if (double.TryParse(e.ChatMessage.TmiSentTs, out double timestampD)) {
return DateTimeExtensions.FromUnixTimestamp(timestampD);
}

return null;
}
}