Merge pull request #4676 from jmarshallnz/dont_set_scraper_on_tvshow_on_nfo
[vuplus_xbmc] / xbmc / utils / TimeUtils.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "TimeUtils.h"
22 #include "XBDateTime.h"
23 #include "threads/SystemClock.h"
24
25 #if   defined(TARGET_DARWIN)
26 #include <mach/mach_time.h>
27 #include <CoreVideo/CVHostTime.h>
28 #elif defined(TARGET_WINDOWS)
29 #include <windows.h>
30 #else
31 #include <time.h>
32 #endif
33
34 #include "TimeSmoother.h"
35
36 int64_t CurrentHostCounter(void)
37 {
38 #if   defined(TARGET_DARWIN)
39   return( (int64_t)CVGetCurrentHostTime() );
40 #elif defined(TARGET_WINDOWS)
41   LARGE_INTEGER PerformanceCount;
42   QueryPerformanceCounter(&PerformanceCount);
43   return( (int64_t)PerformanceCount.QuadPart );
44 #else
45   struct timespec now;
46   clock_gettime(CLOCK_MONOTONIC, &now);
47   return( ((int64_t)now.tv_sec * 1000000000L) + now.tv_nsec );
48 #endif
49 }
50
51 int64_t CurrentHostFrequency(void)
52 {
53 #if defined(TARGET_DARWIN)
54   return( (int64_t)CVGetHostClockFrequency() );
55 #elif defined(TARGET_WINDOWS)
56   LARGE_INTEGER Frequency;
57   QueryPerformanceFrequency(&Frequency);
58   return( (int64_t)Frequency.QuadPart );
59 #else
60   return( (int64_t)1000000000L );
61 #endif
62 }
63
64 CTimeSmoother *CTimeUtils::frameTimer = NULL;
65 unsigned int CTimeUtils::frameTime = 0;
66
67 void CTimeUtils::UpdateFrameTime(bool flip)
68 {
69   if (!frameTimer)
70     frameTimer = new CTimeSmoother();
71   unsigned int currentTime = XbmcThreads::SystemClockMillis();
72   if (flip)
73     frameTimer->AddTimeStamp(currentTime);
74   frameTime = frameTimer->GetNextFrameTime(currentTime);
75 }
76
77 unsigned int CTimeUtils::GetFrameTime()
78 {
79   return frameTime;
80 }
81
82 CDateTime CTimeUtils::GetLocalTime(time_t time)
83 {
84   CDateTime result;
85
86   tm *local = localtime(&time); // Conversion to local time
87   /*
88    * Microsoft implementation of localtime returns NULL if on or before epoch.
89    * http://msdn.microsoft.com/en-us/library/bf12f0hc(VS.80).aspx
90    */
91   if (local)
92     result = *local;
93   else
94     result = time; // Use the original time as close enough.
95
96   return result;
97 }