1/*
2 * Copyright 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_AUDIO_CLOCK_H
18#define ANDROID_AUDIO_CLOCK_H
19
20// This file can be included for either C or C++ source.
21
22#include <stdint.h>
23#include <stdio.h>
24#include <sys/time.h>
25#include <time.h>
26
27// These are declared as macros for compatbility with existing uses.
28// TODO Spell out the words in full.
29#define MICROS_PER_SECOND      1000000LL
30#define MILLIS_PER_SECOND         1000LL
31#define NANOS_PER_MICROSECOND     1000LL
32#define NANOS_PER_MILLISECOND  1000000LL
33#define NANOS_PER_SECOND    1000000000LL
34
35/**
36 * \brief Converts time in ns to a time string, with format similar to logcat.
37 * \param ns          input time in nanoseconds to convert.
38 * \param buffer      caller allocated string buffer, buffer_length must be >= 19 chars
39 *                    in order to fully fit in time.  The string is always returned
40 *                    null terminated if buffer_size is greater than zero.
41 * \param buffer_size size of buffer.
42 */
43static inline void audio_utils_ns_to_string(int64_t ns, char *buffer, size_t buffer_size)
44{
45    if (buffer_size == 0) return;
46
47    const int one_second = 1000000000;
48    const time_t sec = ns / one_second;
49    struct tm tm;
50
51    // Supported on bionic, glibc, and macOS, but not mingw.
52    if (localtime_r(&sec, &tm) == NULL) {
53        buffer[0] = '\0';
54        return;
55    }
56
57    if (snprintf(buffer, buffer_size, "%02d-%02d %02d:%02d:%02d.%03d",
58        tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range
59        tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
60        (int)(ns % one_second / 1000000)) < 0) {
61        buffer[0] = '\0'; // null terminate on format error, which should not happen
62    }
63}
64
65/**
66 * An object that contains the formatted time string.
67 *
68 * The time string is 19 characters (including null termination).
69 * Example: "03-27 16:47:06.187"
70 *           MM DD HH MM SS MS
71 */
72typedef struct audio_utils_time_string {
73    char time[19]; /* minimum size buffer */
74} audio_utils_time_string_t;
75
76/**
77 * \brief Converts time in ns to a time string object, with format similar to logcat.
78 * \param ns          input time in nanoseconds to convert.
79 */
80static inline audio_utils_time_string_t audio_utils_time_string_from_ns(int64_t ns)
81{
82    audio_utils_time_string_t ts;
83
84    audio_utils_ns_to_string(ns, ts.time, sizeof(ts.time));
85    return ts;
86}
87
88/**
89 * \brief Converts a timespec to nanoseconds.
90 * \param ts   input timespec to convert.
91 * \return     timespec converted to nanoseconds.
92 */
93static inline int64_t audio_utils_ns_from_timespec(const struct timespec *ts)
94{
95    return ts->tv_sec * 1000000000LL + ts->tv_nsec;
96}
97
98/**
99 * \brief Gets the real time clock in nanoseconds.
100 * \return the real time clock in nanoseconds, or 0 on error.
101 */
102static inline int64_t audio_utils_get_real_time_ns() {
103
104#if defined(__linux__)
105
106    struct timespec now_ts;
107    if (clock_gettime(CLOCK_REALTIME, &now_ts) == 0) {
108        return audio_utils_ns_from_timespec(&now_ts);
109    }
110    return 0; // should not happen.
111
112#else
113
114    // Mac OS X compatible
115    struct timeval now_tv;
116    if (gettimeofday(&now_tv, NULL /* struct timezone * */) == 0) {
117        return now_tv.tv_sec * 1000000000LL + now_tv.tv_usec * 1000LL;
118    }
119    return 0; // should not happen.
120
121#endif
122
123}
124
125#endif  // !ANDROID_AUDIO_CLOCK_H
126