time.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. void normalize(struct timespec *t)
  19. {
  20. // TODO compute directly ?
  21. while(t->tv_nsec < 0)
  22. {
  23. t->tv_nsec += 1000000000;
  24. t->tv_sec -= 1;
  25. }
  26. while(t->tv_nsec >= 1000000000)
  27. {
  28. t->tv_nsec -= 1000000000;
  29. t->tv_sec += 1;
  30. }
  31. }
  32. struct timespec timeDiff(struct timespec a, struct timespec b)
  33. {
  34. struct timespec r;
  35. r.tv_sec = a.tv_sec - b.tv_sec;
  36. r.tv_nsec = a.tv_nsec - b.tv_nsec;
  37. normalize(&r);
  38. return r;
  39. }
  40. // f should be >= 0
  41. struct timespec timeFactor(struct timespec a, double f)
  42. {
  43. /* bogued code :
  44. struct timespec r;
  45. r.tv_sec = f*a.tv_sec;
  46. r.tv_nsec = f*a.tv_nsec;
  47. while(r.tv_nsec > 1000000000)
  48. {
  49. r.tv_nsec -= 1000000000;
  50. r.tv_sec += 1;
  51. }
  52. return r;
  53. */
  54. return a;
  55. }
  56. struct timespec begin;
  57. struct timespec tPause; // beginning of the pause
  58. int pause;
  59. void timeInitialize()
  60. {
  61. pause = 0;
  62. if(clock_gettime(CLOCK_REALTIME, &begin) < 0)
  63. {
  64. perror("clock_gettime()");
  65. exit(1);
  66. }
  67. }
  68. void timeShift(double rel)
  69. {
  70. begin.tv_nsec += rel*1000000000.;
  71. normalize(&begin);
  72. }
  73. struct timespec timeGetRelative()
  74. {
  75. if(pause)
  76. return tPause;
  77. struct timespec r;
  78. if(clock_gettime(CLOCK_REALTIME, &r) < 0)
  79. {
  80. perror("clock_gettime()");
  81. exit(1);
  82. }
  83. return timeDiff(r, begin);
  84. }
  85. struct timespec timeCreate(time_t s, long ns)
  86. {
  87. struct timespec r;
  88. r.tv_sec = s;
  89. r.tv_nsec = ns;
  90. return r;
  91. }
  92. int timeInFuture(struct timespec t)
  93. {
  94. struct timespec tmp = timeGetRelative();
  95. tmp = timeDiff(t, tmp);
  96. return tmp.tv_sec >= 0;
  97. }
  98. void timePause(int b)
  99. {
  100. if(b)
  101. {
  102. tPause = timeGetRelative();
  103. pause = 1;
  104. }
  105. else
  106. {
  107. pause = 0;
  108. begin = timeDiff(begin, timeDiff(tPause, timeGetRelative()));
  109. }
  110. }
  111. int timeIsPaused()
  112. {
  113. return pause;
  114. }