[Kamailio-Users] Question about Timer, i need milliseconds

Alex Balashov abalashov at evaristesys.com
Wed Jan 20 18:10:54 CET 2010


Marco,

On 01/20/2010 10:53 AM, Marco Bungalski wrote:

> Hello all :-)
>
> I have a question about how to get a time-stamps with milliseconds. To
> create my own CDR's, i write to my databases timestamps, with a command
> like this:
>
> sql_query("ca", "UPDATE active_calls SET state = 'BILLING', end_time=$Ts
> WHERE call_id='$ci'", "ra");
>
> I use the "$Ts"-variable, but with this i can get only "full" seconds.
> But i need milliseconds for my billing-system. Otherwise i have
> differences between my CDR's and my carrier's one.
>
> Example: A call has a duration of 0.2 seconds. In my CDR start- and
> endtime is the same (=0 seconds), to my carrier a have 1 second to pay ;-)
>
> Is there a possibility to get a time-stamp or time with milliseconds?

I do not know of any pseudovariables in Kamailio that expose the wall 
clock time in milliseconds.

One thing you have to keep in mind, though, is that even if such a 
facility were available - for example, it would be relatively simple to 
expose via pseudovariables the microsecond values returned by 
gettimeofday() down to the nearest millisecond - it would not always be 
so precise.  The accuracy and resolution of userspace clocks can be 
distorted and unpredictable and depends on many things.  While this 
would probably not affect you if you're interested in a 1/10th second 
granularity at most, it is still something to consider.

To solve your problem, I suggest that you not update your records with 
values from $Ts.  Instead, do it as the 'acc' module does and use the 
wall clock time from the database itself, which does go down to the 
sub-second level in the manner suggested by ISO timestamps.

The function NOW() is available in both MySQL and PostgreSQL, and 
probably most other RDBMs.  It returns a date string in the native 
timestamp format used within those RDBMs.  In Postgres, which I am most 
familiar with, this type is 'timestamp'. I think in MySQL it may be 
called something else.

So, for example, on 200 OK/pickup:

    UPDATE cdr SET start_time = NOW(), ... WHERE call_id = $ci;

And on BYE:

    UPDATE cdr SET end_time = NOW(), ... WHERE call_id = $ci;

Making the calculation (end_time - start_time) within the database, cast 
and rounded appropriately by various mathematical functions provided 
therein, would allow you to do the rounding up and/or the application of 
fractional increments you're looking for.

I personally use triggers and stored procedures on the event rows 
produced by the 'acc' module to generate my own CDRs in my own desired 
format from this data, using the 'db_extra' parameter to pass through 
any other values I want from the proxy.  I do this in PostgreSQL;  I do 
not know the practicality of this in MySQL.

-- 
Alex Balashov - Principal
Evariste Systems LLC

Tel    : +1 678-954-0670
Direct : +1 678-954-0671
Web    : http://www.evaristesys.com/



More information about the Users mailing list