Change behaviour of stream-intersection with multiple traces
[babeltrace.git] / formats / ctf / ir / clock.c
index 2e765430f66d8ad590e8dfbff01777fc62a3963f..9256bc1f26f58ef5cb4f04a8ca9a92d32dc364e7 100644 (file)
@@ -325,6 +325,19 @@ end:
        return ret;
 }
 
+uint64_t ns_from_value(uint64_t frequency, uint64_t value)
+{
+       uint64_t ns;
+
+       if (frequency == 1000000000) {
+               ns = value;
+       } else {
+               ns = (uint64_t) ((1e9 * (double) value) / (double) frequency);
+       }
+
+       return ns;
+}
+
 int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time)
 {
        int ret = 0;
@@ -343,13 +356,7 @@ int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time)
                goto end;
        }
 
-       /* Common case where cycles are actually nanoseconds */
-       if (clock->frequency == 1000000000) {
-               *time = (int64_t) clock->value;
-       } else {
-               *time = (int64_t) ((1e9 * (double) clock->value) /
-                       (double) clock->frequency);
-       }
+       *time = (int64_t) ns_from_value(clock->frequency, clock->value);
 
 end:
        return ret;
@@ -358,6 +365,7 @@ end:
 int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time)
 {
        int ret = 0;
+       int64_t value;
 
        /* Timestamps are strictly monotonic */
        if (!clock) {
@@ -377,13 +385,13 @@ int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time)
 
        /* Common case where cycles are actually nanoseconds */
        if (clock->frequency == 1000000000) {
-               clock->value = time;
-               goto end;
+               value = time;
+       } else {
+               value = (uint64_t) (((double) time *
+                       (double) clock->frequency) / 1e9);
        }
 
-       ret = bt_ctf_clock_set_value(clock,
-               (uint64_t) (((double) time * (double) clock->frequency) / 1e9));
-
+       ret = bt_ctf_clock_set_value(clock, value);
 end:
        return ret;
 }
@@ -512,3 +520,24 @@ void bt_ctf_clock_destroy(struct bt_object *obj)
 
        g_free(clock);
 }
+
+int64_t bt_ctf_clock_ns_from_value(struct bt_ctf_clock *clock, uint64_t value)
+{
+       int64_t ns = -1ULL;
+
+       if (!clock) {
+               goto end;
+       }
+
+       /* Initialize nanosecond timestamp to clock's offset in seconds */
+       ns = clock->offset_s * 1000000000;
+
+       /* Add offset in cycles, converted to nanoseconds */
+       ns += ns_from_value(clock->frequency, clock->offset);
+
+       /* Add given value, converter to nanoseconds */
+       ns += ns_from_value(clock->frequency, value);
+
+end:
+       return ns;
+}
This page took 0.035081 seconds and 4 git commands to generate.