diff options
author | 2018-07-18 07:34:24 -0700 | |
---|---|---|
committer | 2018-07-18 07:34:24 -0700 | |
commit | a9f3d78c6ef4897816f696366814967fd5db2ad6 (patch) | |
tree | 5d56c5b7490b1b609f64fc01da46662362720526 /src/core/lib/gpr | |
parent | 3904de99b8539acd0957d5e5f65e1d5531fefdaf (diff) | |
parent | 2968bf687af0f5e0db591d20276b79a7fd627c31 (diff) |
Merge pull request #15980 from ncteisen/channelz-subchannel-refs
Channelz Part 3: Subchannel Refs Support for PickFirst
Diffstat (limited to 'src/core/lib/gpr')
-rw-r--r-- | src/core/lib/gpr/string.cc | 28 | ||||
-rw-r--r-- | src/core/lib/gpr/string.h | 10 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/core/lib/gpr/string.cc b/src/core/lib/gpr/string.cc index ef2a6900b4..0a76fc1f54 100644 --- a/src/core/lib/gpr/string.cc +++ b/src/core/lib/gpr/string.cc @@ -23,8 +23,10 @@ #include <ctype.h> #include <limits.h> #include <stddef.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> +#include <time.h> #include <grpc/support/alloc.h> #include <grpc/support/log.h> @@ -54,6 +56,32 @@ typedef struct { char* data; } dump_out; +char* gpr_format_timespec(gpr_timespec tm) { + char time_buffer[35]; + char ns_buffer[11]; // '.' + 9 digits of precision + struct tm* tm_info = localtime((const time_t*)&tm.tv_sec); + strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%dT%H:%M:%S", tm_info); + snprintf(ns_buffer, 11, ".%09d", tm.tv_nsec); + // This loop trims off trailing zeros by inserting a null character that the + // right point. We iterate in chunks of three because we want 0, 3, 6, or 9 + // fractional digits. + for (int i = 7; i >= 1; i -= 3) { + if (ns_buffer[i] == '0' && ns_buffer[i + 1] == '0' && + ns_buffer[i + 2] == '0') { + ns_buffer[i] = '\0'; + // Edge case in which all fractional digits were 0. + if (i == 1) { + ns_buffer[0] = '\0'; + } + } else { + break; + } + } + char* full_time_str; + gpr_asprintf(&full_time_str, "%s%sZ", time_buffer, ns_buffer); + return full_time_str; +} + static dump_out dump_out_create(void) { dump_out r = {0, 0, nullptr}; return r; diff --git a/src/core/lib/gpr/string.h b/src/core/lib/gpr/string.h index 2e8a4898d9..ce51fe4632 100644 --- a/src/core/lib/gpr/string.h +++ b/src/core/lib/gpr/string.h @@ -21,6 +21,8 @@ #include <grpc/support/port_platform.h> +#include <grpc/impl/codegen/gpr_types.h> + #include <stdbool.h> #include <stddef.h> @@ -81,6 +83,14 @@ char* gpr_strjoin_sep(const char** strs, size_t nstrs, const char* sep, void gpr_string_split(const char* input, const char* sep, char*** strs, size_t* nstrs); +/* Returns an allocated string that represents tm according to RFC-3339, and, + more specifically, follows: + https://developers.google.com/protocol-buffers/docs/proto3#json + + Uses RFC 3339, where generated output will always be Z-normalized and uses + 0, 3, 6 or 9 fractional digits. */ +char* gpr_format_timespec(gpr_timespec); + /* A vector of strings... for building up a final string one piece at a time */ typedef struct { char** strs; |