aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_Time.c
blob: 07ab185b877aae12b8f5e9d39f830081ed52f1f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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 <parc/assert/parc_Assert.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);
    parcAssertTrue(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;
}