From ec688b4723a041044226358bcd4dd6e2da39da49 Mon Sep 17 00:00:00 2001 From: Luca Muscariello Date: Thu, 23 Feb 2017 17:01:02 +0100 Subject: Initial commit: cframework. Longbow and Libparc Change-Id: I90378dbd30da6033b20fb1f829b3b822cf366c59 Signed-off-by: Luca Muscariello --- libparc/parc/algol/parc_Time.c | 154 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100755 libparc/parc/algol/parc_Time.c (limited to 'libparc/parc/algol/parc_Time.c') diff --git a/libparc/parc/algol/parc_Time.c b/libparc/parc/algol/parc_Time.c new file mode 100755 index 00000000..7cc358b9 --- /dev/null +++ b/libparc/parc/algol/parc_Time.c @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + */ +#include + +#include +#include +#include +#include + +#include + +#include +#include + +char * +parcTime_TimevalAsString(struct timeval timeval) +{ + char *string; + int nwritten = asprintf(&string, "%ld.%06ld", timeval.tv_sec, (long) timeval.tv_usec); + assertTrue(nwritten >= 0, "Error calling asprintf"); + + char *result = parcMemory_StringDuplicate(string, strlen(string)); + free(string); + return result; +} + +char * +parcTime_TimevalAsRFC3339(const struct timeval *utcTime, char result[64]) +{ + char tmbuf[64]; + struct tm theTime; + + struct tm *nowtm = gmtime_r(&utcTime->tv_sec, &theTime); + strftime(tmbuf, sizeof tmbuf, "%Y-%m-%dT%H:%M:%S", nowtm); + snprintf(result, 64, "%s.%06ldZ", tmbuf, (long) utcTime->tv_usec); + return result; +} + +char * +parcTime_TimevalAsISO8601(const struct timeval *utcTime, char result[64]) +{ + char tmbuf[64]; + struct tm theTime; + + struct tm *nowtm = gmtime_r(&utcTime->tv_sec, &theTime); + strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm); + snprintf(result, 64, "%s.%06ldZ", tmbuf, (long) utcTime->tv_usec); + return result; +} + +char * +parcTime_TimeAsRFC3339(const time_t utcTime, char result[64]) +{ + struct timeval theTime = { utcTime, 0 }; + + return parcTime_TimevalAsRFC3339(&theTime, result); +} + +char * +parcTime_NowAsRFC3339(char result[64]) +{ + struct timeval theTime; + gettimeofday(&theTime, NULL); + + return parcTime_TimevalAsRFC3339(&theTime, result); +} + +char * +parcTime_TimeAsISO8601(const time_t utcTime, char result[64]) +{ + struct timeval theTime = { utcTime, 0 }; + + return parcTime_TimevalAsISO8601(&theTime, result); +} + +char * +parcTime_NowAsISO8601(char result[64]) +{ + struct timeval theTime; + gettimeofday(&theTime, NULL); + + return parcTime_TimevalAsISO8601(&theTime, result); +} + +struct timeval +parcTime_TimevalAdd(const struct timeval *addend1, const struct timeval *addend2) +{ + struct timeval sum; + + sum.tv_sec = addend1->tv_sec + addend2->tv_sec; + sum.tv_usec = addend1->tv_usec + addend2->tv_usec; + if (sum.tv_usec >= 1000000) { + sum.tv_usec -= 1000000; + sum.tv_sec++; + } + return sum; +} + +struct timeval +parcTime_TimevalSubtract(const struct timeval *minuend, const struct timeval *subtrahend) +{ + struct timeval result; + + result.tv_sec = minuend->tv_sec - subtrahend->tv_sec; + result.tv_usec = minuend->tv_usec - subtrahend->tv_usec; + if (result.tv_usec < 0) { + result.tv_sec--; + result.tv_usec += 1000000; + } + return result; +} + +struct timeval +parcTime_NowTimeval(void) +{ + struct timeval timeval; + gettimeofday(&timeval, NULL); + return timeval; +} + +uint64_t +parcTime_NowMicroseconds(void) +{ + struct timeval timeval; + gettimeofday(&timeval, NULL); + + uint64_t result = timeval.tv_sec * 1000000 + timeval.tv_usec; + return result; +} + +uint64_t +parcTime_NowNanoseconds(void) +{ + struct timeval timeval; + gettimeofday(&timeval, NULL); + + uint64_t result = timeval.tv_sec * 1000000000 + timeval.tv_usec * 1000; + return result; +} -- cgit 1.2.3-korg