2
0

sip-date.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@page sip-date Print or parse SIP date
  25. *
  26. * @section synopsis Synopsis
  27. *
  28. * <tt>sip-date [-n] [SIP-date | [YYYYy] [DDd] [HHh] [MMm] [SS[s]]]</tt>
  29. *
  30. * @section description Description
  31. *
  32. * @em sip-date is an utility for printing a SIP date (in the format
  33. * specified by RFC 1123, but the timezone must always be GMT) or parsing a
  34. * given SIP date. The date can be given as a SIP date or by giving year,
  35. * day, hour, minutes and seconds separately.
  36. *
  37. * @section options Options
  38. *
  39. * The @em sip-date utility takes options as follows:
  40. * <dl>
  41. * <dt>-n</dt>
  42. * <dd>The @em sip-date utility prints the date as seconds elapsed since
  43. * epoch (01 Jan 1900 00:00:00).
  44. * </dd>
  45. * </dl>
  46. *
  47. * @section examples Examples
  48. *
  49. * You want to convert current time to SIP date:
  50. * @code
  51. * $ sip-date
  52. * @endcode
  53. * You want to find out how many seconds there was in 1900's:
  54. * @code
  55. * $ siptime -n 2000y
  56. * 3155673600
  57. * @endcode
  58. *
  59. * @section bugs Reporting Bugs
  60. * Report bugs to <sofia-sip-devel@lists.sourceforge.net>.
  61. *
  62. * @section author Author
  63. * Pekka Pessi <Pekka -dot- Pessi -at- nokia -dot- com>
  64. *
  65. * @section copyright Copyright
  66. * Copyright (C) 2005 Nokia Corporation.
  67. *
  68. * This program is free software; see the source for copying conditions.
  69. * There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
  70. * PARTICULAR PURPOSE.
  71. */
  72. #include "config.h"
  73. #include <stdio.h>
  74. #include <string.h>
  75. #include <stddef.h>
  76. #include <stdlib.h>
  77. #include <sofia-sip/sip.h>
  78. #include <sofia-sip/sip_header.h>
  79. #include <sofia-sip/msg_date.h>
  80. void usage(void)
  81. {
  82. fprintf(stderr,
  83. "usage: sip-date [-n] "
  84. "[SIP-date | [YYYYy] [DDd] [HHh] [MMm] [SS[s]]]\n");
  85. exit(1);
  86. }
  87. /* Epoch year. */
  88. #define EPOCH 1900
  89. /* Day number of New Year Day of given year */
  90. #define YEAR_DAYS(y) \
  91. (((y)-1) * 365 + ((y)-1) / 4 - ((y)-1) / 100 + ((y)-1) / 400)
  92. int main(int ac, char *av[])
  93. {
  94. int numeric = 0;
  95. sip_time_t t = 0, t2 = 0;
  96. char const *s;
  97. char buf[1024];
  98. if (av[1] && strcmp(av[1], "-n") == 0)
  99. av++, numeric = 1;
  100. if (av[1] && (strcmp(av[1], "-?") == 0 || strcmp(av[1], "-h") == 0))
  101. usage();
  102. if ((s = av[1])) {
  103. size_t n, m;
  104. /* Concatenate all arguments with a space in between them */
  105. for (n = 0; (s = av[1]); av++) {
  106. m = strlen(s);
  107. if (n + m + 2 > sizeof(buf))
  108. exit(1);
  109. memcpy(buf + n, s, m);
  110. buf[n + m] = ' ';
  111. n += m + 1;
  112. }
  113. buf[n] = '\0';
  114. s = buf;
  115. if (s[0] < '0' || s[0] > '9') {
  116. if (msg_date_d(&s, &t) < 0) {
  117. fprintf(stderr, "sip-date: %s is not valid time\n", s);
  118. exit(1);
  119. }
  120. }
  121. else {
  122. for (; *s; ) {
  123. if (msg_delta_d(&s, &t2) < 0)
  124. usage();
  125. switch (*s) {
  126. case 'y': t2 = YEAR_DAYS(t2) - YEAR_DAYS(EPOCH);
  127. /*FALLTHROUGH*/
  128. case 'd': t += t2 * 24 * 60 * 60; s++; break;
  129. case 'h': t += t2 * 60 * 60; s++; break;
  130. case 'm': t += t2 * 60; s++; break;
  131. case 's':
  132. s++;
  133. default:
  134. t += t2;
  135. break;
  136. }
  137. while (*s && *s == ' ')
  138. s++;
  139. }
  140. }
  141. }
  142. else {
  143. t = sip_now();
  144. }
  145. if (numeric) {
  146. msg_delta_e(buf, sizeof(buf), t);
  147. }
  148. else {
  149. msg_date_e(buf, sizeof(buf), t);
  150. }
  151. puts(buf);
  152. return 0;
  153. }