Skip to content

Commit 620e14e

Browse files
abulimovfacebook-github-bot
authored andcommitted
introduce fbclock_gettime_utc
Summary: Add method to get time in UTC. For now, simply apply known offset. Reviewed By: leoleovich Differential Revision: D51116026 fbshipit-source-id: 8ad1e08eb9b0f7cb0c103c6cfc4becc98575cb81
1 parent e5d65c6 commit 620e14e

4 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎fbclock/cpp_test/test.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ TEST(fbclock_test, test_fbclock_calculate_time) {
193193
EXPECT_EQ(truetime.latest_ns, 1647290691804195223);
194194
}
195195

196+
TEST(fbclock_test, test_fbclock_apply_utc_offset) {
197+
int err;
198+
fbclock_truetime truetime;
199+
truetime.earliest_ns = 1647269091803102338;
200+
truetime.latest_ns = 1647269091803103576;
201+
202+
fbclock_apply_utc_offset(&truetime);
203+
204+
EXPECT_EQ(truetime.earliest_ns, 1647269054803102338);
205+
EXPECT_EQ(truetime.latest_ns, 1647269054803103576);
206+
}
207+
196208
int main(int argc, char** argv) {
197209
::testing::InitGoogleTest(&argc, argv);
198210
return RUN_ALL_TESTS();

‎fbclock/fbclock.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,24 @@ int fbclock_gettime(fbclock_lib* lib, fbclock_truetime* truetime) {
221221
error_bound, h_value, state.ingress_time_ns, res.ts, truetime);
222222
}
223223

224+
void fbclock_apply_utc_offset(fbclock_truetime* truetime) {
225+
truetime->earliest_ns += UTC_TAI_OFFSET;
226+
truetime->latest_ns += UTC_TAI_OFFSET;
227+
}
228+
229+
// When/if new leap second is announced, we would need to update this function
230+
// to do smearing, and fetch offset from fbclock daemon.
231+
// For now, we apply the offset, as leap seconds will be abandoned by 2035
232+
// and it is possible we won't have any at all.
233+
int fbclock_gettime_utc(fbclock_lib* lib, fbclock_truetime* truetime) {
234+
int rcode = fbclock_gettime(lib, truetime);
235+
if (rcode != 0) {
236+
return rcode;
237+
}
238+
fbclock_apply_utc_offset(truetime);
239+
return 0;
240+
}
241+
224242
const char* fbclock_strerror(int err_code) {
225243
const char* err_info = "unknown error";
226244
switch (err_code) {

‎fbclock/fbclock.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,17 @@ func (f *FBClock) GetTime() (*TrueTime, error) {
8787

8888
return &TrueTime{Earliest: earliest, Latest: latest}, nil
8989
}
90+
91+
// GetTimeUTC returns TrueTime in UTC
92+
func (f *FBClock) GetTimeUTC() (*TrueTime, error) {
93+
tt := &C.fbclock_truetime{}
94+
errCode := C.fbclock_gettime_utc(f.cFBClock, tt)
95+
if errCode != 0 {
96+
return nil, fmt.Errorf("reading FBClock TrueTime UTC: %s", strerror(errCode))
97+
}
98+
99+
earliest := time.Unix(0, int64(tt.earliest_ns))
100+
latest := time.Unix(0, int64(tt.latest_ns))
101+
102+
return &TrueTime{Earliest: earliest, Latest: latest}, nil
103+
}

‎fbclock/fbclock.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ typedef atomic_uint_fast64_t atomic_uint64;
3535
#define FBCLOCK_E_WOU_TOO_BIG -6
3636
#define FBCLOCK_E_PHC_IN_THE_PAST -7
3737

38+
// until new leap second is introduced, UTC is exactly 37 seconds behind TAI
39+
#define UTC_TAI_OFFSET (int64_t)(-37e9)
40+
3841
#ifdef __cplusplus
3942
extern "C" {
4043
#endif
@@ -85,11 +88,13 @@ int fbclock_calculate_time(
8588
int64_t ingress_time_ns,
8689
int64_t phctime_ns,
8790
fbclock_truetime* truetime);
91+
void fbclock_apply_utc_offset(fbclock_truetime* truetime);
8892

8993
// methods we provide to end users
9094
int fbclock_init(fbclock_lib* lib, const char* shm_path);
9195
int fbclock_destroy(fbclock_lib* lib);
9296
int fbclock_gettime(fbclock_lib* lib, fbclock_truetime* truetime);
97+
int fbclock_gettime_utc(fbclock_lib* lib, fbclock_truetime* truetime);
9398
// turn error code into err msg
9499
const char* fbclock_strerror(int err_code);
95100

0 commit comments

Comments
 (0)