Skip to content

Instantly share code, notes, and snippets.

@svick
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save svick/c9b84c7b807e1bf0a8e5 to your computer and use it in GitHub Desktop.

Select an option

Save svick/c9b84c7b807e1bf0a8e5 to your computer and use it in GitHub Desktop.
C# 6.0 simple time units
using System;
using TimeUnits;
public static class Program
{
public void Main()
{
Console.WriteLine(70.5 * s);
}
}
// TODO: what does NodaTime do?
public static class TimeUnits
{
public static TimeUnit ms { get; } = new TimeUnit(TimeSpan.FromMilliseconds(1));
public static TimeUnit s { get; } = new TimeUnit(TimeSpan.FromSeconds(1));
public static TimeUnit min { get; } = new TimeUnit(TimeSpan.FromMinutes(1));
public static TimeUnit h { get; } = new TimeUnit(TimeSpan.FromHours(1));
public static TimeUnit d { get; } = new TimeUnit(TimeSpan.FromDays(1));
}
public class TimeUnit
{
public TimeUnit(TimeSpan value)
{
Value = value;
}
public TimeSpan Value { get; private set; }
public static TimeSpan operator *(int amount, TimeUnit unit)
{
return new TimeSpan(amount * unit.Value.Ticks);
}
public static TimeSpan operator *(double amount, TimeUnit unit)
{
return TimeSpan.FromMilliseconds(amount * unit.Value.TotalMilliseconds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment