diff options
author | Michele Papalini <micpapal+fdio@cisco.com> | 2017-02-24 08:00:33 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@fd.io> | 2017-02-24 08:00:33 +0000 |
commit | 4df7f4cc98b6288177df256e1db70ddc3f7d00db (patch) | |
tree | 55e71277b419e4830ae641868ab8e751c8b86972 /libparc/parc/algol/parc_Time.c | |
parent | f28308bd99381ef5f1e178e2e1f870f245e35873 (diff) | |
parent | ec688b4723a041044226358bcd4dd6e2da39da49 (diff) |
Merge "Initial commit: cframework. Longbow and Libparc" into cframework/master
Diffstat (limited to 'libparc/parc/algol/parc_Time.c')
-rwxr-xr-x | libparc/parc/algol/parc_Time.c | 154 |
1 files changed, 154 insertions, 0 deletions
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 <config.h> + +#include <stdio.h> +#include <string.h> +#include <time.h> +#include <sys/time.h> + +#include <LongBow/runtime.h> + +#include <parc/algol/parc_Time.h> +#include <parc/algol/parc_Memory.h> + +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; +} |