blob: 4c3be3a71eb2ddacac0d3051aea41b8851e8cc66 [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 Thompsond63baba2013-10-18 17:47:58 -07007#include <ndn-cpp/ndn-cpp-config.h>
8#if NDN_CPP_HAVE_TIME_H
Jeff Thompsond32bab62013-10-18 14:55:26 -07009#include <time.h>
Jeff Thompsond63baba2013-10-18 17:47:58 -070010#endif
11#if NDN_CPP_HAVE_SYS_TIME_H
Jeff Thompson9ae4d782013-10-17 10:25:54 -070012#include <sys/time.h>
Jeff Thompsond63baba2013-10-18 17:47:58 -070013#endif
Jeff Thompson5a6ce832013-10-18 11:54:35 -070014#include <math.h>
15#include <string.h>
16#include <stdio.h>
Jeff Thompson9ae4d782013-10-17 10:25:54 -070017#include "time.h"
18
Jeff Thompson9a8e82f2013-10-17 14:13:43 -070019ndn_MillisecondsSince1970
Jeff Thompson9ae4d782013-10-17 10:25:54 -070020ndn_getNowMilliseconds()
21{
22 struct timeval t;
Jeff Thompsonea946202013-10-18 14:35:32 -070023 // Note: configure.ac requires gettimeofday.
Jeff Thompson9ae4d782013-10-17 10:25:54 -070024 gettimeofday(&t, 0);
25 return t.tv_sec * 1000.0 + t.tv_usec / 1000.0;
26}
Jeff Thompson5a6ce832013-10-18 11:54:35 -070027
Jeff Thompsonea946202013-10-18 14:35:32 -070028ndn_Error
29ndn_toIsoString(ndn_MillisecondsSince1970 milliseconds, char *isoString)
Jeff Thompson5a6ce832013-10-18 11:54:35 -070030{
Jeff Thompsonea946202013-10-18 14:35:32 -070031#if NDN_CPP_HAVE_GMTIME_SUPPORT
Jeff Thompsonf51f9a52013-10-18 16:21:48 -070032 if (milliseconds < 0)
33 return NDN_ERROR_Calendar_time_value_out_of_range;
34 else if (milliseconds > 2e14)
35 // 2e14 is about the year 8300. We don't want to go over a 4-digit year.
36 return NDN_ERROR_Calendar_time_value_out_of_range;
37
Jeff Thompsonea946202013-10-18 14:35:32 -070038 double secondsSince1970 = milliseconds / 1000.0;
Jeff Thompson5a6ce832013-10-18 11:54:35 -070039 char fractionBuffer[10];
40 sprintf(fractionBuffer, "%.06lf", fmod(secondsSince1970, 1.0));
41 const char *fraction = strchr(fractionBuffer, '.');
42 if (!fraction)
43 // Don't expect this to happen.
44 fraction = ".000000";
45
Jeff Thompson30f0afe2013-10-18 15:57:36 -070046 time_t seconds = (time_t)floor(secondsSince1970);
Jeff Thompson5a6ce832013-10-18 11:54:35 -070047 struct tm* gmt = gmtime(&seconds);
48 sprintf(isoString, "%04d%02d%02dT%02d%02d%02d%s", 1900 + gmt->tm_year, gmt->tm_mon + 1, gmt->tm_mday,
49 gmt->tm_hour, gmt->tm_min, gmt->tm_sec, fraction);
Jeff Thompsonea946202013-10-18 14:35:32 -070050
51 return NDN_ERROR_success;
52#else
53 return NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library;
54#endif
Jeff Thompson5a6ce832013-10-18 11:54:35 -070055}
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070056
Jeff Thompsonea946202013-10-18 14:35:32 -070057ndn_Error
58ndn_fromIsoString(const char* isoString, ndn_MillisecondsSince1970 *milliseconds)
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070059{
Jeff Thompsonea946202013-10-18 14:35:32 -070060#if NDN_CPP_HAVE_GMTIME_SUPPORT
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070061 // Initialize time zone, etc.
62 time_t dummyTime = 0;
Jeff Thompsonea946202013-10-18 14:35:32 -070063 struct tm tm1 = *gmtime(&dummyTime);
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070064
Jeff Thompsonea946202013-10-18 14:35:32 -070065 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 -070066 // Skip the time past minutes and get the float seconds.
67 double seconds;
68 sscanf(isoString + (4 + 2 + 2 + 1 + 2 + 2), "%lf", &seconds);
69
70 // tm_year starts from 1900.
Jeff Thompsonea946202013-10-18 14:35:32 -070071 tm1.tm_year -= 1900;
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070072 // tm_mon starts from 0, not 1.
Jeff Thompsonea946202013-10-18 14:35:32 -070073 tm1.tm_mon -= 1;
74 tm1.tm_sec = 0;
75
76 *milliseconds = (timegm(&tm1) + seconds) * 1000.0;
77 return NDN_ERROR_success;
78#else
79 return NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library;
80#endif
81}