Moved the DateTime To Unix converter an official converter apart of the library. Bumped package version to 1.0.2

This commit is contained in:
2023-01-30 21:28:34 -08:00
parent b125aef0ff
commit 70dd86766b
3 changed files with 61 additions and 38 deletions

View File

@ -20,46 +20,10 @@ namespace MontoyaTech.MySqlPlus.Example
[MySqlColumn("year")]
public uint Year = 0;
[MySqlColumn("dateCreated", typeof(DateTime2UnixConverter))]
[MySqlColumn("dateCreated", typeof(DateTimeToUnixConverter))]
public DateTime DateCreated = DateTime.UtcNow;
}
public class DateTime2UnixConverter : MySqlColumnConverter
{
public object ConvertFrom(object input)
{
if (input is ulong unix)
{
if (unix == 0)
return DateTime.MinValue;
return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(unix).ToUniversalTime();
}
else
{
throw new NotSupportedException("Unsupported input object to convert from unix.");
}
}
public object ConvertTo(object input)
{
if (input is DateTime dateTime)
{
if (dateTime == DateTime.MinValue)
return 0;
if (dateTime == DateTime.MaxValue)
return ulong.MaxValue;
return (ulong)(dateTime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))).TotalSeconds;
}
else
{
throw new NotSupportedException("Unsupported input object to convert to unix.");
}
}
}
public static void Main(string[] args)
{
var session = new MySqlSession("");