Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
timestamp.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * timestamp.c - Convert a textual timestamps to seconds since epoch
59  *
60  * Read textual timestamps, one per line, from the standard input, and
61  * convert them to integers giving the corresponding number of seconds
62  * since the beginning of the epoch (beginning of 1970). Both the input
63  * and the results should be in UTC.
64  *
65  * Lars Wirzenius
66  */
67 
68 
69 #include <stdio.h>
70 
71 #include "gwlib/gwlib.h"
72 
73 
74 static Octstr *read_line(FILE *f, Octstr *buf)
75 {
76  Octstr *os;
77  char cbuf[8*1024];
78  size_t n;
79  long pos;
80 
81  pos = octstr_search_char(buf, '\n', 0);
82  while (pos == -1 && (n = fread(cbuf, 1, sizeof(cbuf), f)) > 0) {
83  octstr_append_data(buf, cbuf, n);
84  pos = octstr_search_char(buf, '\n', 0);
85  }
86 
87  if (pos == -1) {
88  pos = octstr_len(buf);
89  if (pos == 0)
90  return NULL;
91  }
92  os = octstr_copy(buf, 0, pos);
93  octstr_delete(buf, 0, pos + 1);
94 
95  return os;
96 }
97 
98 
99 static int remove_long(long *p, Octstr *os)
100 {
101  long pos;
102 
103  pos = octstr_parse_long(p, os, 0, 10);
104  if (pos == -1)
105  return -1;
106  octstr_delete(os, 0, pos);
107  return 0;
108 }
109 
110 
111 static int remove_prefix(Octstr *os, Octstr *prefix)
112 {
113  if (octstr_ncompare(os, prefix, octstr_len(prefix)) != 0)
114  return -1;
115  octstr_delete(os, 0, octstr_len(prefix));
116  return 0;
117 }
118 
119 
120 static int parse_date(struct universaltime *ut, Octstr *os)
121 {
122  if (remove_long(&ut->year, os) == -1 ||
123  remove_prefix(os, octstr_imm("-")) == -1 ||
124  remove_long(&ut->month, os) == -1 ||
125  remove_prefix(os, octstr_imm("-")) == -1 ||
126  remove_long(&ut->day, os) == -1 ||
127  remove_prefix(os, octstr_imm(" ")) == -1 ||
128  remove_long(&ut->hour, os) == -1 ||
129  remove_prefix(os, octstr_imm(":")) == -1 ||
130  remove_long(&ut->minute, os) == -1 ||
131  remove_prefix(os, octstr_imm(":")) == -1 ||
132  remove_long(&ut->second, os) == -1 ||
133  remove_prefix(os, octstr_imm(" ")) == -1)
134  return -1;
135  return 0;
136 }
137 
138 
139 int main(void)
140 {
141  struct universaltime ut;
142  Octstr *os;
143  Octstr *buf;
144 
145  gwlib_init();
146  buf = octstr_create("");
147  while ((os = read_line(stdin, buf)) != NULL) {
148  if (parse_date(&ut, os) == -1)
149  panic(0, "Bad line: %s", octstr_get_cstr(os));
150  printf("%ld %s\n", date_convert_universal(&ut), octstr_get_cstr(os));
151  octstr_destroy(os);
152  }
153 
155  gwlib_shutdown();
156 
157  return 0;
158 }
long year
Definition: date.h:72
void octstr_append_data(Octstr *ostr, const char *data, long len)
Definition: octstr.c:1495
static int parse_date(struct universaltime *ut, Octstr *os)
Definition: timestamp.c:120
long minute
Definition: date.h:74
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
long octstr_search_char(const Octstr *ostr, int ch, long pos)
Definition: octstr.c:1010
static int remove_prefix(Octstr *os, Octstr *prefix)
Definition: timestamp.c:111
static int remove_long(long *p, Octstr *os)
Definition: timestamp.c:99
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
void log_set_output_level(enum output_level level)
Definition: log.c:217
void octstr_delete(Octstr *ostr1, long pos, long len)
Definition: octstr.c:1525
int octstr_ncompare(const Octstr *ostr1, const Octstr *ostr2, long n)
Definition: octstr.c:950
long day
Definition: date.h:70
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
long second
Definition: date.h:75
static Octstr * read_line(FILE *f, Octstr *buf)
Definition: timestamp.c:74
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
long hour
Definition: date.h:73
long month
Definition: date.h:71
#define panic
Definition: log.h:87
int main(void)
Definition: timestamp.c:139
long octstr_parse_long(long *nump, Octstr *ostr, long pos, int base)
Definition: octstr.c:747
void gwlib_shutdown(void)
Definition: gwlib.c:94
void gwlib_init(void)
Definition: gwlib.c:78
long date_convert_universal(struct universaltime *t)
Definition: date.c:118
Definition: log.h:69
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.