2 Replies - 427 Views - Last Post: 04 November 2015 - 06:44 AM Rate Topic: -----

#1 cp24eva  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 15-April 15

(BASH) convert epoch time to human readable then increment in hours

Posted 03 November 2015 - 08:21 PM

Hello ladies and gents. I'm really a newb at scripting, so I've come to you professionals for a push in the right direction. I've been tasked with converting an epoch time to various increments of human readable time. I feel like I'm on the right track, but I've come across a snag. When using the following script I get the error:

./epoch.sh: line 4: ((: Saturday December 31, 11:00:01 2011 -0800: syntax error in expression (error token is "December 31, 11:00:01 2011 -0800")

startdate=$(date -d @1325358001 +"%A %B %d, %T %Y %z")
next=3600

for (( i=startdate; i < startdate + 8*next; i+=next )); do
     date -d"@$i" +"%A %B %d, %T %Y %z"

done




When I take out the @1325358001 on the startdate variable that was set, then the script mostly works as intended. What am I doing wrong here?

Is This A Good Question/Topic? 0
  • +

Replies To: (BASH) convert epoch time to human readable then increment in hours

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: (BASH) convert epoch time to human readable then increment in hours

Posted 04 November 2015 - 06:31 AM

You can increment a number, but the result of a date -d call is a string.

So, you first get the numbers moving, then format them. This should work:
#!/bin/bash

START_DT=1325358001
FMT="%A %B %d, %T %Y %z"
INC=3600
(( END_DT = $START_DT + 8 * $INC ))

for (( N=$START_DT; $N < $END_DT; N+=INC )); do
    # echo $N
    date -d"@$N" +"$FMT"
done


Was This Post Helpful? 0
  • +
  • -

#3 cp24eva  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 15-April 15

Re: (BASH) convert epoch time to human readable then increment in hours

Posted 04 November 2015 - 06:44 AM

View Postbaavgai, on 04 November 2015 - 06:31 AM, said:

You can increment a number, but the result of a date -d call is a string.

So, you first get the numbers moving, then format them. This should work:
#!/bin/bash

START_DT=1325358001
FMT="%A %B %d, %T %Y %z"
INC=3600
(( END_DT = $START_DT + 8 * $INC ))

for (( N=$START_DT; $N < $END_DT; N+=INC )); do
    # echo $N
    date -d"@$N" +"$FMT"
done



Ah thanks for the input! This is great and has got me thinking in a different train of thought. The issue I'm having ow is trying to get the time zone offsets to increment as well. I'm almost thinking I would have to add a for loop that would increment the TZ offsets as well. I'm going to brainstorm in a little. I'm at work now. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1