blob: d1e2163f6fa6b6eec0cf6bb4cf621f5ba9631832 [file] [log] [blame]
Jeff Thompson9ae4d782013-10-17 10:25:54 -07001/**
2 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
4 * See COPYING for copyright and distribution information.
5 */
6
Jeff Thompsond32bab62013-10-18 14:55:26 -07007#include <time.h>
Jeff Thompson9ae4d782013-10-17 10:25:54 -07008#include <sys/time.h>
Jeff Thompson5a6ce832013-10-18 11:54:35 -07009#include <math.h>
10#include <string.h>
11#include <stdio.h>
Jeff Thompson9ae4d782013-10-17 10:25:54 -070012#include "time.h"
13
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070014ndn_MillisecondsSince1970
Jeff Thompson9ae4d782013-10-17 10:25:54 -070015ndn_getNowMilliseconds()
16{
17 struct timeval t;
Jeff Thompsonea946202013-10-18 14:35:32 -070018 // Note: configure.ac requires gettimeofday.
Jeff Thompson9ae4d782013-10-17 10:25:54 -070019 gettimeofday(&t, 0);
20 return t.tv_sec * 1000.0 + t.tv_usec / 1000.0;
21}
Jeff Thompson5a6ce832013-10-18 11:54:35 -070022
Jeff Thompsonea946202013-10-18 14:35:32 -070023ndn_Error
24ndn_toIsoString(ndn_MillisecondsSince1970 milliseconds, char *isoString)
Jeff Thompson5a6ce832013-10-18 11:54:35 -070025{
Jeff Thompsonea946202013-10-18 14:35:32 -070026#if NDN_CPP_HAVE_GMTIME_SUPPORT
27 double secondsSince1970 = milliseconds / 1000.0;
Jeff Thompson5a6ce832013-10-18 11:54:35 -070028 char fractionBuffer[10];
29 sprintf(fractionBuffer, "%.06lf", fmod(secondsSince1970, 1.0));
30 const char *fraction = strchr(fractionBuffer, '.');
31 if (!fraction)
32 // Don't expect this to happen.
33 fraction = ".000000";
34
Jeff Thompson30f0afe2013-10-18 15:57:36 -070035 time_t seconds = (time_t)floor(secondsSince1970);
Jeff Thompson5a6ce832013-10-18 11:54:35 -070036 struct tm* gmt = gmtime(&seconds);
37 sprintf(isoString, "%04d%02d%02dT%02d%02d%02d%s", 1900 + gmt->tm_year, gmt->tm_mon + 1, gmt->tm_mday,
38 gmt->tm_hour, gmt->tm_min, gmt->tm_sec, fraction);
Jeff Thompsonea946202013-10-18 14:35:32 -070039
40 return NDN_ERROR_success;
41#else
42 return NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library;
43#endif
Jeff Thompson5a6ce832013-10-18 11:54:35 -070044}
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070045
Jeff Thompsonea946202013-10-18 14:35:32 -070046ndn_Error
47ndn_fromIsoString(const char* isoString, ndn_MillisecondsSince1970 *milliseconds)
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070048{
Jeff Thompsonea946202013-10-18 14:35:32 -070049#if NDN_CPP_HAVE_GMTIME_SUPPORT
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070050 // Initialize time zone, etc.
51 time_t dummyTime = 0;
Jeff Thompsonea946202013-10-18 14:35:32 -070052 struct tm tm1 = *gmtime(&dummyTime);
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070053
Jeff Thompsonea946202013-10-18 14:35:32 -070054 sscanf(isoString, "%4d%2d%2dT%2d%2d", &tm1.tm_year, &tm1.tm_mon, &tm1.tm_mday, &tm1.tm_hour, &tm1.tm_min);
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070055 // Skip the time past minutes and get the float seconds.
56 double seconds;
57 sscanf(isoString + (4 + 2 + 2 + 1 + 2 + 2), "%lf", &seconds);
58
59 // tm_year starts from 1900.
Jeff Thompsonea946202013-10-18 14:35:32 -070060 tm1.tm_year -= 1900;
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070061 // tm_mon starts from 0, not 1.
Jeff Thompsonea946202013-10-18 14:35:32 -070062 tm1.tm_mon -= 1;
63 tm1.tm_sec = 0;
64
65 *milliseconds = (timegm(&tm1) + seconds) * 1000.0;
66 return NDN_ERROR_success;
67#else
68 return NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library;
69#endif
70}