# [OpenSIPS-Users] time going backwards?

Kingsley Tart kingsley at dns99.co.uk
Mon Jul 12 13:53:00 EST 2021

```Gah, thanks - I was having a mega brain fart, clearly. I even already
had this function in my config (!!) ... :-/

BTW, apologies for the lateness of my reply - I've been on holiday.

route[get_timestamp_hires] {
\$var(N25_resultAVP) = \$param(1);
# returns a floating point number as a string "\$seconds.\$decimals" representing the timestamp
# accurate to a millisecond. Because \$Ts and \$Tsm are separate variabls in OpenSIPS, there is
# a tiny chance that \$Ts might advance to the next clock second while reading \$Tsm, which could
# result in a reported timestamp almost exactly a second wrong.
# Therefore, here we generate a timestamp string where we mitigate against that possibility.

\$var(N25_Ts) = \$Ts;
\$var(N25_Tsm) = \$Tsm/1000;
if (\$Ts > \$var(N25_Ts)) {
# clock has advanced, take new values
\$var(N25_Ts) = \$Ts;
\$var(N25_Tsm) = \$Tsm/1000;
}
if (\$var(N25_Tsm) < 10) {
\$var(N25_Tsm) = "00" + \$var(N25_Tsm);
} else if (\$var(N25_Tsm) < 100) {
\$var(N25_Tsm) = "0" + \$var(N25_Tsm);
}

\$avp(\$var(N25_resultAVP)) = "" + \$var(N25_Ts) + "." + \$var(N25_Tsm);
}

Cheers,
Kingsley.

On Sat, 2021-06-26 at 19:37 +0200, Gregory Massel wrote:
> Per https://www.opensips.org/Documentation/Script-CoreVar-3-1
> \$Tsm - reference to current microseconds of the current second
> This means that if the second counter cycles up, the microsecond counter will cycle back to zero and you'll get a negative.
> e.g. Let's assume that at the time of the first reading of \$Tsm it is 12:00:00.99999 and, at the time of the second reading, it's 12:00:01.00003, then you'll get a result of -0.99996, because you're looking at the fraction of a second in isolation to the second counter.
> So you probably need something like:
> 	\$var(t0s) = \$Ts;
> 	\$var(t0ms) = \$Tsm;
> 	\$var(node_number) = route(get_my_node_number);
> 	\$var(dur) = ((\$Ts - \$var(t0s))*1000000) + \$Tsm - \$var(t0ms);
>
> Note: I'm not sure whether the syntax above is correct (I've never tried to do arithmetic operations within OpenSIPS), but the basic idea is there.
> --Greg
> On 2021-06-25 22:01, Kingsley Tart wrote:
> > Hi,
> >
> > For testing, this code at the top of my route script:
> >
> > 	\$var(t0) = \$Tsm;
> > 	\$var(node_number) = route(get_my_node_number);
> > 	\$var(dur) = \$Tsm - \$var(t0);
> > 	xlog("My node number is \$var(node_number) and it took \$var(dur) microsecconds to find out\n");
> >
> > has just logged this:
> >
> > 	Jun 25 20:55:01 [598]: My node number is -1 and it took 65 microsecconds to find out
> > 	Jun 25 20:55:03 [598]: My node number is -1 and it took 67 microsecconds to find out
> > 	Jun 25 20:55:05 [598]: My node number is -1 and it took 64 microsecconds to find out
> > 	Jun 25 20:55:05 [578]: My node number is -1 and it took 602523 microsecconds to find out
> > 	Jun 25 20:55:06 [579]: My node number is -1 and it took -79396 microsecconds to find out
> > 	Jun 25 20:55:07 [598]: My node number is -1 and it took 63 microsecconds to find out
> > 	Jun 25 20:55:07 [580]: My node number is -1 and it took -455503 microsecconds to find out
> > 	Jun 25 20:55:07 [579]: My node number is -1 and it took 108 microsecconds to find out
> > 	Jun 25 20:55:09 [598]: My node number is -1 and it took 78 microsecconds to find out
> > 	Jun 25 20:55:11 [598]: My node number is -1 and it took 68 microsecconds to find out
> > 	Jun 25 20:55:13 [598]: My node number is -1 and it took 66 microsecconds to find out
> > 	Jun 25 20:55:15 [598]: My node number is -1 and it took 68 microsecconds to find out
> > 	Jun 25 20:55:17 [598]: My node number is -1 and it took 65 microsecconds to find out
> > 	Jun 25 20:55:18 [578]: My node number is -1 and it took 107 microsecconds to find out
> > 	Jun 25 20:55:19 [598]: My node number is -1 and it took 71 microsecconds to find out
> >
> > which is weird because time seems to have gone backwards a couple of times.
> >
> > Using OpenSIPS 3.1.2 in a VM running Debian 10, with the host being a
> > CentOS 7 machine running Qemu KVM.
> >
> > Any idea what's happening here?
> >
> > Cheers,
> > Kingsley.
> >
> >
> > _______________________________________________
> > Users mailing list
> > Users at lists.opensips.org
> > http://lists.opensips.org/cgi-bin/mailman/listinfo/users
>
> _______________________________________________
> Users mailing list
> Users at lists.opensips.org
> http://lists.opensips.org/cgi-bin/mailman/listinfo/users

```