Merge pull request #367 from haberman/timestamp-minute-offset

Support non-zero minutes in the timestamp offset for JSON.
pull/13171/head
Joshua Haberman 4 years ago committed by GitHub
commit b080659eee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      tests/bindings/lua/BUILD
  2. 6
      tests/bindings/lua/test.proto
  3. 5
      tests/bindings/lua/test_upb.lua
  4. 10
      upb/json_decode.c

@ -37,6 +37,7 @@ proto_library(
name = "test_proto", name = "test_proto",
testonly = 1, testonly = 1,
srcs = ["test.proto"], srcs = ["test.proto"],
deps = ["@com_google_protobuf//:timestamp_proto"],
) )
lua_proto_library( lua_proto_library(

@ -1,6 +1,8 @@
syntax = "proto2"; syntax = "proto2";
import "google/protobuf/timestamp.proto";
package upb_test; package upb_test;
message MapTest { message MapTest {
@ -26,3 +28,7 @@ message UnpackedTest {
message TestLargeFieldNumber { message TestLargeFieldNumber {
optional int32 i32 = 456214797; optional int32 i32 = 456214797;
} }
message TestTimestamp {
optional google.protobuf.Timestamp ts = 1;
}

@ -714,6 +714,11 @@ function test_large_field_number()
assert_equal(msg.i32, msg2.i32) assert_equal(msg.i32, msg2.i32)
end end
function test_timestamp_minutes()
local msg = upb.json_decode(upb_test.TestTimestamp, '{"ts": "2000-01-01T00:00:00-06:59"}')
assert_equal(msg.ts.seconds, 946684800 + ((6 * 60) + 59) * 60)
end
function test_gc() function test_gc()
local top = test_messages_proto3.TestAllTypesProto3() local top = test_messages_proto3.TestAllTypesProto3()
local n = 100 local n = 100

@ -1059,7 +1059,8 @@ static void jsondec_timestamp(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
{ {
/* [+-]08:00 or Z */ /* [+-]08:00 or Z */
int ofs = 0; int ofs_hour = 0;
int ofs_min = 0;
bool neg = false; bool neg = false;
if (ptr == end) goto malformed; if (ptr == end) goto malformed;
@ -1070,9 +1071,10 @@ static void jsondec_timestamp(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
/* fallthrough */ /* fallthrough */
case '+': case '+':
if ((end - ptr) != 5) goto malformed; if ((end - ptr) != 5) goto malformed;
ofs = jsondec_tsdigits(d, &ptr, 2, ":00"); ofs_hour = jsondec_tsdigits(d, &ptr, 2, ":");
ofs *= 60 * 60; ofs_min = jsondec_tsdigits(d, &ptr, 2, NULL);
seconds.int64_val += (neg ? ofs : -ofs); ofs_min = ((ofs_hour * 60) + ofs_min) * 60;
seconds.int64_val += (neg ? ofs_min : -ofs_min);
break; break;
case 'Z': case 'Z':
if (ptr != end) goto malformed; if (ptr != end) goto malformed;

Loading…
Cancel
Save