How to convert DATE to UNIX TIMESTAMP in shell script on MacOS
man date
on OSX has this example
date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
Which I think does what you want.
You can use this for a specific date
date -j -f "%a %b %d %T %Z %Y" "Tue Sep 28 19:35:15 EDT 2010" "+%s"
Or use whatever format you want.
How to convert date string to epoch timestamp with the OS X BSD `date` command?
Do you mean this?
date -j -f "%a %b %d %T %Z %Y" "Wed Jul 13 11:30:27 BST 2016" +"%s"
1468405827
I worked that out by telling date
to output in the same format as you were using:
date -j +"%a %b %d %T %Z %Y"
Convert Unix timestamp to a date string
With date
from GNU coreutils you can do:
date -d "@$TIMESTAMP"
# date -d @0
Wed Dec 31 19:00:00 EST 1969
(From: BASH: Convert Unix Timestamp to a Date)
On OS X, use date -r
.
date -r "$TIMESTAMP"
Alternatively, use strftime()
. It's not available directly from the shell, but you can access it via gawk. The %c
specifier displays the timestamp in a locale-dependent manner.
echo "$TIMESTAMP" | gawk '{print strftime("%c", $0)}'
# echo 0 | gawk '{print strftime("%c", $0)}'
Wed 31 Dec 1969 07:00:00 PM EST
On MAC OS machine. How can we convert date in 2017-01-09T09:35:26Z to Timestamp using shell script
Well, what a hassle for such a simple question. (PS: I think it was downvoted for not showing an error message, and the use of the OSX tag might better be replaced for macOS if you are on an up-to-date Mac).
The first problem I found out by reading this post. It explains that in order for your command to work the abbreviations need to be in the system language. This can be solved by adding LANG=C
in front of your command.
However, your command still delivered the following error:
Failed conversion of ``Tue Sep 28 19:35:15 EDT 2010'' using format ``%a %b %d %T %Z %Y''
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
Then, when looking into man date
I found this line explaining the -f argument:
-f Use input_fmt as the format string to parse the new_date provided rather than using the default
[[[mm]dd]HH]MM[[cc]yy][.ss] format. Parsing is done using strptime(3).
Then, in man strptime
I read:
Bugs - The %Z format specifier only accepts time zone abbreviations of
the local
time zone, or the value "GMT". This limitation is because of ambiguity
due to of the over loading of time zone abbreviations. One such example
is EST which is both Eastern Standard Time and Eastern Australia Summer
Time.
So, I was able to execute your command either like so:
LANG=C date -j -u -f '%a %b %d %T %Z %Y' 'Tue Sep 28 19:35:15 GMT 2010' +%s
Or, like so,
TZ=EDT LANG=C date -j -u -f '%a %b %d %T %Z %Y' 'Tue Sep 28 19:35:15 GMT 2010' +%s
But both aren't actually what you where looking for I guess..
You thus seem to be bitten by a known bug in the default macOS version of strptime.
EDIT
However, if you would have a list with the offset of each timezone (the offset towards GMT), you could do something like:
TZ=EDT LANG=C date -juf '%a %b %d %T %z %Y' 'Tue Sep 28 19:35:15 -0400 2010' +%s
Have a look at this post for more possibilities.
Convert unix epoch time to human readable date on Mac OSX - BSD
Here you go:
# date -r 123439819723 '+%m/%d/%Y:%H:%M:%S'
08/26/5881:17:48:43
In a bash script you could have something like this:
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
dayOfWeek=$(date --date @1599032939 +"%A")
dateString=$(date --date @1599032939 +"%m/%d/%Y:%H:%M:%S")
elif [[ "$OSTYPE" == "darwin"* ]]; then
dayOfWeek=$(date -r 1599032939 +%A)
dateString=$(date -r 1599032939 +%m/%d/%Y:%H:%M:%S)
fi
How to convert timestamps to dates in Bash?
On systems with GNU Coreutils >= 5.3.0, e.g. Linux you can use:
date -d @1267619929
How can I generate Unix timestamps?
In Linux or MacOS you can use:
date +%s
where
+%s
, seconds since 1970-01-01 00:00:00 UTC. (GNU Coreutils 8.24 Date manual)
Example output now 1454000043.
Related Topics
Linux Sed Command - Using Variable with Backslash
How to Install Packages in Linux (Centos) Without Root User with Automatic Dependency Handling
How to Load Luks Passphrase from Usb, Falling Back to Keyboard
Get Current Time in Hours and Minutes
Find File Then Cd to That Directory in Linux
What Context Does the Scheduler Code Run In
X11 Forwarding Request Failed on Channel 0
What Does the Mkdir -P Mean in a Script File
Trailing Arguments with Find -Exec {} +
Counting Number of Lines in a File Using Grep and Wc
Calculate Size of Files in Shell
How to Learn the Structure of Linux Wireless Drivers (Mac80211)
Adding Any Current Directory './' to the Search Path in Linux
Linux, Why Can't I Write Even Though I Have Group Permissions