sip-date.c 3.99 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
 * 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
 *
 */

25
/**@page sip-date Print or parse SIP date
Pekka Pessi's avatar
Pekka Pessi committed
26
 * 
27
 * @section synopsis Synopsis
Pekka Pessi's avatar
Pekka Pessi committed
28
 *
29
 * <tt>sip-date [-n] [SIP-date | [YYYYy] [DDd] [HHh] [MMm] [SS[s]]]</tt>
Pekka Pessi's avatar
Pekka Pessi committed
30
 *
31
 * @section description Description
Pekka Pessi's avatar
Pekka Pessi committed
32
 * 
33
 * @em sip-date is an utility for printing a SIP date (in the format
Pekka Pessi's avatar
Pekka Pessi committed
34 35 36 37
 * specified by RFC 1123, but the timezone must always be GMT) or parsing a
 * given SIP date. The date can be given as a SIP date or by giving year,
 * day, hour, minutes and seconds separately.
 * 
38
 * @section options Options
Pekka Pessi's avatar
Pekka Pessi committed
39
 *
40
 * The @em sip-date utility takes options as follows:
Pekka Pessi's avatar
Pekka Pessi committed
41 42
 * <dl>
 * <dt>-n</dt>
43
 * <dd>The @em sip-date utility prints the date as seconds elapsed since
Pekka Pessi's avatar
Pekka Pessi committed
44 45 46 47
 * epoch (01 Jan 1900 00:00:00).
 * </dd>
 * </dl>
 *
48
 * @section examples Examples
Pekka Pessi's avatar
Pekka Pessi committed
49 50 51
 *
 * You want to convert current time to SIP date:
 * @code
52
 * $ sip-date
Pekka Pessi's avatar
Pekka Pessi committed
53 54 55 56 57 58 59
 * @endcode
 * You want to find out how many seconds there was in 1900's:
 * @code
 * $ siptime -n 2000y
 * 3155673600
 * @endcode
 *
60 61 62 63 64
 * @section bugs Reporting Bugs
 * Report bugs to <sofia-sip-devel@lists.sourceforge.net>.
 *
 * @section author Author
 * Pekka Pessi <Pekka -dot- Pessi -at- nokia -dot- com>
Pekka Pessi's avatar
Pekka Pessi committed
65
 *
66 67 68 69 70 71
 * @section copyright Copyright
 * Copyright (C) 2005 Nokia Corporation.
 *
 * This program is free software; see the source for copying conditions.
 * There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.
Pekka Pessi's avatar
Pekka Pessi committed
72 73
 */

74 75 76 77 78 79 80 81 82 83
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>

#include <sofia-sip/sip.h>
#include <sofia-sip/sip_header.h>
#include <sofia-sip/msg_date.h>
84

Pekka Pessi's avatar
Pekka Pessi committed
85 86 87
void usage(void)
{
  fprintf(stderr, 
88
	  "usage: sip-date [-n] "
Pekka Pessi's avatar
Pekka Pessi committed
89 90 91 92
          "[SIP-date | [YYYYy] [DDd] [HHh] [MMm] [SS[s]]]\n");
  exit(1);
}

93
/* Epoch year. */
Pekka Pessi's avatar
Pekka Pessi committed
94
#define EPOCH 1900		
95
/* Day number of New Year Day of given year */
Pekka Pessi's avatar
Pekka Pessi committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#define YEAR_DAYS(y) \
  (((y)-1) * 365 + ((y)-1) / 4 - ((y)-1) / 100 + ((y)-1) / 400)

int main(int ac, char *av[])
{
  int numeric = 0;
  sip_time_t t, t2;
  char const *s;
  char buf[1024];

  if (av[1] && strcmp(av[1], "-n") == 0)
    av++, numeric = 1;

  if (av[1] && (strcmp(av[1], "-?") == 0 || strcmp(av[1], "-h") == 0))
    usage();

  if ((s = av[1])) {
113
    size_t n, m;
Pekka Pessi's avatar
Pekka Pessi committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    /* Concatenate all arguments with a space in between them */
    for (n = 0; (s = av[1]); av++) {
      m = strlen(s);
      if (n + m + 2 > sizeof(buf))
	exit(1);
      memcpy(buf + n, s, m);
      buf[n + m] = ' ';
      n += m + 1;
    }
    buf[n] = '\0';

    s = buf;
      
    if (s[0] < '0' || s[0] > '9') {
      if (msg_date_d(&s, &t) < 0) {
129
	fprintf(stderr, "sip-date: %s is not valid time\n", s);
Pekka Pessi's avatar
Pekka Pessi committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	exit(1);
      }
    }
    else {
      for (; *s; ) {
	t2 = 0;

	if (msg_delta_d(&s, &t2) < 0)
	  usage();
      
	switch (*s) {
	case 'y': t2 = YEAR_DAYS(t2) - YEAR_DAYS(EPOCH); 
	  /*FALLTHROUGH*/
	case 'd': t += t2 * 24 * 60 * 60; s++; break;
	case 'h': t += t2 * 60 * 60; s++; break;
	case 'm': t += t2 * 60; s++; break;
	case 's': 
	  s++;
	default:
	  t += t2; 
	  break;
	}

	while (*s && *s == ' ')
	  s++;
      }
    }
  }
  else {
    t = sip_now();
  }

  if (numeric) {
    msg_delta_e(buf, sizeof(buf), t);
  }
  else {
    msg_date_e(buf, sizeof(buf), t);
  }

  puts(buf);

  return 0;
}