adding milliseconds to timestamps

Post Reply
Alaran
Posts: 27
Joined: Sat Dec 19, 2009 2:51 pm

adding milliseconds to timestamps

Post by Alaran »

I know there's a timestamp function in mudlet, but I'd like to append mine to the end of the line - I'm just used to that. ;)

So I figured out how to get something like "time is: 15:40:13" echo'd by using "echo("time is: " .. os.date( "%H:%M:%S." ) .. "\n")" but I'm stuck on how to append the milliseconds. Any help how I could add milliseconds please?

EDIT: Argh. And I totally wanted to post that in the help section, sorry! If anyone could move it I'd appreciate it. ;)

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: adding milliseconds to timestamps

Post by Vadi »

Need a Lua function for that and unfortunately I don't think we have it yet (just the stopwatch which works in ms, but that doesn't help for getting the current time). So it would be an API request for that atm :)

Alaran
Posts: 27
Joined: Sat Dec 19, 2009 2:51 pm

Re: adding milliseconds to timestamps

Post by Alaran »

That's a shame. Does such an API request exist yet?

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: adding milliseconds to timestamps

Post by Vadi »

no, so put down one here: http://forums.mudlet.org/viewtopic.php? ... 2&start=30

just having a getCurrentTime() which returns to a table with hours, mins, seconds and ms would be nice. In the future we could expand it to allow for 12/24h switching and such.

Iocun
Posts: 174
Joined: Wed Dec 02, 2009 1:45 am

Re: adding milliseconds to timestamps

Post by Iocun »

I think for most purposes, the stopwatch would be sufficient for this. Generally, when you want timestamps with milliseconds, you don't want that to know the exact time down to milliseconds, but to be able to read the time difference between various events. So you could have a trigger that, upon connecting to the mud, reads the current time with os.date() and stores it in a variable, then starts a stopwatch. Then, at every line it displays the sum of the stored starting time plus the currently elapsed stopwatch time. This will give you a time stamp that is accurate down to seconds regarding absolute time, but will still have accurate relative time differences between lines down to milliseconds. I.e. your timestamps may all be some milliseconds off, all by the same amount, but they will be absolutely correct in relation to each other.

But of course, a new function for this would be much more comfortable.

hempa
Posts: 48
Joined: Sat Jan 02, 2010 1:07 pm

Re: adding milliseconds to timestamps

Post by hempa »

Would it not be possible to include this:

http://www.opengroup.org/onlinepubs/000 ... ime.h.html

sys/time.h has a function called gettimeofday. This has been done before by Whyte in the MudBot core

Code: Select all

static int ilua_gettimeofday( lua_State *L )
{
   struct timeval tv;
   lua_Number timeofday;
   
   gettimeofday( &tv, NULL );
   
   /* Lowering the upper precision, so that the fractional position can fit. */
   timeofday = (lua_Number) (tv.tv_sec & 0xFFFFFF) +
               (lua_Number) tv.tv_usec / 1000000;
   
   lua_pushnumber( L, timeofday );
   
   return 1;
}
Of course, this has to be adapted for the Mudlet source, but.. It can be done!

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: adding milliseconds to timestamps

Post by Vadi »

That won't work on windows.

Regardless, I just realized I did a windows/linux version of this in code elsewhere so I'll try and implement the function. Qt might give one function to us to begin with.

hempa
Posts: 48
Joined: Sat Jan 02, 2010 1:07 pm

Re: adding milliseconds to timestamps

Post by hempa »

I'm very sure gettimeofday works on Windows, since I run my Mudbot on windows. But hey, if there's a windows/linux version of the code, that would be great.

One could also use timers, I suppose, if ther is a way to get the elapsed time from the start of a timer.

Found this example for using QTime:

Code: Select all

QTime myTimer;
myTimer.start();
// do something..
int nMilliseconds = myTimer.elapsed();

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: adding milliseconds to timestamps

Post by Vadi »

let me repeat - there is no gettimeofday on windows.

mudbot works around this by creating its own on windows:

Code: Select all

/* A poor implementation of gettimeofday, just for local use. */

int gettimeofday(struct timeval *tv, void *tz)
{
    LARGE_INTEGER frequency, lcounter;
    unsigned long long counter;

    tv->tv_sec = 0;
    tv->tv_usec = 0;

    if (!QueryPerformanceFrequency(&frequency))
        return -1;

    /* Too big for us? */
    if (frequency.HighPart)
        return -1;

    if (!frequency.LowPart /*&& !frequency.HighPart */ )
        return -1;

    if (!QueryPerformanceCounter(&lcounter))
        return -1;

    //seconds = LargeIntegerDivide( counter, frequency, NULL );
    counter = lcounter.HighPart;
    counter = counter << 32;
    counter += lcounter.LowPart;

    tv->tv_sec = (counter / frequency.LowPart);

    counter = counter % frequency.LowPart;
    counter *= 1e6;
    counter /= frequency.LowPart;

    tv->tv_usec = counter;

    return 0;
}

hempa
Posts: 48
Joined: Sat Jan 02, 2010 1:07 pm

Re: adding milliseconds to timestamps

Post by hempa »

Ahhh, I so totally missed that one. Thanks for clearing it up Vadi :)

Post Reply