Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

This gnu date command lets me get milliseconds this way:

date +%M:%S.%N;

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

share|improve this question

5 Answers 5

up vote 4 down vote accepted

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.

share|improve this answer
    
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 Aug 24 '10 at 13:23
    
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 Aug 24 '10 at 14:16
    
yep - very cool. –  andersonbd1 Aug 25 '10 at 17:53

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.

share|improve this answer
    
I'm not the sysadmin. Is there no other solaris command to do it? –  andersonbd1 Aug 20 '10 at 14:29
    
I'm pretty sure you can install coreutils under your home directory. You can also try Aaron Digulla's perl suggestion. –  Doug Harris Aug 20 '10 at 14:31
    
What do you get if you type this in bash: type -a date? –  Doug Harris Aug 20 '10 at 14:34
    
I'm trying to put coreutils in my home directory now... $ type -a date date is /usr/bin/date date is /bin/date –  andersonbd1 Aug 20 '10 at 14:39
1  
I 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 Aug 20 '10 at 14:53
#!/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

share|improve this answer

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.

share|improve this answer
    
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 Aug 20 '10 at 14:31
1  
Then use gettimeofday() directly. Or the time command for that matter. –  Aaron Digulla Aug 20 '10 at 14:49

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.