time.c 2.5 KB

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