PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

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