ccntimefromdatetime.c

Go to the documentation of this file.
00001 /**
00002  * @file ccntimefromdatetime.c
00003  *
00004  * A little utility for converting canonical dateTime values to
00005  * the scaled binary form used by ccn
00006  *
00007  * A CCNx command-line utility.
00008  *
00009  * Copyright (C) 2008, 2009 Palo Alto Research Center, Inc.
00010  *
00011  * This work is free software; you can redistribute it and/or modify it under
00012  * the terms of the GNU General Public License version 2 as published by the
00013  * Free Software Foundation.
00014  * This work is distributed in the hope that it will be useful, but WITHOUT ANY
00015  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
00016  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00017  * for more details. You should have received a copy of the GNU General Public
00018  * License along with this program; if not, write to the
00019  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020  * Boston, MA 02110-1301, USA.
00021 */
00022 #include <time.h>
00023 #include <math.h>
00024 #include <stdio.h>
00025 #include <stdint.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 
00029 static int
00030 cvt_a_date(char *s)
00031 {
00032     char *leftover;
00033     char *z = "?";
00034     struct tm tm = {0};
00035     time_t seconds;
00036     double fraction = 0.0;
00037     double fulltime;
00038     double back;
00039     int res = 0;
00040     intmax_t fixedscaled;
00041     
00042     leftover = strptime(s, "%FT%T", &tm);
00043     seconds = timegm(&tm);
00044     if (leftover != NULL)
00045         fraction = strtod(leftover, &z);
00046     if (0 != strcmp(z, "Z") || seconds <= 0 ||
00047         fraction < 0.0 || fraction >= 1.0) {
00048         res = 1;
00049         fprintf(stderr, "problem converting %s\n", s);
00050     }
00051     else {
00052         fulltime = (((double)seconds) + fraction);
00053         fixedscaled = (intmax_t)round(fulltime * (double)(1U << 12));
00054         back = (double)fixedscaled / 4096.0; /* Check */
00055         printf("%s\t%012jX\t%f\t%f\n", s, fixedscaled, fulltime, back);
00056     }
00057     return(res);
00058 }
00059 
00060 int
00061 main(int argc, char **argv)
00062 {
00063     int i;
00064     int res = 0;
00065     for (i = 1; i < argc; i++)
00066         res |= cvt_a_date(argv[i]);
00067     return(res == 0 ? 0 : 1);
00068 }

Generated on Thu Feb 16 00:43:59 2012 for Content-Centric Networking in C by  doxygen 1.5.6