aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/gpr
diff options
context:
space:
mode:
authorGravatar Noah Eisen <ncteisen@gmail.com>2018-07-18 07:34:24 -0700
committerGravatar GitHub <noreply@github.com>2018-07-18 07:34:24 -0700
commita9f3d78c6ef4897816f696366814967fd5db2ad6 (patch)
tree5d56c5b7490b1b609f64fc01da46662362720526 /src/core/lib/gpr
parent3904de99b8539acd0957d5e5f65e1d5531fefdaf (diff)
parent2968bf687af0f5e0db591d20276b79a7fd627c31 (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.cc28
-rw-r--r--src/core/lib/gpr/string.h10
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;