Last active
March 20, 2018 19:54
-
-
Save mjs3339/2e0f561db89e31739774b1d649e9c0fa to your computer and use it in GitHub Desktop.
C# Tick Time Date Constants and Conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Serializable] | |
public class TicksToTime | |
{ | |
////--------------Constants--------------- | |
public const double TicksPerMicrosecond = 10; | |
private const double MicrosecondsPerTick = 1.0 / TicksPerMicrosecond; | |
public const long TicksPerMillisecond = 10000; | |
private const double MillisecondsPerTick = 1.0 / TicksPerMillisecond; | |
public const long TicksPerSecond = TicksPerMillisecond * 1000; | |
private const double SecondsPerTick = 1.0 / TicksPerSecond; | |
public const long TicksPerMinute = TicksPerSecond * 60; | |
private const double MinutesPerTick = 1.0 / TicksPerMinute; | |
public const long TicksPerHour = TicksPerMinute * 60; | |
private const double HoursPerTick = 1.0 / TicksPerHour; | |
public const long TicksPerDay = TicksPerHour * 24; | |
private const double DaysPerTick = 1.0 / TicksPerDay; | |
public const int MillisPerSecond = 1000; | |
public const int MillisPerMinute = MillisPerSecond * 60; | |
public const int MillisPerHour = MillisPerMinute * 60; | |
public const int MillisPerDay = MillisPerHour * 24; | |
private const long MaxMilliSeconds = long.MaxValue / TicksPerMillisecond; | |
private const long MinMilliSeconds = long.MinValue / TicksPerMillisecond; | |
public const long MicrosPerSecond = 1000000; | |
public const long MicrosPerMinute = MicrosPerSecond * 60; | |
public const long MicrosPerHour = MicrosPerMinute * 60; | |
public const long MicrosPerDay = MicrosPerHour * 24; | |
public static readonly TicksToTime MaxValue = new TicksToTime(long.MaxValue); | |
public static readonly TicksToTime MinValue = new TicksToTime(long.MinValue); | |
////--------------Constructor--------------- | |
public TicksToTime(long ticks) | |
{ | |
Ticks = ticks; | |
} | |
////--------------Individual Time Components--------------- | |
public long Ticks { get; } | |
public int Days => (int) (Ticks / TicksPerDay); | |
public int Hours => (int) (Ticks / TicksPerHour % 24); | |
public int Milliseconds => (int) (Ticks / TicksPerMillisecond % 1000); | |
public int Microseconds => (int) (Ticks / TicksPerMicrosecond % 1000); | |
public int Minutes => (int) (Ticks / TicksPerMinute % 60); | |
public int Seconds => (int) (Ticks / TicksPerSecond % 60); | |
////--------------Total Times(Elapsed)--------------- | |
public double TotalDays => Ticks * DaysPerTick; | |
public double TotalHours => Ticks * HoursPerTick; | |
public double TotalMinutes => Ticks * MinutesPerTick; | |
public double TotalSeconds => Ticks * SecondsPerTick; | |
public double TotalMilliseconds => Ticks * MillisecondsPerTick; | |
public double TotalMicroseconds => Ticks * MicrosecondsPerTick; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment