This gnu date command lets me get milliseconds this way:
date +%M:%S.%N;
but this command doesn't work on solaris... any ideas?
This gnu date command lets me get milliseconds this way:
date +%M:%S.%N;
but this command doesn't work on solaris... any ideas?
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.
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.
coreutils under your home directory. You can also try Aaron Digulla's perl suggestion.
– Doug Harris
Aug 20 '10 at 14:31
$ type -a date
date is /usr/bin/date
date is /bin/date
– andersonbd1
Aug 20 '10 at 14:39
./configure --prefix=/home/myuser. Coreutils files were installed in bin, lib and share subdirectories under my home directory.
– Doug Harris
Aug 20 '10 at 14:53
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.
#!/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
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.
time command for that matter.
– Aaron Digulla
Aug 20 '10 at 14:49