sip-date.c 4.03 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
26
 *
Pekka Pessi's avatar
Pekka Pessi committed
27
 * @section sip_date_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
 *
Pekka Pessi's avatar
Pekka Pessi committed
31
 * @section sip_date_description Description
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
 * 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.
37
 *
Pekka Pessi's avatar
Pekka Pessi committed
38
 * @section sip_date_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>
 *
Pekka Pessi's avatar
Pekka Pessi committed
48
 * @section sip_date_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
 *
Pekka Pessi's avatar
Pekka Pessi committed
60
 * @section sip_date_bugs Reporting Bugs
61 62
 * Report bugs to <sofia-sip-devel@lists.sourceforge.net>.
 *
Pekka Pessi's avatar
Pekka Pessi committed
63
 * @section sip_date_author Author
64
 * Pekka Pessi <Pekka -dot- Pessi -at- nokia -dot- com>
Pekka Pessi's avatar
Pekka Pessi committed
65
 *
Pekka Pessi's avatar
Pekka Pessi committed
66
 * @section sip_date_copyright Copyright
67 68 69 70 71
 * 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>
Pekka Pessi's avatar
Pekka Pessi committed
84

Pekka Pessi's avatar
Pekka Pessi committed
85 86
void usage(void)
{
87
  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);
}

Pekka Pessi's avatar
Pekka Pessi committed
93
/* Epoch year. */
94
#define EPOCH 1900
Pekka Pessi's avatar
Pekka Pessi committed
95
/* Day number of New Year Day of given year */
Pekka Pessi's avatar
Pekka Pessi committed
96 97 98 99 100 101
#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;
102
  sip_time_t t = 0, t2 = 0;
Pekka Pessi's avatar
Pekka Pessi committed
103 104 105 106 107 108 109 110 111 112
  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])) {
Pekka Pessi's avatar
Pekka Pessi committed
113
    size_t n, m;
Pekka Pessi's avatar
Pekka Pessi committed
114 115 116 117 118 119 120 121 122 123 124 125
    /* 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;
126

Pekka Pessi's avatar
Pekka Pessi committed
127 128
    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
	exit(1);
      }
    }
    else {
      for (; *s; ) {
	if (msg_delta_d(&s, &t2) < 0)
	  usage();
137

Pekka Pessi's avatar
Pekka Pessi committed
138
	switch (*s) {
139
	case 'y': t2 = YEAR_DAYS(t2) - YEAR_DAYS(EPOCH);
Pekka Pessi's avatar
Pekka Pessi committed
140 141 142 143
	  /*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;
144
	case 's':
Pekka Pessi's avatar
Pekka Pessi committed
145 146
	  s++;
	default:
147
	  t += t2;
Pekka Pessi's avatar
Pekka Pessi committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	  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;
}