time.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. struct timespec r;
  45. r.tv_sec = a.tv_sec;
  46. r.tv_nsec = f*a.tv_nsec + (f-1.)*a.tv_sec * 1000000000;
  47. normalize(&r);
  48. return r;
  49. }
  50. struct timespec begin;
  51. struct timespec tPause; // beginning of the pause
  52. int pause;
  53. void timeInitialize()
  54. {
  55. pause = 0;
  56. if(clock_gettime(CLOCK_REALTIME, &begin) < 0)
  57. {
  58. perror("clock_gettime()");
  59. exit(1);
  60. }
  61. }
  62. void timeShift(double rel)
  63. {
  64. begin.tv_nsec += rel*1000000000.;
  65. normalize(&begin);
  66. }
  67. struct timespec timeGetRelative()
  68. {
  69. if(pause)
  70. return tPause;
  71. struct timespec r;
  72. if(clock_gettime(CLOCK_REALTIME, &r) < 0)
  73. {
  74. perror("clock_gettime()");
  75. exit(1);
  76. }
  77. return timeDiff(r, begin);
  78. }
  79. struct timespec timeCreate(time_t s, long ns)
  80. {
  81. struct timespec r;
  82. r.tv_sec = s;
  83. r.tv_nsec = ns;
  84. normalize(&r);
  85. return r;
  86. }
  87. int timeInFuture(struct timespec t)
  88. {
  89. struct timespec tmp = timeGetRelative();
  90. tmp = timeDiff(t, tmp);
  91. return tmp.tv_sec >= 0;
  92. }
  93. void timePause(int b)
  94. {
  95. if(b)
  96. {
  97. tPause = timeGetRelative();
  98. pause = 1;
  99. }
  100. else
  101. {
  102. pause = 0;
  103. begin = timeDiff(begin, timeDiff(tPause, timeGetRelative()));
  104. }
  105. }
  106. int timeIsPaused()
  107. {
  108. return pause;
  109. }