Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 1 | /** |
| 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 Yu | 61ec272 | 2014-01-20 14:22:32 -0800 | [diff] [blame] | 7 | #include <ndn-cpp-dev/ndn-cpp-config.h> |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 8 | #if NDN_CPP_HAVE_TIME_H |
Jeff Thompson | d32bab6 | 2013-10-18 14:55:26 -0700 | [diff] [blame] | 9 | #include <time.h> |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 10 | #endif |
| 11 | #if NDN_CPP_HAVE_SYS_TIME_H |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 12 | #include <sys/time.h> |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame] | 13 | #endif |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 14 | #include <math.h> |
| 15 | #include <string.h> |
| 16 | #include <stdio.h> |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame^] | 17 | #include "time.hpp" |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 18 | |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame^] | 19 | namespace ndn { |
| 20 | |
| 21 | MillisecondsSince1970 |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 22 | ndn_getNowMilliseconds() |
| 23 | { |
| 24 | struct timeval t; |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 25 | // Note: configure.ac requires gettimeofday. |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 26 | gettimeofday(&t, 0); |
| 27 | return t.tv_sec * 1000.0 + t.tv_usec / 1000.0; |
| 28 | } |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame^] | 30 | int |
| 31 | ndn_toIsoString(MillisecondsSince1970 milliseconds, char *isoString) |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 32 | { |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 33 | #if NDN_CPP_HAVE_GMTIME_SUPPORT |
Jeff Thompson | f51f9a5 | 2013-10-18 16:21:48 -0700 | [diff] [blame] | 34 | if (milliseconds < 0) |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame^] | 35 | return -1; |
Jeff Thompson | f51f9a5 | 2013-10-18 16:21:48 -0700 | [diff] [blame] | 36 | else if (milliseconds > 2e14) |
| 37 | // 2e14 is about the year 8300. We don't want to go over a 4-digit year. |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame^] | 38 | return -1; |
Jeff Thompson | f51f9a5 | 2013-10-18 16:21:48 -0700 | [diff] [blame] | 39 | |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 40 | double secondsSince1970 = milliseconds / 1000.0; |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 41 | 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 Yu | 88663af | 2014-01-15 15:21:38 -0800 | [diff] [blame] | 48 | time_t seconds = (time_t)secondsSince1970; |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 49 | 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 Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 52 | |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame^] | 53 | return 0; |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 54 | #else |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame^] | 55 | return -1; |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 56 | #endif |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 57 | } |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame^] | 59 | int |
| 60 | ndn_fromIsoString(const char* isoString, MillisecondsSince1970 *milliseconds) |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 61 | { |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 62 | #if NDN_CPP_HAVE_GMTIME_SUPPORT |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 63 | // Initialize time zone, etc. |
| 64 | time_t dummyTime = 0; |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 65 | struct tm tm1 = *gmtime(&dummyTime); |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 66 | |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 67 | sscanf(isoString, "%4d%2d%2dT%2d%2d", &tm1.tm_year, &tm1.tm_mon, &tm1.tm_mday, &tm1.tm_hour, &tm1.tm_min); |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 68 | // 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 Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 73 | tm1.tm_year -= 1900; |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 74 | // tm_mon starts from 0, not 1. |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 75 | tm1.tm_mon -= 1; |
| 76 | tm1.tm_sec = 0; |
| 77 | |
| 78 | *milliseconds = (timegm(&tm1) + seconds) * 1000.0; |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame^] | 79 | return 0; |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 80 | #else |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame^] | 81 | return -1; |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 82 | #endif |
| 83 | } |
Alexander Afanasyev | d409d59 | 2014-01-28 18:36:38 -0800 | [diff] [blame^] | 84 | |
| 85 | } // namespace ndn |