time.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright (C) 2014 Olivier Marty <olivier.marty.m at gmail.com>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. */
  15. #include "time.h"
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. int timeSleep(struct timespec t)
  19. {
  20. int r = -2;
  21. if(t.tv_sec >= 0)
  22. {
  23. do
  24. {
  25. r = nanosleep(&t, &t);
  26. } while(errno == EINTR);
  27. }
  28. return r;
  29. }
  30. struct timespec timeDiff(struct timespec a, struct timespec b)
  31. {
  32. struct timespec r;
  33. r.tv_sec = a.tv_sec - b.tv_sec;
  34. r.tv_nsec = a.tv_nsec - b.tv_nsec;
  35. if(r.tv_nsec < 0)
  36. {
  37. r.tv_nsec += 1000000000;
  38. r.tv_sec -= 1;
  39. }
  40. return r;
  41. }
  42. // f should be >= 0
  43. struct timespec timeFactor(struct timespec a, double f)
  44. {
  45. /* bogued code :
  46. struct timespec r;
  47. r.tv_sec = f*a.tv_sec;
  48. r.tv_nsec = f*a.tv_nsec;
  49. while(r.tv_nsec > 1000000000)
  50. {
  51. r.tv_nsec -= 1000000000;
  52. r.tv_sec += 1;
  53. }
  54. return r;
  55. */
  56. return a;
  57. }
  58. struct timespec begin;
  59. struct timespec tPause; // beginning of the pause
  60. int pause;
  61. void timeInitialize(int rel)
  62. {
  63. pause = 0;
  64. if(clock_gettime(CLOCK_REALTIME, &begin) < 0)
  65. {
  66. perror("clock_gettime()");
  67. exit(1);
  68. }
  69. begin.tv_sec += rel;
  70. }
  71. struct timespec timeGetRelative()
  72. {
  73. if(pause)
  74. return tPause;
  75. struct timespec r;
  76. if(clock_gettime(CLOCK_REALTIME, &r) < 0)
  77. {
  78. perror("clock_gettime()");
  79. exit(1);
  80. }
  81. return timeDiff(r, begin);
  82. }
  83. int timeSleepUntil(struct timespec t)
  84. {
  85. struct timespec current = timeGetRelative();
  86. return timeSleep(timeDiff(t, current));
  87. }
  88. struct timespec timeCreate(time_t s, long ns)
  89. {
  90. struct timespec r;
  91. r.tv_sec = s;
  92. r.tv_nsec = ns;
  93. return r;
  94. }
  95. int timeInFuture(struct timespec t)
  96. {
  97. struct timespec tmp = timeGetRelative();
  98. tmp = timeDiff(t, tmp);
  99. return tmp.tv_sec >= 0;
  100. }
  101. void timePause(int b)
  102. {
  103. if(b)
  104. {
  105. tPause = timeGetRelative();
  106. pause = 1;
  107. }
  108. else
  109. {
  110. pause = 0;
  111. begin = timeDiff(begin, timeDiff(tPause, timeGetRelative()));
  112. }
  113. }
  114. int timeIsPaused()
  115. {
  116. return pause;
  117. }