x86UNIXTime.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "platformX86UNIX/x86UNIXState.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. U32 x86UNIXGetTickCount();
  31. //--------------------------------------
  32. void Platform::getLocalTime(LocalTime &lt)
  33. {
  34. struct tm *systime;
  35. time_t long_time;
  36. time( &long_time ); // Get time as long integer.
  37. systime = localtime( &long_time ); // Convert to local time.
  38. lt.sec = systime->tm_sec;
  39. lt.min = systime->tm_min;
  40. lt.hour = systime->tm_hour;
  41. lt.month = systime->tm_mon;
  42. lt.monthday = systime->tm_mday;
  43. lt.weekday = systime->tm_wday;
  44. lt.year = systime->tm_year;
  45. lt.yearday = systime->tm_yday;
  46. lt.isdst = systime->tm_isdst;
  47. }
  48. U32 Platform::getTime()
  49. {
  50. time_t long_time;
  51. time( &long_time );
  52. return long_time;
  53. }
  54. U32 Platform::getRealMilliseconds()
  55. {
  56. // struct rusage usageStats;
  57. // getrusage(RUSAGE_SELF, &usageStats);
  58. // return usageStats.ru_utime.tv_usec;
  59. return x86UNIXGetTickCount();
  60. }
  61. U32 Platform::getVirtualMilliseconds()
  62. {
  63. return x86UNIXState->currentTime;
  64. }
  65. void Platform::advanceTime(U32 delta)
  66. {
  67. x86UNIXState->currentTime += delta;
  68. }
  69. //------------------------------------------------------------------------------
  70. //-------------------------------------- x86UNIX Implementation
  71. //
  72. //
  73. static bool sg_initialized = false;
  74. static U32 sg_secsOffset = 0;
  75. //--------------------------------------
  76. U32 x86UNIXGetTickCount()
  77. {
  78. // TODO: What happens when crossing a day boundary?
  79. //
  80. timeval t;
  81. if (sg_initialized == false) {
  82. sg_initialized = true;
  83. gettimeofday(&t, NULL);
  84. sg_secsOffset = t.tv_sec;
  85. }
  86. gettimeofday(&t, NULL);
  87. U32 secs = t.tv_sec - sg_secsOffset;
  88. U32 uSecs = t.tv_usec;
  89. // Make granularity 1 ms
  90. return (secs * 1000) + (uSecs / 1000);
  91. }
  92. void Platform::sleep(U32 ms)
  93. {
  94. // note: this will overflow if you want to sleep for more than 49 days. just so ye know.
  95. usleep( ms * 1000 );
  96. }