From b81105146d1a31d278259580df6fb42e4ba66962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Peyronnet?= Date: Sun, 4 Dec 2022 09:10:01 +0100 Subject: [PATCH] Added ``DateTimeToUnixTime()`` method (#43) --- PeyrSharp.Core/Converters/Time.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/PeyrSharp.Core/Converters/Time.cs b/PeyrSharp.Core/Converters/Time.cs index 26bef1b..002d22b 100644 --- a/PeyrSharp.Core/Converters/Time.cs +++ b/PeyrSharp.Core/Converters/Time.cs @@ -107,5 +107,16 @@ public static DateTime UnixTimeToDateTime(int unixTime) dtDateTime = dtDateTime.AddSeconds(unixTime).ToLocalTime(); // Add the seconds return dtDateTime; // Return the result } + + /// + /// Converts a to Unix Time. + /// + /// The to convert. + /// The converted in unix time. + 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 + } } }