4 #include <linux/time64.h>
11 #include "time-utils.h"
13 int parse_nsec_time(const char *str, u64 *ptime)
15 u64 time_sec, time_nsec;
18 time_sec = strtoul(str, &end, 10);
19 if (*end != '.' && *end != '\0')
26 if (strlen(++end) > 9)
29 strncpy(nsec_buf, end, 9);
32 /* make it nsec precision */
33 for (i = strlen(nsec_buf); i < 9; i++)
36 time_nsec = strtoul(nsec_buf, &end, 10);
42 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
46 static int parse_timestr_sec_nsec(struct perf_time_interval *ptime,
47 char *start_str, char *end_str)
49 if (start_str && (*start_str != '\0') &&
50 (parse_nsec_time(start_str, &ptime->start) != 0)) {
54 if (end_str && (*end_str != '\0') &&
55 (parse_nsec_time(end_str, &ptime->end) != 0)) {
62 int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr)
64 char *start_str, *end_str;
68 if (ostr == NULL || *ostr == '\0')
71 /* copy original string because we need to modify it */
79 /* str has the format: <start>,<stop>
80 * variations: <start>,
85 d = strchr(start_str, ',');
92 rc = parse_timestr_sec_nsec(ptime, start_str, end_str);
96 /* make sure end time is after start time if it was given */
97 if (rc == 0 && ptime->end && ptime->end < ptime->start)
100 pr_debug("start time %" PRIu64 ", ", ptime->start);
101 pr_debug("end time %" PRIu64 "\n", ptime->end);
106 bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp)
108 /* if time is not set don't drop sample */
112 /* otherwise compare sample time to time window */
113 if ((ptime->start && timestamp < ptime->start) ||
114 (ptime->end && timestamp > ptime->end)) {
121 int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
123 u64 sec = timestamp / NSEC_PER_SEC;
124 u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
126 return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
129 int fetch_current_timestamp(char *buf, size_t sz)
135 if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
138 if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
141 scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);