3

This gnu date command lets me get milliseconds this way:

date +%M:%S.%N;

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

5 Answers5

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
  • 14,179
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.

alanc
  • 1,074
Doug Harris
  • 27,811
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.

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

Jens Erat
  • 17,897
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.