/xbmc/utils/TimeUtils.cpp

http://github.com/xbmc/xbmc · C++ · 101 lines · 78 code · 11 blank · 12 comment · 4 complexity · a2cd10ad2009bd2188e88df4baf1f717 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2018 Team Kodi
  3. * This file is part of Kodi - https://kodi.tv
  4. *
  5. * SPDX-License-Identifier: GPL-2.0-or-later
  6. * See LICENSES/README.md for more information.
  7. */
  8. #include "TimeUtils.h"
  9. #include "XBDateTime.h"
  10. #include "threads/SystemClock.h"
  11. #include "windowing/GraphicContext.h"
  12. #if defined(TARGET_DARWIN)
  13. #include <mach/mach_time.h>
  14. #include <CoreVideo/CVHostTime.h>
  15. #elif defined(TARGET_WINDOWS)
  16. #include <windows.h>
  17. #else
  18. #include <time.h>
  19. #endif
  20. int64_t CurrentHostCounter(void)
  21. {
  22. #if defined(TARGET_DARWIN)
  23. return( (int64_t)CVGetCurrentHostTime() );
  24. #elif defined(TARGET_WINDOWS)
  25. LARGE_INTEGER PerformanceCount;
  26. QueryPerformanceCounter(&PerformanceCount);
  27. return( (int64_t)PerformanceCount.QuadPart );
  28. #else
  29. struct timespec now;
  30. #if defined(CLOCK_MONOTONIC_RAW) && !defined(TARGET_ANDROID)
  31. clock_gettime(CLOCK_MONOTONIC_RAW, &now);
  32. #else
  33. clock_gettime(CLOCK_MONOTONIC, &now);
  34. #endif // CLOCK_MONOTONIC_RAW && !TARGET_ANDROID
  35. return( ((int64_t)now.tv_sec * 1000000000L) + now.tv_nsec );
  36. #endif
  37. }
  38. int64_t CurrentHostFrequency(void)
  39. {
  40. #if defined(TARGET_DARWIN)
  41. return( (int64_t)CVGetHostClockFrequency() );
  42. #elif defined(TARGET_WINDOWS)
  43. LARGE_INTEGER Frequency;
  44. QueryPerformanceFrequency(&Frequency);
  45. return( (int64_t)Frequency.QuadPart );
  46. #else
  47. return( (int64_t)1000000000L );
  48. #endif
  49. }
  50. unsigned int CTimeUtils::frameTime = 0;
  51. void CTimeUtils::UpdateFrameTime(bool flip)
  52. {
  53. unsigned int currentTime = XbmcThreads::SystemClockMillis();
  54. unsigned int last = frameTime;
  55. while (frameTime < currentTime)
  56. {
  57. frameTime += (unsigned int)(1000 / CServiceBroker::GetWinSystem()->GetGfxContext().GetFPS());
  58. // observe wrap around
  59. if (frameTime < last)
  60. break;
  61. }
  62. }
  63. unsigned int CTimeUtils::GetFrameTime()
  64. {
  65. return frameTime;
  66. }
  67. CDateTime CTimeUtils::GetLocalTime(time_t time)
  68. {
  69. CDateTime result;
  70. tm *local;
  71. #ifdef HAVE_LOCALTIME_R
  72. tm res = {};
  73. local = localtime_r(&time, &res); // Conversion to local time
  74. #else
  75. local = localtime(&time); // Conversion to local time
  76. #endif
  77. /*
  78. * Microsoft implementation of localtime returns NULL if on or before epoch.
  79. * http://msdn.microsoft.com/en-us/library/bf12f0hc(VS.80).aspx
  80. */
  81. if (local)
  82. result = *local;
  83. else
  84. result = time; // Use the original time as close enough.
  85. return result;
  86. }
  87. std::string CTimeUtils::WithoutSeconds(const std::string hhmmss)
  88. {
  89. return hhmmss.substr(0, 5);
  90. }