CurrentTimeGetter.cs 687 B

12345678910111213141516171819202122232425262728
  1. namespace System;
  2. public class CurrentTimeGetter
  3. {
  4. private static int lastTicks = -1;
  5. private static DateTime lastDateTime = DateTime.MinValue;
  6. /// <summary>
  7. /// Gets the current time in an optimized fashion.
  8. /// </summary>
  9. /// <value>Current time.</value>
  10. public static DateTime UtcNow
  11. {
  12. get
  13. {
  14. int tickCount = Environment.TickCount;
  15. if (tickCount == lastTicks)
  16. {
  17. return lastDateTime;
  18. }
  19. DateTime dt = DateTime.UtcNow;
  20. lastTicks = tickCount;
  21. lastDateTime = dt;
  22. return dt;
  23. }
  24. }
  25. }