test_date.c 2.68 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@internal
 *
27
 * @CFILE test_date.c  
Pekka Pessi's avatar
Pekka Pessi committed
28 29 30 31 32 33 34 35
 *
 * Tester for SIP date parser
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
 *
 * @date Wed Mar 21 19:12:13 2001 ppessi
 */

36 37
#include "config.h"

Pekka Pessi's avatar
Pekka Pessi committed
38
#include <stdio.h>
39
#include <sofia-sip/string0.h>
Pekka Pessi's avatar
Pekka Pessi committed
40 41 42
#include <stddef.h>
#include <stdlib.h>

43 44 45
#include <sofia-sip/sip.h>
#include <sofia-sip/sip_header.h>
#include <sofia-sip/msg_date.h>
Pekka Pessi's avatar
Pekka Pessi committed
46

47
void usage(int exitcode)
Pekka Pessi's avatar
Pekka Pessi committed
48 49
{
  fprintf(stderr, 
50
	  "usage: test_date [SIP-date] "
Pekka Pessi's avatar
Pekka Pessi committed
51
	  "[YYYYy][DDd][HHh][MMm][SS[s]]\n");
52
  exit(exitcode);
Pekka Pessi's avatar
Pekka Pessi committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
}

int main(int ac, char *av[])
{
  int i;
  sip_time_t t, delta, t2;
  char const *s;
  int verbatim = 0, retval = 0;

  t = ((31 + 27) * 24) * 60 * 60;
  delta = (365 * 24 + 6) * 60 * 60;

  if (str0cmp(av[1], "-v") == 0)
    verbatim = 1, av++;

  if ((s = av[1])) {
    if (msg_date_d(&s, &t) < 0) {
70
      fprintf(stderr, "test_date: %s is not valid time\n", s);
Pekka Pessi's avatar
Pekka Pessi committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
      exit(1);
    }
      
    if ((s = av[2])) {
      for (delta = 0; *s; ) {
	t2 = 0;
	msg_delta_d(&s, &t2);

	switch (*s++) {
	case 'y': delta += t2 * (365 * 24 + 6) * 60 * 60; break;
	case 'd': delta += t2 * 24 * 60 * 60; break;
	case 'h': delta += t2 * 60 * 60; break;
	case 'm': delta += t2 * 60; break;
	case '\0': --s;		/* FALLTHROUGH */
	case 's': delta += t2; break;
	default:
87
	  fprintf(stderr, "test_date: %s is not valid time offset\n" , av[2]);
88
	  usage(1);
Pekka Pessi's avatar
Pekka Pessi committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	  break;
	}
      }
    }
  }

  for (i = 0; i < 20; i++) {
    char buf[80];

    msg_date_e(buf, sizeof(buf), t);
    
    if (verbatim)
      printf("%08lx is %s\n", t, buf);

    s = buf, t2 = 0;
    if (msg_date_d(&s, &t2) < 0) {
105
      fprintf(stderr, "test_date: decoding %s failed\n", buf);
Pekka Pessi's avatar
Pekka Pessi committed
106 107 108 109
      retval = 1;
      break;
    }
    else if (t2 != t) {
110
      fprintf(stderr, "test_date: %lu != %lu\n", t, t2);
Pekka Pessi's avatar
Pekka Pessi committed
111 112 113 114 115 116 117 118
      retval = 1;
      break;
    }
    t += delta;
  }

  return retval;
}