ccntimefromdatetime.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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;
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 }