Skip to content

Commit

Permalink
Added DateTimeToUnixTime() method (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Dec 4, 2022
1 parent 584d971 commit b811051
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions PeyrSharp.Core/Converters/Time.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,16 @@ public static DateTime UnixTimeToDateTime(int unixTime)
dtDateTime = dtDateTime.AddSeconds(unixTime).ToLocalTime(); // Add the seconds
return dtDateTime; // Return the result
}

/// <summary>
/// Converts a <see cref="DateTime"/> to Unix Time.
/// </summary>
/// <param name="dateTime">The <see cref="DateTime"/> to convert.</param>
/// <returns>The converted <see cref="DateTime"/> in unix time.</returns>
public static int DateTimeToUnixTime(DateTime dateTime)
{
DateTime dtDateTime = new(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); // Create a date
return (int)dateTime.Subtract(dtDateTime).TotalSeconds; // Return the result
}
}
}

0 comments on commit b811051

Please sign in to comment.