test_date.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /**@internal
  25. *
  26. * @CFILE test_date.c
  27. *
  28. * Tester for SIP date parser
  29. *
  30. * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
  31. *
  32. * @date Wed Mar 21 19:12:13 2001 ppessi
  33. */
  34. #include "config.h"
  35. #include <stdio.h>
  36. #include <sofia-sip/su_string.h>
  37. #include <stddef.h>
  38. #include <stdlib.h>
  39. #include <sofia-sip/sip.h>
  40. #include <sofia-sip/sip_header.h>
  41. #include <sofia-sip/msg_date.h>
  42. void usage(int exitcode)
  43. {
  44. fprintf(stderr,
  45. "usage: test_date [SIP-date] "
  46. "[YYYYy][DDd][HHh][MMm][SS[s]]\n");
  47. exit(exitcode);
  48. }
  49. int main(int ac, char *av[])
  50. {
  51. int i;
  52. sip_time_t t, delta, t2;
  53. char const *s;
  54. int verbatim = 0, retval = 0;
  55. t = ((31 + 27) * 24) * 60 * 60;
  56. delta = (365 * 24 + 6) * 60 * 60;
  57. if (su_strmatch(av[1], "-v"))
  58. verbatim = 1, av++;
  59. if ((s = av[1])) {
  60. if (msg_date_d(&s, &t) < 0) {
  61. fprintf(stderr, "test_date: %s is not valid time\n", s);
  62. exit(1);
  63. }
  64. if ((s = av[2])) {
  65. for (delta = 0; *s; ) {
  66. t2 = 0;
  67. msg_delta_d(&s, &t2);
  68. switch (*s++) {
  69. case 'y': delta += t2 * (365 * 24 + 6) * 60 * 60; break;
  70. case 'd': delta += t2 * 24 * 60 * 60; break;
  71. case 'h': delta += t2 * 60 * 60; break;
  72. case 'm': delta += t2 * 60; break;
  73. case '\0': --s; /* FALLTHROUGH */
  74. case 's': delta += t2; break;
  75. default:
  76. fprintf(stderr, "test_date: %s is not valid time offset\n" , av[2]);
  77. usage(1);
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. for (i = 0; i < 20; i++) {
  84. char buf[80];
  85. msg_date_e(buf, sizeof(buf), t);
  86. if (verbatim)
  87. printf("%08lx is %s\n", t, buf);
  88. s = buf, t2 = 0;
  89. if (msg_date_d(&s, &t2) < 0) {
  90. fprintf(stderr, "test_date: decoding %s failed\n", buf);
  91. retval = 1;
  92. break;
  93. }
  94. else if (t2 != t) {
  95. fprintf(stderr, "test_date: %lu != %lu\n", t, t2);
  96. retval = 1;
  97. break;
  98. }
  99. t += delta;
  100. }
  101. return retval;
  102. }