x86UNIXTime.cpp 4.4 KB

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