Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date command does not follow Linux specifications (Mac OS X Lion)

I have been developing a script on my linux box for quite some time, and wanted to run it on my Mac as well.

I thought that the functions on the Mac were the same as the functions on linux, but today I realized it was wrong. I knew that fewer functions existed on the Mac, but I thought that the functions that did exist, had the same implementation.

This problem is specifically in regards to the date command.

When I run the command on my linux machine with the parameter to provide some time in nanoseconds, I get the correct result, but when I run it on my mac, it does not have that option.

Linux-Machine> date +%N 55555555555 #Current time in nanoseconds Mac-Machine> date +%N N 

How do I go about getting the current time in nanoseconds as a bash command on the Mac?

Worst case is I create a small piece of code that calls a system function in C or something and then call it within my script.

Any help is much appreciated!

like image 830
Kaushik Shankar Avatar asked Mar 21 '12 12:03

Kaushik Shankar


People also ask

How do I fix the date in Terminal Mac?

Immediately hold down the Command and R keys until you see an Apple logo or spinning globe after restarting your Apple computer. While in the Recovery mode, cancel the installation process, and navigate to Utilities and open the Terminal app and type Date to check current date.

Can I run Linux commands on macOS?

After you obtain a BASH shell, you can run many of the same commands you've become accustomed to on Linux. It's also important to note that the same syntax rules that you used on Linux apply to BASH on macOS: All commands are case sensitive.

How do I change the date format in Terminal Mac?

Type date into Terminal followed by today's date in this format [month][day][hour][minute][year] so today is July 11, 2016, 2:15 that would be date 0711141516 then press enter. Of course, your date and time will vary. Rerun the first command in Terminal date and press enter; the date should now be fixed.

Is Mac OS X built on Linux?

You may have heard that Macintosh OSX is just Linux with a prettier interface. That's not actually true. But OSX is built in part on an open source Unix derivative called FreeBSD.


2 Answers

This is because OSX and Linux use two different sets of tools. Linux uses the GNU version of the date command (hence, GNU/Linux). Remember that Linux is Linux and OS X is Unix. They're different.

You can install the GNU date command which is included in the "coreutils" package from MacPorts. It will be installed on your system as gdate. You can either use that, or link the date binary with the new gdate binary; your choice.

like image 71
JoeLinux Avatar answered Sep 24 '22 00:09

JoeLinux


man date indicates that it doesn't go beyond one second. I would recommend trying another language (Python 2):

$ python -c 'import time; print repr(time.time())' 1332334298.898616 

For Python 3, use:

$ python -c 'import time; print(repr(time.time()))' 
like image 24
callumacrae Avatar answered Sep 23 '22 00:09

callumacrae