blob: 314eeada574ae7aaabef7f8144614fd6b9847cb4 [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
Jeff Thompsonf51f9a52013-10-18 16:21:48 -070027 if (milliseconds < 0)
28 return NDN_ERROR_Calendar_time_value_out_of_range;
29 else if (milliseconds > 2e14)
30 // 2e14 is about the year 8300. We don't want to go over a 4-digit year.
31 return NDN_ERROR_Calendar_time_value_out_of_range;
32
Jeff Thompsonea946202013-10-18 14:35:32 -070033 double secondsSince1970 = milliseconds / 1000.0;
Jeff Thompson5a6ce832013-10-18 11:54:35 -070034 char fractionBuffer[10];
35 sprintf(fractionBuffer, "%.06lf", fmod(secondsSince1970, 1.0));
36 const char *fraction = strchr(fractionBuffer, '.');
37 if (!fraction)
38 // Don't expect this to happen.
39 fraction = ".000000";
40
Jeff Thompson30f0afe2013-10-18 15:57:36 -070041 time_t seconds = (time_t)floor(secondsSince1970);
Jeff Thompson5a6ce832013-10-18 11:54:35 -070042 struct tm* gmt = gmtime(&seconds);
43 sprintf(isoString, "%04d%02d%02dT%02d%02d%02d%s", 1900 + gmt->tm_year, gmt->tm_mon + 1, gmt->tm_mday,
44 gmt->tm_hour, gmt->tm_min, gmt->tm_sec, fraction);
Jeff Thompsonea946202013-10-18 14:35:32 -070045
46 return NDN_ERROR_success;
47#else
48 return NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library;
49#endif
Jeff Thompson5a6ce832013-10-18 11:54:35 -070050}
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070051
Jeff Thompsonea946202013-10-18 14:35:32 -070052ndn_Error
53ndn_fromIsoString(const char* isoString, ndn_MillisecondsSince1970 *milliseconds)
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070054{
Jeff Thompsonea946202013-10-18 14:35:32 -070055#if NDN_CPP_HAVE_GMTIME_SUPPORT
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070056 // Initialize time zone, etc.
57 time_t dummyTime = 0;
Jeff Thompsonea946202013-10-18 14:35:32 -070058 struct tm tm1 = *gmtime(&dummyTime);
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070059
Jeff Thompsonea946202013-10-18 14:35:32 -070060 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 -070061 // Skip the time past minutes and get the float seconds.
62 double seconds;
63 sscanf(isoString + (4 + 2 + 2 + 1 + 2 + 2), "%lf", &seconds);
64
65 // tm_year starts from 1900.
Jeff Thompsonea946202013-10-18 14:35:32 -070066 tm1.tm_year -= 1900;
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070067 // tm_mon starts from 0, not 1.
Jeff Thompsonea946202013-10-18 14:35:32 -070068 tm1.tm_mon -= 1;
69 tm1.tm_sec = 0;
70
71 *milliseconds = (timegm(&tm1) + seconds) * 1000.0;
72 return NDN_ERROR_success;
73#else
74 return NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library;
75#endif
76}