2 * Copyright 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_AUDIO_CLOCK_H
18 #define ANDROID_AUDIO_CLOCK_H
20 // This file can be included for either C or C++ source.
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
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.
43 static inline void audio_utils_ns_to_string(int64_t ns, char *buffer, size_t buffer_size)
45 if (buffer_size == 0) return;
47 const int one_second = 1000000000;
48 const time_t sec = ns / one_second;
51 // Supported on bionic, glibc, and macOS, but not mingw.
52 if (localtime_r(&sec, &tm) == NULL) {
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
66 * An object that contains the formatted time string.
68 * The time string is 19 characters (including null termination).
69 * Example: "03-27 16:47:06.187"
72 typedef struct audio_utils_time_string {
73 char time[19]; /* minimum size buffer */
74 } audio_utils_time_string_t;
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.
80 static inline audio_utils_time_string_t audio_utils_time_string_from_ns(int64_t ns)
82 audio_utils_time_string_t ts;
84 audio_utils_ns_to_string(ns, ts.time, sizeof(ts.time));
89 * \brief Converts a timespec to nanoseconds.
90 * \param ts input timespec to convert.
91 * \return timespec converted to nanoseconds.
93 static inline int64_t audio_utils_ns_from_timespec(const struct timespec *ts)
95 return ts->tv_sec * 1000000000LL + ts->tv_nsec;
99 * \brief Gets the real time clock in nanoseconds.
100 * \return the real time clock in nanoseconds, or 0 on error.
102 static inline int64_t audio_utils_get_real_time_ns() {
104 #if defined(__linux__)
106 struct timespec now_ts;
107 if (clock_gettime(CLOCK_REALTIME, &now_ts) == 0) {
108 return audio_utils_ns_from_timespec(&now_ts);
110 return 0; // should not happen.
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;
119 return 0; // should not happen.
125 #endif // !ANDROID_AUDIO_CLOCK_H