pim_time.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. PIM for Quagga
  3. Copyright (C) 2008 Everton da Silva Marques
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING; if not, write to the
  14. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  15. MA 02110-1301 USA
  16. $QuaggaId: $Format:%an, %ai, %h$ $
  17. */
  18. #include <zebra.h>
  19. #include <string.h>
  20. #include <sys/time.h>
  21. #include <time.h>
  22. #include "log.h"
  23. #include "thread.h"
  24. #include "pim_time.h"
  25. static int gettime_monotonic(struct timeval *tv)
  26. {
  27. int result;
  28. result = gettimeofday(tv, 0);
  29. if (result) {
  30. zlog_err("%s: gettimeofday() failure: errno=%d: %s",
  31. __PRETTY_FUNCTION__,
  32. errno, safe_strerror(errno));
  33. }
  34. return result;
  35. }
  36. /*
  37. pim_time_monotonic_sec():
  38. number of seconds since some unspecified starting point
  39. */
  40. int64_t pim_time_monotonic_sec()
  41. {
  42. struct timeval now_tv;
  43. if (gettime_monotonic(&now_tv)) {
  44. zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
  45. __PRETTY_FUNCTION__,
  46. errno, safe_strerror(errno));
  47. return -1;
  48. }
  49. return now_tv.tv_sec;
  50. }
  51. /*
  52. pim_time_monotonic_dsec():
  53. number of deciseconds since some unspecified starting point
  54. */
  55. int64_t pim_time_monotonic_dsec()
  56. {
  57. struct timeval now_tv;
  58. int64_t now_dsec;
  59. if (gettime_monotonic(&now_tv)) {
  60. zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
  61. __PRETTY_FUNCTION__,
  62. errno, safe_strerror(errno));
  63. return -1;
  64. }
  65. now_dsec = ((int64_t) now_tv.tv_sec) * 10 + ((int64_t) now_tv.tv_usec) / 100000;
  66. return now_dsec;
  67. }
  68. int pim_time_mmss(char *buf, int buf_size, long sec)
  69. {
  70. long mm;
  71. int wr;
  72. zassert(buf_size >= 5);
  73. mm = sec / 60;
  74. sec %= 60;
  75. wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec);
  76. return wr != 8;
  77. }
  78. static int pim_time_hhmmss(char *buf, int buf_size, long sec)
  79. {
  80. long hh;
  81. long mm;
  82. int wr;
  83. zassert(buf_size >= 8);
  84. hh = sec / 3600;
  85. sec %= 3600;
  86. mm = sec / 60;
  87. sec %= 60;
  88. wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec);
  89. return wr != 8;
  90. }
  91. void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer)
  92. {
  93. if (t_timer) {
  94. pim_time_mmss(buf, buf_size,
  95. thread_timer_remain_second(t_timer));
  96. }
  97. else {
  98. snprintf(buf, buf_size, "--:--");
  99. }
  100. }
  101. void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer)
  102. {
  103. if (t_timer) {
  104. pim_time_hhmmss(buf, buf_size,
  105. thread_timer_remain_second(t_timer));
  106. }
  107. else {
  108. snprintf(buf, buf_size, "--:--:--");
  109. }
  110. }
  111. void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec)
  112. {
  113. zassert(buf_size >= 8);
  114. pim_time_hhmmss(buf, buf_size, uptime_sec);
  115. }
  116. void pim_time_uptime_begin(char *buf, int buf_size, int64_t now, int64_t begin)
  117. {
  118. if (begin > 0)
  119. pim_time_uptime(buf, buf_size, now - begin);
  120. else
  121. snprintf(buf, buf_size, "--:--:--");
  122. }
  123. long pim_time_timer_remain_msec(struct thread *t_timer)
  124. {
  125. /* FIXME: Actually fetch msec resolution from thread */
  126. /* no timer thread running means timer has expired: return 0 */
  127. return t_timer ?
  128. 1000 * thread_timer_remain_second(t_timer) :
  129. 0;
  130. }