Skip to content

Instantly share code, notes, and snippets.

@kpespisa
kpespisa / EpochConversion
Created February 20, 2015 16:39
Convert to and from Epoch
public static class DateTimeHelper
{
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static DateTime UnixTimeToDateTime(string text)
{
double seconds = double.Parse(text, CultureInfo.InvariantCulture);
var time = Epoch.AddSeconds(seconds);
return time;
@kpespisa
kpespisa / SQLiteExtensions
Last active June 2, 2017 07:04
Save and Delete Extensions to SQLite-NET
public static class SQLiteExtensions
{
/// <summary>
/// Save the specified entity by calling insert or update, if the entity already exists.
/// </summary>
/// <param name="pk">The primary key of the entity</param>
/// <param name="obj">The instance of the entity</param>
/// <typeparam name="T">The entity type.</typeparam>
public static int Save<T>(this SQLiteConnection db, object pk, object obj) where T : class, new()
{
@kpespisa
kpespisa / ToSafeString() extension method
Created February 13, 2015 21:56
ToSafeString() extension method to get an object's string representation or the empty string if the object is null
public static string ToSafeString(this object obj)
{
if (obj == null)
{
return string.Empty;
}
return obj.ToString();
}
@kpespisa
kpespisa / GetValueOrDefault() extension for dictionaries
Created February 13, 2015 21:47
GetValueOrDefault() extension method for Dictionaries
public static TValue GetValueOrDefault<TKey, TValue> (this IDictionary<TKey, TValue> dictionary, TKey key)
{
TValue ret;
dictionary.TryGetValue(key, out ret);
return ret;
}