kamal
kamal

Reputation: 9785

How can i convert linux "date +%s" output to a more readable format in bash script

Here is the script:


#!/bin/bash

i="0"
startTime=`date -u +%s`
startTime=$[$startTime+$1+5]
echo ""
echo "##################"
echo "LAUNCHING REQUESTS"
echo "  COUNT:  $1 "
echo "  DELAY:  1 "
echo "  EXECUTION:  $startTime "
echo "##################"
echo ""

while [ $1 -gt "$i" ]
do
  i=$[$i+1]
  php avtestTimed.php $1 $2 $startTime &
  echo "QUEUEING REQUEST $i"
  sleep 1
done



so i want to convert $startTime into a UTC format

Upvotes: 2

Views: 7790

Answers (5)

sorpigal
sorpigal

Reputation: 26086

Got perl?

human_time=$(perl -e 'print scalar gmtime(shift)' $startTime)

Upvotes: 0

Marcin
Marcin

Reputation: 3524

I'd have another variable

startTimeHuman=$(date -u -R)

right next to where you set startTime. This way you'd have both variants, one for doing math, and one for humans to understand.

Upvotes: 0

Benjamin Bannier
Benjamin Bannier

Reputation: 58586

You can use -d to pass $startTime back into date for processing, just prefix it with @ so it is recognized as seconds since the epoch.

$ date -d @$startTime

Once you have that down you can change the output format. I would suggest looking at the man page or the info documentation of date for that. For UTC output you would use

$ date -d @$startTime -u

Upvotes: 1

mishunika
mishunika

Reputation: 1916

Try:

date -d @$(($startTime-$1-5))

if you want to decode the first state of $startTime variable, or this:

date -d @$startTime

and, you can add -u argument to get the UTC time... %)

read the man date

Upvotes: 3

JJ.
JJ.

Reputation: 5475

What format are you looking for? Simply executing date with -u Will give you a readable output format of UTC time, ie:

# date -u
Tue Nov 30 15:35:12 UTC 2010

Upvotes: 1

Related Questions