Merge lp:~chrismurf-bluefin/dccl/variable-time-codec into lp:~dccl-dev/dccl/3.0

Proposed by Chris Murphy
Status: Merged
Merged at revision: 274
Proposed branch: lp:~chrismurf-bluefin/dccl/variable-time-codec
Merge into: lp:~dccl-dev/dccl/3.0
Diff against target: 94 lines (+27/-8)
4 files modified
src/field_codec_default.h (+12/-7)
src/protobuf/option_extensions.proto (+3/-0)
src/test/dccl_header/header.proto (+6/-1)
src/test/dccl_header/test.cpp (+6/-0)
To merge this branch: bzr merge lp:~chrismurf-bluefin/dccl/variable-time-codec
Reviewer Review Type Date Requested Status
toby schneider Approve
Patrick LaRocque Pending
Review via email: mp+181098@code.launchpad.net

Description of the change

Adding a parameter to allow TimeCodec to encode times outside the +-12 hour window (24 hours centered around receive time) that it supports now. The parameter is currently named num_days - feel free to rename or request a rename. Small test case added.

I also uncovered something that I wasn't aware of - it seems like TimeCodec can't encode partial seconds? Not a problem for me now, but a surprise. See Bug 1214478.

To post a comment you must log in.
Revision history for this message
toby schneider (tes) wrote :

Clean implementation of a sensible feature. I think `num_days` is a fine name for the option extension. The only "better" way we could do it is to add units to DCCL and allow the field and max/min to have different units (e.g. seconds for the field, days for the max/min) and have the conversion done automatically.

But that's a wishlist item, so you have my approval to merge your changes without modification.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/field_codec_default.h'
2--- src/field_codec_default.h 2013-06-24 17:53:00 +0000
3+++ src/field_codec_default.h 2013-08-20 16:47:34 +0000
4@@ -234,21 +234,23 @@
5 {
6 public:
7 int32 pre_encode(const TimeType& time_of_day) {
8- return static_cast<int64>(time_of_day / conversion_factor) % SECONDS_IN_DAY;
9+ int32 max_secs = static_cast<int32>(max());
10+ return static_cast<int64>(time_of_day / conversion_factor) % max_secs;
11 }
12
13 TimeType post_decode(const int32& encoded_time) {
14+ int32 max_secs = static_cast<int32>(max());
15 timeval t;
16 gettimeofday(&t, 0);
17 int64 now = t.tv_sec;
18- int64 daystart = now - (now % SECONDS_IN_DAY);
19+ int64 daystart = now - (now % max_secs);
20 int64 today_time = now - daystart;
21
22 // If time is more than 12 hours ahead of now, assume it's yesterday.
23- if ((encoded_time - today_time) > (SECONDS_IN_DAY/2)) {
24- daystart -= SECONDS_IN_DAY;
25- } else if ((today_time - encoded_time) > (SECONDS_IN_DAY/2)) {
26- daystart += SECONDS_IN_DAY;
27+ if ((encoded_time - today_time) > (max_secs/2)) {
28+ daystart -= max_secs;
29+ } else if ((today_time - encoded_time) > (max_secs/2)) {
30+ daystart += max_secs;
31 }
32
33 return conversion_factor * (daystart + encoded_time);
34@@ -257,7 +259,10 @@
35 private:
36 void validate() { }
37
38- double max() { return SECONDS_IN_DAY; }
39+ double max() {
40+ return FieldCodecBase::dccl_field_options().num_days() * SECONDS_IN_DAY;
41+ }
42+
43 double min() { return 0; }
44 enum { SECONDS_IN_DAY = 86400 };
45 };
46
47=== modified file 'src/protobuf/option_extensions.proto'
48--- src/protobuf/option_extensions.proto 2012-10-24 15:24:12 +0000
49+++ src/protobuf/option_extensions.proto 2013-08-20 16:47:34 +0000
50@@ -38,6 +38,9 @@
51 optional double min = 5 [default = 0];
52 optional double max = 6 [default = 0];
53
54+ // time ("1 day" can encode times 12h before or after the receiver's time)
55+ optional uint32 num_days = 7 [default = 1];
56+
57 // static
58 optional string static_value = 8 [default = ""];
59
60
61=== modified file 'src/test/dccl_header/header.proto'
62--- src/test/dccl_header/header.proto 2013-06-24 17:53:00 +0000
63+++ src/test/dccl_header/header.proto 2013-08-20 16:47:34 +0000
64@@ -16,7 +16,12 @@
65 (dccl.field).in_head=true];
66 optional double time_double = 21 [(dccl.field).codec="_time",
67 (dccl.field).in_head=true];
68-
69+ optional double pasttime_double = 22 [(dccl.field).codec="_time",
70+ (dccl.field).num_days=6,
71+ (dccl.field).in_head=true];
72+ optional double futuretime_double = 23 [(dccl.field).codec="_time",
73+ (dccl.field).num_days=6,
74+ (dccl.field).in_head=true];
75
76 //
77 // source
78
79=== modified file 'src/test/dccl_header/test.cpp'
80--- src/test/dccl_header/test.cpp 2013-06-24 17:53:00 +0000
81+++ src/test/dccl_header/test.cpp 2013-08-20 16:47:34 +0000
82@@ -53,6 +53,12 @@
83 msg_in1.mutable_header()->set_time_signed(now);
84 msg_in1.mutable_header()->set_time_double(now / 1000000);
85
86+ dccl::int64 past = now / 1000000.0 - 200000; // Pick a time 2+ days in the past.
87+ dccl::int64 future = now / 1000000.0 + 200000; // Pick a time 2+ days in the future.
88+ msg_in1.mutable_header()->set_pasttime_double(past);
89+ msg_in1.mutable_header()->set_futuretime_double(future);
90+
91+
92 msg_in1.mutable_header()->set_source_platform(1);
93 msg_in1.mutable_header()->set_dest_platform(3);
94 msg_in1.mutable_header()->set_dest_type(Header::PUBLISH_OTHER);

Subscribers

People subscribed via source and target branches

to all changes: