blob: 03eb81b9f2e20431fd2cff415eebc50633d58563 [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
Yingdi Yu61ec2722014-01-20 14:22:32 -08007#include <ndn-cpp-dev/ndn-cpp-config.h>
Jeff Thompsond63baba2013-10-18 17:47:58 -07008#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>
Alexander Afanasyevd409d592014-01-28 18:36:38 -080017#include "time.hpp"
Jeff Thompson9ae4d782013-10-17 10:25:54 -070018
Alexander Afanasyevd409d592014-01-28 18:36:38 -080019namespace ndn {
20
21MillisecondsSince1970
Jeff Thompson9ae4d782013-10-17 10:25:54 -070022ndn_getNowMilliseconds()
23{
24 struct timeval t;
Jeff Thompsonea946202013-10-18 14:35:32 -070025 // Note: configure.ac requires gettimeofday.
Jeff Thompson9ae4d782013-10-17 10:25:54 -070026 gettimeofday(&t, 0);
27 return t.tv_sec * 1000.0 + t.tv_usec / 1000.0;
28}
Jeff Thompson5a6ce832013-10-18 11:54:35 -070029
Alexander Afanasyevd409d592014-01-28 18:36:38 -080030int
31ndn_toIsoString(MillisecondsSince1970 milliseconds, char *isoString)
Jeff Thompson5a6ce832013-10-18 11:54:35 -070032{
Jeff Thompsonea946202013-10-18 14:35:32 -070033#if NDN_CPP_HAVE_GMTIME_SUPPORT
Jeff Thompsonf51f9a52013-10-18 16:21:48 -070034 if (milliseconds < 0)
Alexander Afanasyevd409d592014-01-28 18:36:38 -080035 return -1;
Jeff Thompsonf51f9a52013-10-18 16:21:48 -070036 else if (milliseconds > 2e14)
37 // 2e14 is about the year 8300. We don't want to go over a 4-digit year.
Alexander Afanasyevd409d592014-01-28 18:36:38 -080038 return -1;
Jeff Thompsonf51f9a52013-10-18 16:21:48 -070039
Jeff Thompsonea946202013-10-18 14:35:32 -070040 double secondsSince1970 = milliseconds / 1000.0;
Jeff Thompson5a6ce832013-10-18 11:54:35 -070041 char fractionBuffer[10];
42 sprintf(fractionBuffer, "%.06lf", fmod(secondsSince1970, 1.0));
43 const char *fraction = strchr(fractionBuffer, '.');
44 if (!fraction)
45 // Don't expect this to happen.
46 fraction = ".000000";
47
Yingdi Yu88663af2014-01-15 15:21:38 -080048 time_t seconds = (time_t)secondsSince1970;
Jeff Thompson5a6ce832013-10-18 11:54:35 -070049 struct tm* gmt = gmtime(&seconds);
50 sprintf(isoString, "%04d%02d%02dT%02d%02d%02d%s", 1900 + gmt->tm_year, gmt->tm_mon + 1, gmt->tm_mday,
51 gmt->tm_hour, gmt->tm_min, gmt->tm_sec, fraction);
Jeff Thompsonea946202013-10-18 14:35:32 -070052
Alexander Afanasyevd409d592014-01-28 18:36:38 -080053 return 0;
Jeff Thompsonea946202013-10-18 14:35:32 -070054#else
Alexander Afanasyevd409d592014-01-28 18:36:38 -080055 return -1;
Jeff Thompsonea946202013-10-18 14:35:32 -070056#endif
Jeff Thompson5a6ce832013-10-18 11:54:35 -070057}
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070058
Alexander Afanasyevd409d592014-01-28 18:36:38 -080059int
60ndn_fromIsoString(const char* isoString, MillisecondsSince1970 *milliseconds)
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070061{
Jeff Thompsonea946202013-10-18 14:35:32 -070062#if NDN_CPP_HAVE_GMTIME_SUPPORT
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070063 // Initialize time zone, etc.
64 time_t dummyTime = 0;
Jeff Thompsonea946202013-10-18 14:35:32 -070065 struct tm tm1 = *gmtime(&dummyTime);
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070066
Jeff Thompsonea946202013-10-18 14:35:32 -070067 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 -070068 // Skip the time past minutes and get the float seconds.
69 double seconds;
70 sscanf(isoString + (4 + 2 + 2 + 1 + 2 + 2), "%lf", &seconds);
71
72 // tm_year starts from 1900.
Jeff Thompsonea946202013-10-18 14:35:32 -070073 tm1.tm_year -= 1900;
Jeff Thompsonfdaaa582013-10-18 13:05:10 -070074 // tm_mon starts from 0, not 1.
Jeff Thompsonea946202013-10-18 14:35:32 -070075 tm1.tm_mon -= 1;
76 tm1.tm_sec = 0;
77
78 *milliseconds = (timegm(&tm1) + seconds) * 1000.0;
Alexander Afanasyevd409d592014-01-28 18:36:38 -080079 return 0;
Jeff Thompsonea946202013-10-18 14:35:32 -070080#else
Alexander Afanasyevd409d592014-01-28 18:36:38 -080081 return -1;
Jeff Thompsonea946202013-10-18 14:35:32 -070082#endif
83}
Alexander Afanasyevd409d592014-01-28 18:36:38 -080084
85} // namespace ndn