File: time.c

package info (click to toggle)
mailavenger 0.8.1-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,860 kB
  • ctags: 5,015
  • sloc: cpp: 21,195; ansic: 15,232; sh: 10,544; makefile: 265
file content (81 lines) | stat: -rw-r--r-- 1,750 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* $Id$ */

/*
 *
 * Copyright (C) 2003 David Mazieres (dm@uun.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#include "avutil.h"

long
parse_expire (const char *age, time_t now)
{
  unsigned long res = -1;
  char mult;
  unsigned int len;
  int sign = 1;

  if (age[0] == '+')
    age++;
  else if (age[0] == '-') {
    sign = -1;
    age++;
  }
  else if (sscanf (age, "%lu%n", &res, &len) == 1 && len == strlen (age))
    return res;

  if (sscanf (age, "%lu%c%n", &res, &mult, &len) != 2
      || len != strlen (age)) {
    fprintf (stderr, "bad time %s\n", age);
    return -1;
  }

  if (sign < 0)
    res = -res;
  switch (mult) {
  case 'Y':
    res *= 365 * 24 * 60 * 60;
    break;
  case 'M':
    res *= 30 * 24 * 60 * 60;
    break;
  case 'W':
  case 'w':
    res *= 7 * 24 * 60 * 60;
    break;
  case 'D':
  case 'd':
    res *= 24 * 60 * 60;
    break;
  case 'h':
    res *= 60 * 60;
    break;
  case 'm':
    res *= 60;
    break;
  case 's':
    break;
  default:
    fprintf (stderr, "unknown age unit '%c'\n", mult);
    return -1;
  }

  return now + res;
}