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:
		| @@ -20,46 +20,10 @@ namespace MontoyaTech.MySqlPlus.Example | |||||||
|             [MySqlColumn("year")] |             [MySqlColumn("year")] | ||||||
|             public uint Year = 0; |             public uint Year = 0; | ||||||
|  |  | ||||||
|             [MySqlColumn("dateCreated", typeof(DateTime2UnixConverter))] |             [MySqlColumn("dateCreated", typeof(DateTimeToUnixConverter))] | ||||||
|             public DateTime DateCreated = DateTime.UtcNow; |             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) |         public static void Main(string[] args) | ||||||
|         { |         { | ||||||
|             var session = new MySqlSession(""); |             var session = new MySqlSession(""); | ||||||
|   | |||||||
							
								
								
									
										59
									
								
								MySqlPlus/DateTimeToUnixConverter.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								MySqlPlus/DateTimeToUnixConverter.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | |||||||
|  | using System; | ||||||
|  | using System.Collections.Generic; | ||||||
|  | using System.Linq; | ||||||
|  | using System.Text; | ||||||
|  | using System.Threading.Tasks; | ||||||
|  |  | ||||||
|  | namespace MontoyaTech.MySqlPlus | ||||||
|  | { | ||||||
|  |     /// <summary> | ||||||
|  |     /// The outline of a MySqlColumn converter that converts a DateTime to Unix and back. | ||||||
|  |     /// </summary> | ||||||
|  |     public class DateTimeToUnixConverter : MySqlColumnConverter | ||||||
|  |     { | ||||||
|  |         /// <summary> | ||||||
|  |         /// Converts from a Unix Time to a DateTime. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="input"></param> | ||||||
|  |         /// <returns></returns> | ||||||
|  |         /// <exception cref="NotSupportedException"></exception> | ||||||
|  |         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."); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Converts to a Unix Time from a DateTime. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="input"></param> | ||||||
|  |         /// <returns></returns> | ||||||
|  |         /// <exception cref="NotSupportedException"></exception> | ||||||
|  |         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."); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -7,7 +7,7 @@ | |||||||
|     <AssemblyName>MontoyaTech.MySqlPlus</AssemblyName> |     <AssemblyName>MontoyaTech.MySqlPlus</AssemblyName> | ||||||
|     <RootNamespace>MontoyaTech.MySqlPlus</RootNamespace> |     <RootNamespace>MontoyaTech.MySqlPlus</RootNamespace> | ||||||
|     <Title>MontoyaTech.MySqlPlus</Title> |     <Title>MontoyaTech.MySqlPlus</Title> | ||||||
|     <Version>1.0.1</Version> |     <Version>1.0.2</Version> | ||||||
|     <Company>MontoyaTech</Company> |     <Company>MontoyaTech</Company> | ||||||
|     <Description>A simple C# library to help work with MySql.</Description> |     <Description>A simple C# library to help work with MySql.</Description> | ||||||
|     <Copyright>MontoyaTech 2023</Copyright> |     <Copyright>MontoyaTech 2023</Copyright> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user