Skip to content

Instantly share code, notes, and snippets.

View gmcelhanon's full-sized avatar

Geoff McElhanon gmcelhanon

View GitHub Profile
@gmcelhanon
gmcelhanon / DateTimeExtensions.cs
Created January 31, 2018 19:27
Extension methods for converting .NET DateTime to/from UNIX time
public static DateTime FromUnixTime(this long unixTime)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return epoch.AddSeconds(unixTime);
}
public static long ToUnixTime(this DateTime date)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToInt64((date - epoch).TotalSeconds);
@gmcelhanon
gmcelhanon / LambdaEqualityComparer
Last active December 13, 2017 04:17
A generically typed equality comparer implementation based on Lambda expressions (removing the need for an explicit class implementation)
using System;
using System.Collections.Generic;
namespace Comparers
{
/// <summary>
/// Implements an equality comparer that allows the method implementations
/// to be supplied as functions in the constructor, eliminating the need
/// for a custom class to be implemented.
/// </summary>
@gmcelhanon
gmcelhanon / gist:ba91ec91628214d375c2ac1f6d6146fa
Last active August 4, 2016 05:00
ToDictionary method that wraps Linq's ToDictionary, but then lists the keys and identifies duplicates for troubleshooting purposes
/// <summary>
/// Provides a wrapper method around <see cref="Enumerable.ToDictionary"/> to provide details about
/// the duplicate keys encountered (for troubleshooting purposes).
/// </summary>
public static class EnumerableHelper
{
/// <summary>
/// Wraps the standard LINQ ToDictionary extension method to provide additional details about
/// the keys that were to be added to the dictionary.
@gmcelhanon
gmcelhanon / SerializationHelper
Created February 24, 2016 17:40
Compresses and serializes data (using binary formatter) to and from base 64 encoding.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
namespace GMac.Serialization
{
public static class SerializationHelper
{
// This class will not be serialized by protobuf-net because of the use of IEnumerable<T>:
[Serializable, ProtoContract]
public class GoalPlanningModel : ResourceModelBase
{
[ProtoMember(1)]
public IEnumerable<ProposedGoal> ProposedGoals { get; set; }
[ProtoMember(2)]
public IEnumerable<PublishedGoal> PublishedGoals { get; set; }