Torque3D Documentation / _generateds / x86UNIXTime.cpp

x86UNIXTime.cpp

Engine/source/platformX86UNIX/x86UNIXTime.cpp

More...

Public Functions

Detailed Description

Public Variables

bool sg_initialized 
U32 sg_secsOffset 
U32 sgCurrentTime 

Public Functions

x86UNIXGetTickCount()

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platformX86UNIX/platformX86UNIX.h"
 25#include "platform/platformTimer.h"
 26#include "time.h"
 27#include <errno.h>
 28#include <time.h>
 29#include <sys/time.h>
 30#include <sys/resource.h>
 31#include <unistd.h>
 32
 33static U32 sgCurrentTime = 0;
 34
 35U32 x86UNIXGetTickCount();
 36
 37//--------------------------------------
 38void Platform::getLocalTime(LocalTime &lt)
 39{
 40   struct tm *systime;
 41   time_t long_time;
 42
 43   time( &long_time );                // Get time as long integer.
 44   systime = localtime( &long_time ); // Convert to local time.
 45   
 46   lt.sec      = systime->tm_sec;      
 47   lt.min      = systime->tm_min;      
 48   lt.hour     = systime->tm_hour;     
 49   lt.month    = systime->tm_mon;    
 50   lt.monthday = systime->tm_mday; 
 51   lt.weekday  = systime->tm_wday;  
 52   lt.year     = systime->tm_year;     
 53   lt.yearday  = systime->tm_yday;  
 54   lt.isdst    = systime->tm_isdst;    
 55}  
 56
 57String Platform::localTimeToString( const LocalTime &lt )
 58{
 59   tm systime;
 60   
 61   systime.tm_sec    = lt.sec;
 62   systime.tm_min    = lt.min;
 63   systime.tm_hour   = lt.hour;
 64   systime.tm_mon    = lt.month;
 65   systime.tm_mday   = lt.monthday;
 66   systime.tm_wday   = lt.weekday;
 67   systime.tm_year   = lt.year;
 68   systime.tm_yday   = lt.yearday;
 69   systime.tm_isdst  = lt.isdst;
 70
 71   return asctime( &systime );
 72}
 73
 74U32 Platform::getTime()
 75{
 76   time_t long_time;
 77   time( &long_time );
 78   return long_time;
 79}   
 80
 81U32 Platform::getRealMilliseconds()
 82{
 83//   struct rusage usageStats;
 84//   getrusage(RUSAGE_SELF, &usageStats);
 85//   return usageStats.ru_utime.tv_usec;
 86   return x86UNIXGetTickCount();
 87}
 88
 89U32 Platform::getVirtualMilliseconds()
 90{
 91   return sgCurrentTime;
 92}   
 93
 94void Platform::advanceTime(U32 delta)
 95{
 96   sgCurrentTime += delta;
 97}   
 98
 99void Platform::fileToLocalTime(const FileTime & ft, LocalTime * lt)
100{
101   if(!lt)
102      return;
103      
104   time_t long_time = ft;
105
106   struct tm systime;
107
108   /// Convert to local time, thread safe.
109   localtime_r( &long_time, &systime );
110   
111   /// Fill the return struct
112   lt->sec      = systime.tm_sec;
113   lt->min      = systime.tm_min;
114   lt->hour     = systime.tm_hour;
115   lt->month    = systime.tm_mon;
116   lt->monthday = systime.tm_mday;
117   lt->weekday  = systime.tm_wday;
118   lt->year     = systime.tm_year;
119   lt->yearday  = systime.tm_yday;
120   lt->isdst    = systime.tm_isdst;
121}
122
123PlatformTimer *PlatformTimer::create()
124{
125   return new DefaultPlatformTimer();
126}
127//------------------------------------------------------------------------------
128//-------------------------------------- x86UNIX Implementation
129//
130//
131static bool   sg_initialized = false;
132static U32 sg_secsOffset  = 0;
133//--------------------------------------
134U32 x86UNIXGetTickCount()
135{
136   // TODO: What happens when crossing a day boundary?
137   //
138   timeval t;
139
140   if (sg_initialized == false) {
141      sg_initialized = true;
142
143      gettimeofday(&t, NULL);
144      sg_secsOffset = t.tv_sec;
145   }
146
147   gettimeofday(&t, NULL);
148
149   U32 secs  = t.tv_sec - sg_secsOffset;
150   U32 uSecs = t.tv_usec;
151
152   // Make granularity 1 ms
153   return (secs * 1000) + (uSecs / 1000);
154}
155
156
157void Platform::sleep(U32 ms)
158{
159   // note: this will overflow if you want to sleep for more than 49 days. just so ye know.
160   usleep( ms * 1000 );
161}
162       
163