Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

How do I get the time since the epoch, in milliseconds, in the OSX terminal?

The Linux/Ubuntu equivalent is date +%s.%N:

Linux $ date +%s.%N
1403377762.035521859

Which does not work in my OSX terminal:

OSX $ date +%s.%N
1403377800.N
share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

The date program in OS X is different than GNU's coreutils date program. You can install coreutils (including gnu-date), then you will have a version of date that supports milliseconds.

As the installation from source can be a hassle for native OS X users I advise you to use Homebrew.

To install these tools using Homebrew run this oneliner in your terminal:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Homebrew is now installed (it is wise to follow the installer's suggestions after installation). Now we will install coreutils using brew.

brew install coreutils

As the installation says, all commands have been installed with the prefix 'g' (e.g. gdate, gcat, gln, etc etc). If you really need to use these commands with their normal names, you can add a "gnubin" directory to your PATH (~/.bash_profile) like:

PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"

You can now run

gdate +%s.%N

and this will output your time since the epoch in milliseconds.

share|improve this answer
    
Nice. I didn't even have to add the path, gdate was installed in /usr/local/bin/gdate. –  Adam Matan Jun 22 at 13:22
    
That's what you would expect. All the binaries are installed with the g prefix. In the path I provided all the same gnu-coreutils are without the g prefix. So if you add it in your path like PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH" it will replace the originals. So OS X date becomes GNU date (instead of gdate) –  CousinCocaine Jun 22 at 15:28
    
Fine that it worked out. –  CousinCocaine Jun 22 at 15:30
add comment

In OS X, just run date +%s as OS X doesn't support any more precision than this in date's output and any excess precision not supported by the internal representation is truncated toward minus infinity.

If you want milliseconds output, you can use the following command, although the output is just corrected by appending zeros rather than adding precision due to the aforementioned reason. The following does output correct milliseconds on systems which support the necessary precision.

echo $(($(date +'%s * 1000 + %-N / 1000000')))

Source for above command: Unix.SE – How to get milliseconds since Unix epoch

If you just want a command that appends the right number of zeros in OS X, you can use:

date +%s000
share|improve this answer
    
OS X doesn't support %-N, so that doesn't do anything (and could cause trouble if the shell variable $N is set). Just use date +%s000 (or date +%s.000 if you want the decimal point). –  Gordon Davisson Jun 21 at 19:24
    
@Gordon Yes, I mentioned this, but if OP is looking for a portable solution that does work successfully on systems which are capable of that precision, the above command works. Of course you could simply append zeros to the output but copying that to other systems which may support the extra precision would not be ideal. –  George Garside Jun 21 at 19:26
    
Ah, I see. But since OS X's date command just passes %-N through as N, it can interfere with the calculation. Try setting N=7000000000000000000, then try the command... This is an unlikely case in practice, but I'd feel safer with something that didn't depend on the environment. –  Gordon Davisson Jun 21 at 19:30
    
@Gordon Indeed, setting the variable would break it—I suppose the specific variable could be reset just before the command is run, but resetting variables like that is something every good script should do before running commands anyway, imo, just in case. Of course, this is all based on my assumption that the OP wants a script—edited answer to include the literal appending of zeros. –  George Garside Jun 21 at 19:32
    
The OP asked for "time since the epoch, in milliseconds", this is an answer on how to add zero's to a string. Not the same. Although explained in the post, not an answer to the question. –  CousinCocaine Jun 22 at 15:33
show 2 more comments

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.