Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

timestamp.c

Go to the documentation of this file.
00001 /* ==================================================================== 
00002  * The Kannel Software License, Version 1.0 
00003  * 
00004  * Copyright (c) 2001-2008 Kannel Group  
00005  * Copyright (c) 1998-2001 WapIT Ltd.   
00006  * All rights reserved. 
00007  * 
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions 
00010  * are met: 
00011  * 
00012  * 1. Redistributions of source code must retain the above copyright 
00013  *    notice, this list of conditions and the following disclaimer. 
00014  * 
00015  * 2. Redistributions in binary form must reproduce the above copyright 
00016  *    notice, this list of conditions and the following disclaimer in 
00017  *    the documentation and/or other materials provided with the 
00018  *    distribution. 
00019  * 
00020  * 3. The end-user documentation included with the redistribution, 
00021  *    if any, must include the following acknowledgment: 
00022  *       "This product includes software developed by the 
00023  *        Kannel Group (http://www.kannel.org/)." 
00024  *    Alternately, this acknowledgment may appear in the software itself, 
00025  *    if and wherever such third-party acknowledgments normally appear. 
00026  * 
00027  * 4. The names "Kannel" and "Kannel Group" must not be used to 
00028  *    endorse or promote products derived from this software without 
00029  *    prior written permission. For written permission, please  
00030  *    contact org@kannel.org. 
00031  * 
00032  * 5. Products derived from this software may not be called "Kannel", 
00033  *    nor may "Kannel" appear in their name, without prior written 
00034  *    permission of the Kannel Group. 
00035  * 
00036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
00037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00039  * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 
00040  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
00041  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  
00042  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  
00043  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
00044  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  
00045  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  
00046  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00047  * ==================================================================== 
00048  * 
00049  * This software consists of voluntary contributions made by many 
00050  * individuals on behalf of the Kannel Group.  For more information on  
00051  * the Kannel Group, please see <http://www.kannel.org/>. 
00052  * 
00053  * Portions of this software are based upon software originally written at  
00054  * WapIT Ltd., Helsinki, Finland for the Kannel project.  
00055  */ 
00056 
00057 /*
00058  * timestamp.c - Convert a textual timestamps to seconds since epoch
00059  *
00060  * Read textual timestamps, one per line, from the standard input, and 
00061  * convert them to integers giving the corresponding number of seconds
00062  * since the beginning of the epoch (beginning of 1970). Both the input
00063  * and the results should be in UTC.
00064  *
00065  * Lars Wirzenius
00066  */
00067 
00068 
00069 #include <stdio.h>
00070 
00071 #include "gwlib/gwlib.h"
00072 
00073 
00074 static Octstr *read_line(FILE *f, Octstr *buf)
00075 {
00076     Octstr *os;
00077     char cbuf[8*1024];
00078     size_t n;
00079     long pos;
00080     
00081     pos = octstr_search_char(buf, '\n', 0);
00082     while (pos == -1 && (n = fread(cbuf, 1, sizeof(cbuf), f)) > 0) {
00083     octstr_append_data(buf, cbuf, n);
00084     pos = octstr_search_char(buf, '\n', 0);
00085     }
00086 
00087     if (pos == -1) {
00088         pos = octstr_len(buf);
00089     if (pos == 0)
00090         return NULL;
00091     }
00092     os = octstr_copy(buf, 0, pos);
00093     octstr_delete(buf, 0, pos + 1);
00094 
00095     return os;
00096 }
00097 
00098 
00099 static int remove_long(long *p, Octstr *os)
00100 {
00101     long pos;
00102     
00103     pos = octstr_parse_long(p, os, 0, 10);
00104     if (pos == -1)
00105         return -1;
00106     octstr_delete(os, 0, pos);
00107     return 0;
00108 }
00109 
00110 
00111 static int remove_prefix(Octstr *os, Octstr *prefix)
00112 {
00113     if (octstr_ncompare(os, prefix, octstr_len(prefix)) != 0)
00114         return -1;
00115     octstr_delete(os, 0, octstr_len(prefix));
00116     return 0;
00117 }
00118 
00119 
00120 static int parse_date(struct universaltime *ut, Octstr *os)
00121 {
00122     long pos;
00123 
00124     pos = 0;
00125     
00126     if (remove_long(&ut->year, os) == -1 ||
00127         remove_prefix(os, octstr_imm("-")) == -1 ||
00128     remove_long(&ut->month, os) == -1 ||
00129         remove_prefix(os, octstr_imm("-")) == -1 ||
00130     remove_long(&ut->day, os) == -1 ||
00131         remove_prefix(os, octstr_imm(" ")) == -1 ||
00132     remove_long(&ut->hour, os) == -1 ||
00133         remove_prefix(os, octstr_imm(":")) == -1 ||
00134     remove_long(&ut->minute, os) == -1 ||
00135         remove_prefix(os, octstr_imm(":")) == -1 ||
00136     remove_long(&ut->second, os) == -1 ||
00137         remove_prefix(os, octstr_imm(" ")) == -1)
00138         return -1;
00139     return 0;
00140 }
00141 
00142 
00143 int main(void)
00144 {
00145     struct universaltime ut;
00146     Octstr *os;
00147     Octstr *buf;
00148     
00149     gwlib_init();
00150     buf = octstr_create("");
00151     while ((os = read_line(stdin, buf)) != NULL) {
00152     if (parse_date(&ut, os) == -1)
00153         panic(0, "Bad line: %s", octstr_get_cstr(os));
00154     printf("%ld %s\n", date_convert_universal(&ut), octstr_get_cstr(os));
00155     octstr_destroy(os);
00156     }
00157 
00158     log_set_output_level(GW_PANIC);
00159     gwlib_shutdown();
00160 
00161     return 0;
00162 }
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.