Skip to content

Instantly share code, notes, and snippets.

View rianjs's full-sized avatar

Rian Stockbower rianjs

View GitHub Profile
@rianjs
rianjs / NodaTime SQL schema.sql
Last active August 9, 2017 17:31
Matt Johnson's NodaTime-SQL bridge materialized into queryable schema
This file has been truncated, but you can view the full file.
-- Add the IANA time zones so we can reference them when doing DateTime and DateTimeOffset conversions
-- Data from NodaTime via Matt Johnson: https://github.com/mj1856/SqlServerTimeZoneSupport
-- I generated this script by:
-- 1. Running the NodaTime import program into a local database
-- 2. Exporting the schema creation AND data
-- 3. Massaging the SSMS output to make it idempotent
SET ANSI_NULLS ON
GO
@rianjs
rianjs / NodaTimeExamples.cs
Last active April 18, 2018 15:29
NodaTime bread and butter examples
// Create the WND time with a real time zone
var localDt = LocalDateTime.FromDateTime(DateTime.Now);
var wndTz = DateTimeZoneProviders.Tzdb["America/Toronto"];
var zonedAsWnd = localDt.InZoneLeniently(wndTz);
// Convert that WND time to RNO time
var renoTz = DateTimeZoneProviders.Tzdb["America/Los_Angeles"];
var inReno = zonedAsWnd.WithZone(renoTz);
// Convert a DateTimeOffset to WND time with a real time zone

Keybase proof

I hereby claim:

  • I am rianjs on github.
  • I am rianjs (https://keybase.io/rianjs) on keybase.
  • I have a public key ASCOyDcaIMio8HeHSPE8Mt83rZJsqVhcS5rzwU0SDkg06Ao

To claim this, I am signing this object:

#!/bin/sh git log --numstat | awk '/^Author: /{author=$0} /^[0-9]+\t[0-9]+/{n = $1 + $2; d[author] += n; t += n} END { for(a in d) { printf("%6d %6.3f%% %s\n", d[a], d[a] * 100 / t, a)}}' | sort -rn
@rianjs
rianjs / IWorker.cs
Created August 3, 2020 17:14
Loop services framework in C#
using System.Threading.Tasks;
namespace Scratch
{
public interface IWorker
{
Task DoWorkAsync();
}
}
@rianjs
rianjs / CompressionUtils.cs
Created October 11, 2021 13:15
Serializing to and from a C# POCO using System.Text.Json
using System;
using System.IO;
using System.IO.Compression;
using System.Text.Json;
namespace CodatIngestion
{
public static class CompressionUtils
{
public static byte[] ToJsonSerializedGzipBytes<T>(T value, JsonSerializerOptions jsonOpts)
@rianjs
rianjs / GetLogger.cs
Created October 11, 2021 13:16
GetLogger<T> using Microsoft.Extensions.Logging
private static ILogger<T> GetLogger<T>()
{
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("LoggingConsoleApp.Program", LogLevel.Debug)
.AddConsole();
});
@rianjs
rianjs / JoinStrings.cs
Created October 11, 2021 13:19
Joining strings while ignoring empty or null values
public static string JoinStrings(IEnumerable<string> args, string separator)
{
var needsSeparator = false;
var builder = new StringBuilder();
foreach (var arg in args)
{
if (string.IsNullOrWhiteSpace(arg))
{
continue;
}
@rianjs
rianjs / LazyTextDecompression.cs
Created October 11, 2021 13:21
Read a portion of a gzipped text file without decompressing the whole thing using GZipStream
var i = 0;
const int limit = 100_000;
using (var fStream = fs.FileOpenRead(editionsPath))
using (var gzipStream = new GZipStream(fStream, CompressionMode.Decompress))
using (var textReader = new StreamReader(gzipStream))
{
var line = await textReader.ReadLineAsync();
while (!string.IsNullOrWhiteSpace(line) && i < limit)
{
@rianjs
rianjs / HasPermission.cs
Created October 11, 2021 13:23
Creating semantic bitmasks using enums
public bool HasPermission(long userPermissions, Permission desiredAction)
{
if (userPermissions < 1)
{
return false;
}
var asLong = (long)desiredAction;
var hasPermission = userPermissions & asLong;