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
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
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:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
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.
Perl is ubiquitous.
$ perl -MTime::HiRes=time -e 'printf "%.9f\n", time'
1557390839.244920969
16123456778.00000000000. It runs, but it doesn't do what the question asked on my mac.
– Ajax
Oct 11 '23 at 00:52
time() function, instead of Time::HiRes::time(), would you try to run perl -e 'use Time::HiRes; printf "%.9f\n", Time::HiRes::time();' ?
– ernix
Oct 11 '23 at 08:32
. instead of all 0
– Ajax
Dec 13 '23 at 15:32
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
%-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 '14 at 19:24
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 '14 at 19:30
date works fine on your machine, but is useless against portable scripting. For portability, this provides the closest possible value.
– grg
Jun 22 '14 at 15:37
gdate, it doesn't conflict with the existing date tool. A portable script that needs millisecond times could test for the presence of gdate and use it. It would fall back to date if necessary, using the +%s.000 format. A very portable script would test the output of date and verify it contains the necessary information, which would allow it to run on various OSes.
– Mr. Lance E Sloan
Dec 14 '16 at 16:57
This solution works on macOS, if you consider using a bash script and have python available:
#!/bin/bash
python -c 'from time import time; print(int(round(time() * 1000)))'
Or write a python script directly:
#!/usr/bin/python
from time import time
print(int(round(time() * 1000)))
python3 -c "from datetime import datetime; print(datetime.now().strftime('%y-%m-%d.%H-%M-%S.%f'))"
– Ajax
Dec 13 '23 at 15:27
gdatewas installed in/usr/local/bin/gdate. – Adam Matan Jun 22 '14 at 13:22datebecomes GNUdate(instead of gdate) – CousinCocaine Jun 22 '14 at 15:28+%s.%Nformat gives the seconds down to nanosecond precision, not milliseconds. After I used brew to installgdate, I checked the manpage to see whether milliseconds was available. It's not. It also didn't say that a precision for%Ncould be specified, but I tried it anyway. I found that the command to get the seconds down to millisecond precision only (i.e., three decimal places) isgdate +%s.%3N. – Mr. Lance E Sloan Dec 14 '16 at 17:12