equivalent date from GNU to solaris - linux

In GNU with the command date I can do it:
date -d "+4 day"
datei=20130101
i=5
date -d "$datei +$i day"
But i like know:
how can i do it in Solaris?
with the date command

Tcl has a good free-form date scanner, if you have Tcl installed (try which tclsh). A shell function:
tcldate() {
d=${1:-now} # the date string
f=${2:-%c} # the output format
echo "puts [clock format [clock scan {$d}] -format {$f}]" | tclsh
}
In action on an ancient Solaris 8 box with bash 2.03 and tcl 8.3.3
$ tcldate
Tue Jul 23 13:27:17 2013
$ i=4
$ tcldate "$i days"
Sat Jul 27 13:27:34 2013
$ tcldate "$i days" "%Y-%m-%d"
2013-07-27
$ tcldate "20130101 + $i days" "%Y-%m-%d"
2013-01-05
This even handles daylight savings transitions:
$ tcldate "2014-03-09 00:30 + 1 hour" "%D %T %Z"
03/09/14 01:30:00 EST
$ tcldate "2014-03-09 00:30 + 2 hour" "%D %T %Z"
03/09/14 03:30:00 EDT
$ tcldate "2013-11-03 00:30 + 1 hour" "%D %T %Z"
11/03/13 01:30:00 EDT
$ tcldate "2013-11-03 00:30 + 2 hour" "%D %T %Z"
11/03/13 01:30:00 EST

Related

I have to extract the two epoch time from the data and then i have to find difference between them in bash?

I have this data where i have two epoch value and i have to extract the value and then need to calculate there difference?
;;epoch1;;epoch 2
617B96A302C71177;;1638297252.658;;1638297253.078;TTrans1;DEE61;66500;xxxxx;in;0x19;0x0;0;;scb:in;0x19;0x0;0;;sc:iund;0x0;ggp-ir:djkoe:ID 0: DSP: 1:trunk_02:ch_000
I tried below method but don't know how to calculate the difference.
#!/bin/bash
awk -F";;|;" -vOFS='\t' '{print strftime("%c", $3), strftime("%c", $2)}' duration.txt(data is written in this file)
Output of above command:-
Wed Dec 1 00:04:13 2021 Wed Dec 1 00:04:12 2021
Wed Dec 1 00:03:46 2021 Wed Dec 1 00:03:23 2021
epoch 1 =1638297252.658 epoch2 = 1638297253.078 first I have to extract the epochs value from the mentioned string. And then to calculate the time gap (or difference), I am trying to convert it into below format
Wednesday 01 December 2021 12:04:13 AM IST .
But even after converting both epoch in mentioned format i don't know what to do for calculating difference.
And I have used the awk command to extract the field from the string.
please excuse my poor explanation
awk -F ';' 'FNR!=1 {high = $3>$5?$3:$5; low = $3<$5?$3:$5; print high-low}' duration.txt
# gives
0.42
Or perhaps more readably:
awk -F ';' '
FNR!=1 {
if ($3 > $5) {
high=$3; low=$5
} else {
high=$5; low=$3
}
print high-low
}' duration.txt
Using awk
$ awk -F";" '{$3=strftime("%A %d %B %Y %T %Z",$3); $5=strftime("%A %d %B %Y %T %Z",$5); hour=substr($3,17,2);min=substr($3,20,2);sec=substr($3,23,2); hour2=substr($5,17,2);min2=substr($5,20,2);sec2=substr($5,23,2)} NR > 1 {print "epoch 1: "$3"\nepoch 2: "$5"\nTime difference is "hour2 - hour" hours " min2-min" minutes " sec2 - sec" seconds"}' input_file
epoch 1: Tuesday 30 November 2021 18:34:12 GMT
epoch 2: Tuesday 30 November 2021 18:34:13 GMT
Time difference is 0 hours 0 minutes 1 seconds
$ cat script.awk
#!/usr/bin/env/ awk -f
BEGIN {
FS=";"
} {
$3=strftime("%A %d %B %Y %T %Z",$3)
$5=strftime("%A %d %B %Y %T %Z",$5)
hour=substr($3,26,2)
min=substr($3,29,2)
sec=substr($3,32,2)
hour2=substr($5,26,2)
min2=substr($5,29,2)
sec2=substr($5,32,2)
} NR > 1 {
print "epoch 1: "$3"\nepoch 2: "$5"\nTime difference is "hour2 - hour" hours " min2-min" minutes " sec2 - sec" seconds"
}
$ awk -f script.awk input_file
epoch 1: Tuesday 30 November 2021 18:34:12 GMT
epoch 2: Tuesday 30 November 2021 18:34:13 GMT
Time difference is 0 hours 0 minutes 1 seconds

Converting date with timezone in UNIX timestamp Shell/Bash

I need to convert a date from string in the format "yyyy/mm/dd hh:mm:ss TZ" to UNIX time (TZ = Timezone).
What I have done so far is to convert a date in the format "yyyy/mm/dd hh:mm:ss" without a timezone to timestamp by using
dateYMD="2019/2/28 12:23:11.46"
newt=$(date -d "${dateYMD}" +"%s")
echo ${newt}
and I have the following result.
1551349391
My struggle is to find how both timezone and date/time can be converted to timestamp (unix time) . For example I need 4 variables with the same date/time as dateYMD but in 4 different timezones so that their timestamps would be different.
Here is the latest I have tried
dateYMD="2017/09/09 08:58:09"
timez=$(TZ=Australia/Sydney date -d #$(date +%s -d "${dateYMD}"))
unixTimez=$( date --date "${timez}" +"%s" )
echo ${unixTimez}
that showed me the following error
date: invalid date ‘чт фев 28 21:23:11 AEDT 2019’
You don't need to call date twice. Just call it once with TZ set to the timezone you want for that variable.
timesydney=$(TZ=Australia/Sydney date -d "$dateYMD" +%s)
timenyc=$(TZ=US/Eastern date -d "$dateYMD" +%s)
Either you do it by setting the TZ= environment variable (see answer of Barmar), or you include the time zone into the time string. This has higher priority than TZ=.
Examples:
TZ=UTC date -d '2019-01-01 12:00 CET' +'%s, %F %T %Z %z'
TZ=CET date -d '2019-01-01 12:00 CET' +'%s, %F %T %Z %z'
TZ=UTC date -d '2019-01-01 12:00 PDT' +'%s, %F %T %Z %z'
TZ=CET date -d '2019-01-01 12:00 PDT' +'%s, %F %T %Z %z'
TZ=UTC date -d '2019-01-01 12:00 +500' +'%s, %F %T %Z %z'
will print
1546340400, 2019-01-01 11:00:00 UTC +0000
1546340400, 2019-01-01 12:00:00 CET +0100
1546369200, 2019-01-01 19:00:00 UTC +0000
1546369200, 2019-01-01 20:00:00 CET +0100
1546326000, 2019-01-01 07:00:00 UTC +0000

Insert Text Between Two Date Variables

I am trying to insert some text between two date commands
Code:
date -u "+%H":00Z --date="1 hours ago" "+%a %b %d" --date="0 days ago"
Error:
date: extra operand ‘+%a %b %d’
Try 'date --help' for more information.
Expected Output:
11:00Z Fri Oct 20
With GNU date , in 2 steps:
date -d #$(date +%s --date="0 days ago") -u "+%H:00Z %a %b %d" --date="-1 hours ago"

need to split "date" command ouput in shell

I want to split date command output and extract time zone difference and represent that time difference in terms of 30 min.
I tried this:
date -R | awk 'NF>1{print $NF}'
I got the output -0530
This means 5 hours and 30 min difference from GMT. Now I want to convert this -0530 in to -11. So what logic should I use in shell command?
date -R | awk '{tz=$NF;ew=substr(tz,1,1);h=substr(tz,1,3)*2;m=ew substr(tz,4,2)/30;print h + m}'
https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
Edited to work with omitted sign
date -R | awk '{tz=$NF;l=length(tz);m=substr(tz,l-1,2)/30;h=substr(tz,l-3,2)*2;ew=l>4?ew=substr(tz,1,1):"+";print (ew h)+(ew m)}'
No need to complicate things.
tz=$(date '+%z'); echo "${tz:0:1}$(( 2 * ${tz:1:2} + ${tz:3} ))"
I wouldn't, and instead use a different tool. In the perl module Time::Piece you have tzoffset which returns the current timezone skew in seconds.
Thus:
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
$ENV{'TZ'}='GMT-6';
my $now = localtime;
#find number of 30m differences;
print $now -> tzoffset / 60 / 30;
You can also use variants of strftime to print time formatted to your liking:
print $now -> strftime("%c");
This can be cut down into a one liner if so desired:
perl -MTime::Piece 'print localtime -> tzoffset / 60 / 30'
With GNU date, you could perform math on time/date values (in UTC):
$ date -uR -d "now"; date -uR -d "now -60 min"
Sun, 27 Dec 2015 20:34:17 +0000
Sun, 27 Dec 2015 19:34:17 +0000
A time of -5:30 is -330 min:
$ date -uR -d "now"; date -uR -d "now -330 min"
Sun, 27 Dec 2015 20:34:55 +0000
Sun, 27 Dec 2015 15:04:55 +0000
That could be further moved by the time zone:
$ TZ=America/New_York date -R -d "now"; TZ=America/New_York date -R -d "now -330 min"
Sun, 27 Dec 2015 15:34:26 -0500
Sun, 27 Dec 2015 10:04:26 -0500
That is the local time (now) subtracting 5 hours and 30 minutes.
However, what is a time zone of -5:30?
I know that India is +5:30 and there are some (very few) places that are +11:00.
But I fail to find any -5:30 and -11:00 seems just odd.

Get the next time occurance with linux date

Linux date utility can understand a lot of strings including for instance:
$ date -d '8:30'
Fri Jan 2 08:30:00 CET 2015
I'm looking for a way to get the next 8:30, thus:
in case it is Fri Jan 2 before 8:30, the result above should be returned;
otherwise it should print Sat Jan 3 08:30:00 CET 2015.
As one can see next 8:30 doesn't result in the correct answer:
$ date -d 'next 8:30'
date: invalid date ‘next 8:30’
Is there a single expression to calculate this?
Handling it in the shell oneself is of course an option, but makes things more complicates because of daylight save time regulation etc.
In case the clock is adapted to daylight save time, next 8:30 should be parsed to 8:30 according to the settings of the next day.
Testcase:
Given it is Fri Jan 2 12:01:01 CET 2015, the result should be:
$ date -d 'next 8:30'
Sat Jan 3 08:30:00 CET 2015
$ date -d 'next 15:30'
Fri Jan 2 15:30:00 CET 2015
Just use something like:
if [[ $(date -d '8:30 today' +%s) -lt $(date +%s) ]] ; then
next830="$(date -d '8:30 tomorrow')"
else
next830="$(date -d '8:30 today')"
fi
The %s format string gives you seconds since the epoch so the if statement is basically:
if 8:30-today is before now:
use 8:30-tomorrow
else
use 8:30-today
I researched and it does not seem to be possible to do so.
What you can probably do is to compare the hour and minute with 830 and print accordingly:
[ $(date '+%H%M') -le 830 ] && date -d '8:30' || date -d '8:30 + 1 day'
In case you want to work with this easily, create a function to do these calculations.
Test
$ [ $(date '+%H%M') -le 830 ] && date '8:30' || date -d '8:30 + 1 day'
Sat Jan 3 08:30:00 CET 2015

Resources