Convert unix epoch time to human readable date on Mac OSX - BSD - bash

On my Mac OSX, my bash script has a epoch time 123439819723. I am able to convert the date to human readable format by date -r 123439819723 which gives me Fri Aug 26 09:48:43 EST 5881.
But I want the date to be in mm/ddd/yyyy:hh:mi:ss format. The date --date option doesn't work on my machine.

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

To convert a UNIX epoch time with OS X date, use
date -j -f %s 123439819723
The -j prevents date from trying to set the system clock, and -f specifies the input format. You can add +<whatever> to set the output format, as with GNU date.

from command Shell
[aks#APC ~]$ date -r 1474588800
Fri Sep 23 05:30:00 IST 2016
[aks#APC ~]$ date -ur 1474588800
Fri Sep 23 00:00:00 UTC 2016
[aks#APC ~]$ echo "1474588800" | xargs -I {} date -jr {} -u
Fri Sep 23 00:00:00 UTC 2016

In case of Linux (with GNU coreutils 5.3+) it can be achieved using less keystrokes:
date -d #1608185188
#Wed Dec 16 22:06:28 PST 2020
On Mac one may need to install coreutils (brew install coreutils)

Combined solution to run on Mac OS.
Shell code:
T=123439819723
D=$(date -j -f %s $(($T/1000)) '+%m/%d/%Y:%H:%M:%S').$(($T%1000))
echo "[$T] ==> [$D]"
Output:
[123439819723] ==> [11/29/1973:11:50:19.723]
Or one line:
> echo 123439819723 | { read T; D=$(date -j -f %s $(($T/1000)) '+%m/%d/%Y:%H:%M:%S').$(($T%1000)); echo "[$T] ==> [$D]" }
[123439819723] ==> [11/29/1973:11:50:19.723]

Related

linux touch relative time giving wrong result

I'm using bash touch to change the date and time of a file. It works correctly if I simply specify the date and time. If, however, I use a relative time I get unexpected behavior.
$ date -R -r test.txt
Sat, 28 May 2022 02:56:22 -0400
$ touch -d '27 May 2022 05:31:12' test.txt
$ date -R -r test.txt
Fri, 27 May 2022 05:31:12 -0400
$ touch -d '27 May 2022 05:31:12 - 1 hour' test.txt
$ date -R -r test.txt
Fri, 27 May 2022 03:31:12 -0400
$ touch -d "27 May 2022 05:31:12 + 1 hour" test.txt
$ date -R -r test.txt
Fri, 27 May 2022 01:31:12 -0400
Note that although I asked for 1 hour earlier and then 1 hour later, it gave 2 hours earlier and 4 hours earlier respectively.
Any help would be appreciated.
When you do your touch, you do no specify the timezone. If you do, all your date arithmetic works ok.
EX:
$ touch -d '27 May 2022 05:31:12' test.txt
$ date -R -r test.txt
Fri, 27 May 2022 05:31:12 -0400
$
$ touch -d '27 May 2022 05:31:12 -0400 - 1 hour' test.txt
$ date -R -r test.txt
Fri, 27 May 2022 04:31:12 -0400

Close tmux session with a particular name

I would like to have a command that closes all tmux session which name corresponds to a pattern. The pattern, in the language of regex, is nn\d+
I feel that I should be able to use a combination of grep and xargs.
I tried with
tmux ls | grep -P "nn\d+" | ..
but I am not sure how to use xargs here, that is: I am not sure how to refer to only the name part of the matched string, and to one string at the time.
To be more precise, the output of tmux ls is something like:
1: 1 windows (created Thu Mar 25 12:49:17 2021)
2: 1 windows (created Thu Mar 25 12:50:20 2021)
nn312133: 1 windows (created Thu Mar 25 12:53:54 2021)
nn3123: 1 windows (created Thu Mar 25 12:53:52 2021)
The output from grep is:
nn312133: 1 windows (created Thu Mar 25 12:53:54 2021)
nn3123: 1 windows (created Thu Mar 25 12:53:52 2021)
I should need to pipe nn312133 and nn3123 into tmux kill-session -t $x
Any idea? Thanks
Use grep -Po to make grep only print out the matched part instead of the whole line.
The answer is tmux ls | grep -Po "nn\d+" | xargs -n1 tmux kill-session -t

Run function on every prompt line with .bash_profile editing

I have the following PS1 command in my .bash_profile:
PS1="$(svn info 2>&1 | grep 'Relative URL' | awk '{print $NF}')"
So that the output of this command is presented in the prompt line.
But it is run once I start the terminal and it just stays there, instead of changing while I navigate through my directories. So it runs once and is left there.
How can I make it change as I am navigating my directories?
PROMPT_COMMAND
If set, the value is executed as a command prior to issuing each
primary prompt.
$ PROMPT_COMMAND=date
Sun Feb 21 13:35:21 EST 2016
$ echo a
a
Sun Feb 21 13:35:23 EST 2016
$ echo b
b
Sun Feb 21 13:35:24 EST 2016
$ PROMPT_COMMAND='PS1=`date +%H:%M`\ $\ '
13:35 $ sleep 60
13:36 $

No output from bash -c over ssh

How come when I run this on my local machine I get output
$ bash -c 'a=$(date) && echo $a'
Thu Feb 20 23:12:26 MST 2014
but if I try it over ssh (I have a public key on the other box, but no forced commands in authorized_keys)
$ ssh nathan#gnunix bash -c 'a=$(date) && echo $a'
Just a blank line is printed?
You don't need bash -c probably, just this would be able to print date:
ssh nathan#gnunix 'a=$(date) && echo $a'
If you must use bash -c then escape $ like this (otherwise $ is interpreted by current shell not remote one)
ssh nathan#gnunix "bash -c 'a=\$(date) && echo \$a'"
Fri Feb 21 01:22:42 EST 2014

How to convert UTC timestamp in seconds to human readable local time in a shell script?

Given UTC timestamp in seconds 1361322102.430 How do I convert it to human readable local time in a shell script?
Same moment in time at 1361322102.430 UTC seconds
shown in local time zone
$ date -d #1361322102.430
Tue Feb 19 20:01:42 EST 2013
shown in UTC time zone
$ date -d #1361322102.430 -u
Wed Feb 20 01:01:42 UTC 2013
For more details → $ info coreutils 'date invocation'
GNU:
$ date -d #1361414562.231
Wed Feb 20 18:42:42 PST 2013
Most BSDs:
$ date -r 1361414562.231
Wed Feb 20 18:41:38 PST 2013

Resources