34 lines
1.2 KiB
C#
34 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace mbts
|
|
{
|
|
static class APIExtensions
|
|
{
|
|
public static T? TryFirst<T>(this IEnumerable<T> src) where T : struct
|
|
{
|
|
foreach (var v in src)
|
|
return v;
|
|
return null;
|
|
}
|
|
|
|
public static DateTime lldatetime(this ulong ll)
|
|
{
|
|
// Warning: Do not remove these consts, or try to simplify this method.
|
|
// System.DateTime() requires bits 63-64 to be empty in the "DateTime(long ticks)" constructor.
|
|
// The only way to set the DateTimeKind is through the "DateTime(long ticks, DateTimeKind kind)" constructor.
|
|
// (Yes, it is annoying. Internally, System.DateTime stores the DateTimeKind in bits 63-64 - in the exact format we already have!!!)
|
|
const ulong ticks_mask = 0x3FFFFFFFFFFFFFFF;
|
|
const int kind_shift = 62;
|
|
return new DateTime((long)(ll & ticks_mask), (DateTimeKind)(ll >> kind_shift));
|
|
}
|
|
|
|
public static string fmt(this string input, params object[] vals)
|
|
{
|
|
return string.Format(input, vals);
|
|
}
|
|
|
|
public static void ignore(this Task t) { }
|
|
}
|
|
} |