I can have UTC current time with "date -u".
But i would like to print the current time of some common timezones at the same time. (UTC, EDT, CEST...)
I can create a script and add severals :
date -u -d 'x hour ago'
But sometimes, hours change. (Like in France)
Is there another solution to have the "real" current time, based on the country / cities maybe ?
EDIT :
Here what i have done with the answer :
function dateall(){
echo -n "US Pacific : " && TZ=US/Pacific date
echo -n "US Eastern : " && TZ=US/Eastern date
echo -n "UTC ~ GMT : " && date -u
echo -n "Europe Paris : " && TZ=Europe/Paris date
echo -n "Asia Bangkok : " && TZ=Asia/Bangkok date
}
You can point TZ to the timezone you want:
$ TZ=Europe/Paris date
Fri Jun 12 07:41:28 CEST 2015
In my dateutils there's datezone to do what you want in one simple command:
$ datezone now Europe/Paris US/Pacific US/Eastern UTC
2015-06-12T08:23:32+02:00 Europe/Paris
2015-06-11T23:23:32-07:00 US/Pacific
2015-06-12T02:23:32-04:00 US/Eastern
2015-06-12T06:23:32+00:00 UTC
2015-06-12T13:23:32+07:00 Asia/Bangkok
$
with the additional advantage that you also get the current UTC offsets and the times are guaranteed to coincide, i.e. the current time is determined only once and then used for all timezone conversions.
Related
Is there an option of date function?
How do I get LOAD_TEST_START to be 2 minutes forward?
How do I get LOAD_TEST_END to be 2 minutes back?
LOAD_TEST_START=$(date -u +%FT%TZ)
LOAD_TEST_END=$(date -u +%FT%TZ)
This answer helped me and works on the linux-version of date.
LOAD_TEST_START=$(date +%FT%TZ -d "2 minutes ago")
LOAD_TEST_END=$(date +%FT%TZ -d "2 minutes")
On macos/FreeBSD one can use
LOAD_TEST_START=$(date -v+2M +%FT%TZ)
LOAD_TEST_END=$(date -v-2M +%FT%TZ)
Not sure if this is what you want, but you can get the current epoch value and then add/subtract 120 seconds (2 minutes) from it and convert the results back to date.
dateval=`date +%s`
echo "orig "`date -d #$dateval +%FT%T%Z`
start=`expr $dateval + 120`
end=`expr $dateval - 120`
LOAD_TEST_START=`date -d #$start +%FT%T%Z`
LOAD_TEST_END=`date -d #$end +%FT%T%Z`
echo "start "$LOAD_TEST_START
echo "end "$LOAD_TEST_END
Operating System: Red Hat Enterprise Linux Server 7.2 (Maipo)
I want to round the time to the nearest 5 minutes, only up, not down, for example:
08:09:15 should be 08:10:00
08:11:26 should be 08:15:00
08:17:58 should be 08:20:00
I have been trying with:
(date -d #$(( (($(date +%s) + 150) / 300) * 300)) "+%H:%M:%S")
This will round the time but also down (08:11:18 will result in 08:10:00 and not 08:15:00)
Any idea how i can achieve this?
You may use this utility function for your rounding up:
roundDt() {
local n=300
local str="$1"
date -d #$(( ($(date -d "$str" '+%s') + $n)/$n * $n)) '+%H:%M:%S'
}
Then invoke this function as:
roundDt '08:09:15'
08:10:00
roundDt '08:11:26'
08:15:00
roundDt '08:17:58'
08:20:00
To trace how this function is computing use -x (trace mode) after exporting:
export -f roundDt
bash -cx "roundDt '08:11:26'"
+ roundDt 08:11:26
+ typeset n=300
+ typeset str=08:11:26
++ date -d 08:11:26 +%s
+ date -d #1535631300 +%H:%M:%S
08:15:00
GNU date can calculate already. It is explained in the manual in the chapter "Relative items in date strings". So you need just one date call.
d=$(date +%T) # get the current time
IFS=: read h m s <<< "$d" # parse it in hours, minutes and seconds
inc=$(( 300 - (m * 60 + s) % 300 )) # calculate the seconds to increment
date -d "$d $inc sec" +%T # output the new time with the offset
Btw: +%T is the same as +%H:%M:%S.
I use these to judge Sunday in shell:
week=`date -d $day +%w`
month=`date -d "-1 day ago $day" '+%d'`
if [ "$week" = "0" ];then
if [ "$month" = "01" ];then
But when $day is 20171103 , It was regarded as Sunday , then the 20171105 was missed.
Then I run "date -d 20171105 +%w" in the server ,it shows 0, and the if caluse was true. Then I check the Time Zone , Time Zone was right ,+0800
Are there any better methods in shell that can solve this problem? More safer and robust to judge whether the string like YYYYMMDD is Sunday?
Why not just use date %u for day of the week?
There are plenty of options in the man page.
I've been trying to get the following to work but I always seem get some sort of error..
get time from user input
use said time as variable
convert said time to JST
take away $duration (minutes) from time to give new time.
It would be something along the lines of code below.
#!/bin/sh
read -p "Please enter hour: " hour
read -p "Please enter minute: " minute
read -p "Please enter duration: " duration
jptime=$(TZ=JST date --date $hour$minute)
newtime=$(date -d "$jptime" "-$duration minutes")
echo "$newtime"
I'd use a language with a good datetime library. For example, perl
perl -MDateTime -E '
($hour, $minute, $duration) = #ARGV;
$fmt = "%F %T %Z";
$local = DateTime->now(time_zone=>"local")->set(hour=>$hour, minute=>$minute, second=>0);
say $local->strftime($fmt);
$jp = $local->clone->set_time_zone("Asia/Tokyo");
say $jp->strftime($fmt);
$jp2 = $jp->subtract(minutes => $duration);
say $jp2->strftime($fmt);
' 8 0 45
2015-01-17 08:00:00 EST
2015-01-17 22:00:00 JST
2015-01-17 21:15:00 JST
or Tcl
hour=8
minute=0
duration=45
export hour minute duration
tclsh <<'END'
set fmt "%Y-%m-%d %T %Z"
set t [clock scan "$env(hour):$env(minute) today"]
puts [clock format $t -format $fmt]
puts [clock format $t -format $fmt -timezone "Asia/Tokyo"]
puts [clock format [clock add $t -$env(duration) minutes] -format $fmt -timezone "Asia/Tokyo"]
END
2015-01-17 08:00:00 EST
2015-01-17 22:00:00 JST
2015-01-17 21:15:00 JST
Per your comment:
perl -MDateTime -E '
($hour, $minute, $duration) = #ARGV;
say uc DateTime->now(time_zone=>"local")
->set(hour=>$hour, minute=>$minute, second=>0)
->set_time_zone("Asia/Tokyo")
->subtract(minutes => $duration)
->strftime("%a %H:%M");
' 8 0 45 >| output.file
In a Linux script: I have a file that has two time entries for each message within the file. A 'received time' and a 'source time'. there are hundreds of messages within the file.
I want to calculate the elapsed time between the two times.
2014-07-16T18:40:48Z (received time)
2014-07-16T18:38:27Z (source time)
The source time is 3 lines after the received time, not that it matters.
info on the input data:
The input has a lines are as follows:
TimeStamp: 2014-07-16T18:40:48Z
2 lines later: a bunch of messages in one line and within each line, multiple times is:
sourceTimeStamp="2014-07-16T18:38:27Z"
If you have GNU's date (not busybox's), you can give difference in seconds with:
#!/bin/bash
A=$(date -d '2014-07-16T18:40:48Z' '+%s')
B=$(date -d '2014-07-16T18:38:27Z' '+%s')
echo "$(( A - B )) seconds"
For busybox's date and ash (modern probably / BusyBox v1.21.0):
#!/bin/ash
A=$(busybox date -d '2014-07-16 18:40:48' '+%s')
B=$(busybox date -d '2014-07-16 18:38:27' '+%s')
echo "$(( A - B )) seconds"
you should be able to use date like this (e.g.)
date +%s --date="2014-07-16T18:40:48Z"
to convert both timestamps into a unix timestamp. Getting the time difference between them is then reduced to a simple subtraction.
Does this help?
I would use awk. The following script searches for the lines of interest, converts the time value into a UNIX timestamp and saves them in the start, end variables. At the end of the script the difference will get calculated and printed:
timediff.awk:
/received time/ {
"date -d "$1" +%s" | getline end
}
/source time/ {
"date -d "$1" +%s" | getline start
exit
}
END {
printf "%s seconds in between", end - start
}
Execute it like this:
awk -f timediff.awk log.file
Output:
141 seconds in between