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 | |
Jeff Thompson | d63baba | 2013-10-18 17:47:58 -0700 | [diff] [blame^] | 7 | #include <ndn-cpp/ndn-cpp-config.h> |
| 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> |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 17 | #include "time.h" |
| 18 | |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 19 | ndn_MillisecondsSince1970 |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 20 | ndn_getNowMilliseconds() |
| 21 | { |
| 22 | struct timeval t; |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 23 | // Note: configure.ac requires gettimeofday. |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 24 | gettimeofday(&t, 0); |
| 25 | return t.tv_sec * 1000.0 + t.tv_usec / 1000.0; |
| 26 | } |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 27 | |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 28 | ndn_Error |
| 29 | ndn_toIsoString(ndn_MillisecondsSince1970 milliseconds, char *isoString) |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 30 | { |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 31 | #if NDN_CPP_HAVE_GMTIME_SUPPORT |
Jeff Thompson | f51f9a5 | 2013-10-18 16:21:48 -0700 | [diff] [blame] | 32 | 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 Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 38 | double secondsSince1970 = milliseconds / 1000.0; |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 39 | 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 Thompson | 30f0afe | 2013-10-18 15:57:36 -0700 | [diff] [blame] | 46 | time_t seconds = (time_t)floor(secondsSince1970); |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 47 | 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 Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 50 | |
| 51 | return NDN_ERROR_success; |
| 52 | #else |
| 53 | return NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library; |
| 54 | #endif |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 55 | } |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 56 | |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 57 | ndn_Error |
| 58 | ndn_fromIsoString(const char* isoString, ndn_MillisecondsSince1970 *milliseconds) |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 59 | { |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 60 | #if NDN_CPP_HAVE_GMTIME_SUPPORT |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 61 | // Initialize time zone, etc. |
| 62 | time_t dummyTime = 0; |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 63 | struct tm tm1 = *gmtime(&dummyTime); |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 64 | |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 65 | 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] | 66 | // 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 Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 71 | tm1.tm_year -= 1900; |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 72 | // tm_mon starts from 0, not 1. |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 73 | 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 | } |