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 | |
| 7 | #include <sys/time.h> |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 8 | #include <math.h> |
| 9 | #include <string.h> |
| 10 | #include <stdio.h> |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 11 | #include "time.h" |
| 12 | |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 13 | ndn_MillisecondsSince1970 |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 14 | ndn_getNowMilliseconds() |
| 15 | { |
| 16 | struct timeval t; |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame^] | 17 | // Note: configure.ac requires gettimeofday. |
Jeff Thompson | 9ae4d78 | 2013-10-17 10:25:54 -0700 | [diff] [blame] | 18 | gettimeofday(&t, 0); |
| 19 | return t.tv_sec * 1000.0 + t.tv_usec / 1000.0; |
| 20 | } |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 21 | |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame^] | 22 | ndn_Error |
| 23 | ndn_toIsoString(ndn_MillisecondsSince1970 milliseconds, char *isoString) |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 24 | { |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame^] | 25 | #if NDN_CPP_HAVE_GMTIME_SUPPORT |
| 26 | double secondsSince1970 = milliseconds / 1000.0; |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 27 | char fractionBuffer[10]; |
| 28 | sprintf(fractionBuffer, "%.06lf", fmod(secondsSince1970, 1.0)); |
| 29 | const char *fraction = strchr(fractionBuffer, '.'); |
| 30 | if (!fraction) |
| 31 | // Don't expect this to happen. |
| 32 | fraction = ".000000"; |
| 33 | |
| 34 | time_t seconds = secondsSince1970; |
| 35 | struct tm* gmt = gmtime(&seconds); |
| 36 | sprintf(isoString, "%04d%02d%02dT%02d%02d%02d%s", 1900 + gmt->tm_year, gmt->tm_mon + 1, gmt->tm_mday, |
| 37 | gmt->tm_hour, gmt->tm_min, gmt->tm_sec, fraction); |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame^] | 38 | |
| 39 | return NDN_ERROR_success; |
| 40 | #else |
| 41 | return NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library; |
| 42 | #endif |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 43 | } |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 44 | |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame^] | 45 | ndn_Error |
| 46 | ndn_fromIsoString(const char* isoString, ndn_MillisecondsSince1970 *milliseconds) |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 47 | { |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame^] | 48 | #if NDN_CPP_HAVE_GMTIME_SUPPORT |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 49 | // Initialize time zone, etc. |
| 50 | time_t dummyTime = 0; |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame^] | 51 | struct tm tm1 = *gmtime(&dummyTime); |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 52 | |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame^] | 53 | 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] | 54 | // Skip the time past minutes and get the float seconds. |
| 55 | double seconds; |
| 56 | sscanf(isoString + (4 + 2 + 2 + 1 + 2 + 2), "%lf", &seconds); |
| 57 | |
| 58 | // tm_year starts from 1900. |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame^] | 59 | tm1.tm_year -= 1900; |
Jeff Thompson | fdaaa58 | 2013-10-18 13:05:10 -0700 | [diff] [blame] | 60 | // tm_mon starts from 0, not 1. |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame^] | 61 | tm1.tm_mon -= 1; |
| 62 | tm1.tm_sec = 0; |
| 63 | |
| 64 | *milliseconds = (timegm(&tm1) + seconds) * 1000.0; |
| 65 | return NDN_ERROR_success; |
| 66 | #else |
| 67 | return NDN_ERROR_Time_functions_are_not_supported_by_the_standard_library; |
| 68 | #endif |
| 69 | } |