This repository has been archived by the owner on Feb 15, 2021. It is now read-only.
/
Extensions.cs
49 lines (45 loc) · 1.83 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace Unosquare.Labs.LiteLib
{
using System;
using System.Collections.Generic;
/// <summary>
/// Extension methods.
/// </summary>
public static class Extensions
{
private const string IntegerAffinity = "INTEGER";
private const string NumericAffinity = "NUMERIC";
private const string TextAffinity = "TEXT";
private const string DateTimeAffinity = "DATETIME";
private static readonly Dictionary<Type, string> TypeMappings = new Dictionary<Type, string>
{
{typeof(short), IntegerAffinity},
{typeof(int), IntegerAffinity},
{typeof(long), IntegerAffinity},
{typeof(ushort), IntegerAffinity},
{typeof(uint), IntegerAffinity},
{typeof(ulong), IntegerAffinity},
{typeof(byte), IntegerAffinity},
{typeof(char), IntegerAffinity},
{typeof(decimal), NumericAffinity},
{typeof(bool), NumericAffinity},
{typeof(DateTime), DateTimeAffinity},
};
/// <summary>
/// Gets the type mapping.
/// </summary>
/// <param name="propertyType">Type of the property.</param>
/// <returns>A property type of the mapping.</returns>
public static string GetTypeMapping(this Type propertyType) => TypeMappings.ContainsKey(propertyType) ? TypeMappings[propertyType] : TextAffinity;
/// <summary>
/// Transform a DateTime to a SQLite UTC date.
/// </summary>
/// <param name="utcDate">The UTC date.</param>
/// <returns>UTC DateTime.</returns>
public static DateTime ToSQLiteUtcDate(this DateTime utcDate)
{
var startupDifference = (int)DateTime.UtcNow.Subtract(DateTime.Now).TotalHours;
return utcDate.AddHours(startupDifference);
}
}
}