milliseconds from solaris command line

3

1

This gnu date command lets me get milliseconds this way:

date +%M:%S.%N;

but this command doesn't work on solaris... any ideas?

andersonbd1

Posted 2010-08-20T14:10:10.120

Reputation: 485

Answers

5

This "shell script" should display milliseconds:

#!/bin/ksh
if [ ! -x /var/tmp/time_ms ]
then
    cat > /tmp/time_ms.c << %
    #include <sys/time.h>
    main()
    {
        struct timeval tv;
        gettimeofday(&tv,(void*)0);
        printf("%d.%d\n",tv.tv_sec,tv.tv_usec/1000);
    }
%
    PATH=$PATH:/usr/sfw gcc /tmp/time_ms.c -o /var/tmp/time_ms
fi
/var/tmp/time_ms

Of course, you can relocate time_ms in your PATH and call it directly after the first run. That will provide a faster solution than gnu date or any perl/whatever script.

jlliagre

Posted 2010-08-20T14:10:10.120

Reputation: 12 469

hey hey - this looks like a nice idea, jiliagre. However, I'm getting this error: ./time_ms.sh[2]: syntax error at line 4 : `<<' unmatched any ideas? – andersonbd1 – 2010-08-24T13:23:02.337

I fixed my script for it to work with legacy ksh and others shells. It was working fine with ksh93 so I didn't notice. – jlliagre – 2010-08-24T14:16:35.520

yep - very cool. – andersonbd1 – 2010-08-25T17:53:12.463

2

The most likely explanation is that Solaris doesn't use GNU date or doesn't have the latest version. The Solaris man page for date doesn't mention the %N formatting option. The GNU coreutils docs for time conversion specifiers for date specifically say that %N is a GNU extension (oh -- it's nanoseconds, not milliseconds).

If you need the milliseconds, your best bet is to download the latest GNU coreutils and install it under /usr/local (or /opt/local if you prefer). To get the proper version of date, you either configure your PATH so that /usr/local/bin comes before /usr/bin or use a full path to /usr/local/bin/date in your script.

Doug Harris

Posted 2010-08-20T14:10:10.120

Reputation: 23 578

I'm not the sysadmin. Is there no other solaris command to do it? – andersonbd1 – 2010-08-20T14:29:57.580

I'm pretty sure you can install coreutils under your home directory. You can also try Aaron Digulla's perl suggestion. – Doug Harris – 2010-08-20T14:31:38.417

What do you get if you type this in bash: type -a date? – Doug Harris – 2010-08-20T14:34:14.590

I'm trying to put coreutils in my home directory now...

    $ type -a date
    date is /usr/bin/date
    date is /bin/date
 – andersonbd1  – 2010-08-20T14:39:02.457

1I tried this as well. I did ./configure --prefix=/home/myuser. Coreutils files were installed in bin, lib and share subdirectories under my home directory. – Doug Harris – 2010-08-20T14:53:45.830

1

If you're measuring the time taken by a part of the script, you can put this part of the script in a function and call time myfunction. You can also call the times builtin before and after the section you want to time, but doing the arithmetic yourself is a bit of a pain.

Gilles 'SO- stop being evil'

Posted 2010-08-20T14:10:10.120

Reputation: 58 319

1

#!/usr/bin/bash

TSP_MSEC=`perl -MTime::HiRes -e 'print int(1000 * Time::HiRes::gettimeofday),"\n"'`
MSEC=`echo $TSP_MSEC | cut -c11-13`

TSP=`date +%d.%m.20%y" "%H:%M:%S.$MSEC`
echo $TSP

Result: 15.07.2014 16:10:01.260

Dr. Michael Bobsien

Posted 2010-08-20T14:10:10.120

Reputation: 11

0

Perl is almost everywhere. Use:

perl -e 'print time()*1000;print "\n";'

If you really need millisecond accuracy, try the Time:HiRes module.

[EDIT] Alternatively, compile a small C program which calls gettimeofday() and prints the result.

Aaron Digulla

Posted 2010-08-20T14:10:10.120

Reputation: 6 035

I'm measuring milliseconds inside of a bash script. I'm guessing that tarting the perl interpreter would take enough time to throw off my calculations? – andersonbd1 – 2010-08-20T14:31:43.333

1Then use gettimeofday() directly. Or the time command for that matter. – Aaron Digulla – 2010-08-20T14:49:19.317