# Writing a program to find the absolute time from microsecond timestamps

Hi Guys,
I have a large txt data and I got it with textread function. Inside the data a column is time stamp in microseconds. It is iterative rather cumulative. it looks like below:
0.91473
0.91512
.
.
.
0.99893
0.99932
0.99971
9.3e-05
0.000481
0.000871
.
.
.
I need to find the absolute seconds such as 57.987878. I managed to program it to find seconds but I could not add the seconds in front of microsesonds. Can somebody help me?

Hi seyasar, welcome to Octave discourse group!

I guess you want to separate seconds and microseconds, for that you only need to consider that a second is 10^6 microseconds.
e.g.

``````a = 57.987878

a_seconds = floor(a);
a_microseconds = 1e6*(a - a_seconds);
fprintf("time: %d seconds and %.1f microseconds\n",a_seconds,a_microseconds);
``````

resulting in:

time: 57 seconds and 987878.0 microseconds

Can you clarify what you meant by

but I could not add the seconds in front of microsesonds.

Can you add a sample of the desired output (calculated manually) for the first 5 lines of the input ?

1 Like

Hi there, thank you very much for welcome and reply.
I mean the problem is that I donât have 57.98787. I only have microseconds from 0 to 1.000.000. But at total I have 57 seconds, that means I have 57 times 0 to 1.000.000. I have to show them as normal timestamp as 57.987878.

Hi thank you for the response.
for the first second it is present from 0.9 to 1 (actual data is like 954578 ms I transfromed it to 0.954578)
for the other seconds I nedd e.g. 1.01 to 1.99 but it is again shown as 0.01 to 0.99.

Actual data (in seconds):

0.99893
0.99932
0.99971
9.3e-05
0.000481
0.000871
.
.
.
0.97878

What I desire:

0.99893
0.99932
0.99971
1.000093
1.000481
1.000871
.
.
.
57.97878
Thanks

I think what youâre looking for is cumsum function

``````vector = 1:9;
cumsum(vector)
``````

ans =

``1    3    6   10   15   21   28   36   45``

Not really. I dont need a cumulative sum. I need to add seconds in front of decimals.
1.ababa
2.ababa
3.ababa
.
.
.
100.ababa

It wolud probably help to see more than one column if you have other relevant data in those columns.

by my incomplete understandnig, for some reason you have the following situation:

``````REALTIME     YOURDATA
1.023456        0.023456
1.123585        0.123585
1.123988        0.123988
1.124564        0.124564
2.004561        0.004561
2.123456        0.123456
...
``````

Is that correct? is the whole-number second recorded anywhere? Are you just asking how to combine those two columns to get back to the real time? Or is that information not at all recorded? (and that alone is a problem you should probably seek to fix on the data acquisition side).

In any case, if the latter, can you guarantee that there are timesteps in every second, and that you will always have a negative âdeltaâ when it crosses into a new whole number second? e.g., the following becomes ambiguous

``````REALTIME     YOURDATA
1.023456        0.023456
1.123585        0.123585
2.223585        0.223585
``````

if you can be certain that wonât happen, then a counter and `diff` could probably be used to calculate what the whole second value should be, and then append that to the microsecond data.
need some more clarity.

Yeah, some variation of `cumsum([0; (diff(tt)<0)]) + tt` (where `tt` is a column vector with the fractional part of your seconds) is probably what you could use.

1 Like

This is just a âphase unwrappingâ problem. âunwrapâ from signal package might be helpful.

1 Like

Hi there,
the seconds are not recorded additionally somewhere. First I have to count seconds which I did with findpeaks function with locations. but adding the corresponding second to the corresponding microsecond, I could not do it yet.

Yes it can be useful, I will try it.

Thanks a lot now I got the answer. SOLVED