Advertisement
bobmarley12345

c# system time/tick helper

Feb 2nd, 2022
1,552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 KB | None | 0 0
  1. using System.Diagnostics;
  2.  
  3. namespace REghZyGuiGL.Utils {
  4.     public class Time {
  5.         /// <summary>
  6.         /// This specifies how many ticks there are in 1 second. This usually never changes during
  7.         /// the app's runtime. Though, it might change for different operating systems
  8.         /// <para>
  9.         /// If one were to call <see cref="GetSystemTicks"/>, then <see cref="System.Threading.Thread.Sleep(int)"/>
  10.         /// for 1000ms, then <see cref="GetSystemTicks"/>, the interval will roughly equal to this field's value
  11.         /// </para>
  12.         /// </summary>
  13.         public static readonly long TICK_PER_SECOND = Stopwatch.Frequency;      // windows = 10,000,000
  14.  
  15.         public static readonly double TICK_PER_SECOND_D = Stopwatch.Frequency;  // windows = 10000000.0d
  16.  
  17.         /// <summary>
  18.         /// A multiplier for converting ticks to milliseconds
  19.         /// <para>
  20.         /// If one were to call <see cref="GetSystemMillis"/>, then <see cref="System.Threading.Thread.Sleep(int)"/>
  21.         /// for 1000ms, then <see cref="GetSystemMillis"/>, the interval will roughly equal to 1,000
  22.         /// </para>
  23.         /// </summary>
  24.         public static readonly long TICK_PER_MILLIS = TICK_PER_SECOND / 1000;   // windows = 10,000
  25.  
  26.         /// <summary>
  27.         /// A multiplier for converting ticks to milliseconds
  28.         /// <para>
  29.         /// If one were to call <see cref="GetSystemNanos"/>, then <see cref="System.Threading.Thread.Sleep(int)"/>
  30.         /// for 1000ms, then <see cref="GetSystemNanos"/>, the interval will roughly equal to 1,000,000
  31.         /// </para>
  32.         /// </summary>
  33.         public static readonly long TICK_PER_NANOS = TICK_PER_SECOND / 1000000; // windows = 10
  34.  
  35.         /// <summary>
  36.         /// Gets the system's performance counter ticks
  37.         /// </summary>
  38.         public static long GetSystemTicks() {
  39.             return Stopwatch.GetTimestamp();
  40.         }
  41.  
  42.         /// <summary>
  43.         /// Gets the system's performance counter ticks and converts them to milliseconds
  44.         /// </summary>
  45.         public static long GetSystemNanos() {
  46.             return Stopwatch.GetTimestamp() / TICK_PER_NANOS;
  47.         }
  48.  
  49.         /// <summary>
  50.         /// Gets the system's performance counter ticks and converts them to milliseconds
  51.         /// </summary>
  52.         public static long GetSystemMillis() {
  53.             return Stopwatch.GetTimestamp() / TICK_PER_MILLIS;
  54.         }
  55.  
  56.         /// <summary>
  57.         /// Gets the system's performance counter ticks and converts them to milliseconds
  58.         /// </summary>
  59.         /// <returns></returns>
  60.         public static long GetSystemSeconds() {
  61.             return Stopwatch.GetTimestamp() / TICK_PER_SECOND;
  62.         }
  63.  
  64.         /// <summary>
  65.         /// Gets the system's performance counter ticks and converts them to milliseconds
  66.         /// </summary>
  67.         /// <returns></returns>
  68.         public static double GetSystemSecondsD() {
  69.             return (double) Stopwatch.GetTimestamp() / TICK_PER_SECOND_D;
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement