LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-20-2009, 06:47 AM   #1
Glenn D.
Member
 
Registered: May 2009
Location: ACT - Australia
Distribution: Opensuse x86_64 (Latest)
Posts: 61

Rep: Reputation: 16
How can I calculate minutes to the next 7:00am is, in a shell script ?


How can I calculate minutes to the next 7:00am is, in a shell script ?

Example it may be 21:30 or 0:10 now and I want to find out how many minutes to the next 7:00am is ?
Can you provide a sample script to do that please ?

Thanks Glenn
 
Old 11-20-2009, 09:22 AM   #2
indiajoe
Member
 
Registered: Jan 2009
Location: India
Distribution: Slax atma
Posts: 82

Rep: Reputation: 21
Hi
Make a script as follows,
Code:
#!/bin/sh
hour=`date | cut -b 12-13`
minute=`date | cut -b 15-16`
if [ "$hour" -lt 7 ] ; then
hourrem=$((6 - hour))
else
hourrem=$((23 - hour + 7 ))
fi
minuterem=$((59 - minute))
hourmin=$((hourrem * 60))
Total=$((hourmin + minuterem))

echo "Remaining minutes to 7am is $Total"
-Cheers
indiajoe

Last edited by indiajoe; 11-20-2009 at 09:27 AM.
 
Old 11-20-2009, 11:47 AM   #3
#!/usr/bin/which
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 1
This will give you the time left till the next 7:00 a.m. in minutes
Code:
$ bc <<< "(( $(date -d'07:00 am' +%s) - $(date +%s) + 60*60*24 ) % (60*60*24)) / 60"
 
Old 11-20-2009, 11:40 PM   #4
Glenn D.
Member
 
Registered: May 2009
Location: ACT - Australia
Distribution: Opensuse x86_64 (Latest)
Posts: 61

Original Poster
Rep: Reputation: 16
Thanks for the info.
In the end I had to take account of the time zone too.

# cat ./time-test.sh
#!/bin/sh
export TZ=Australia/Sydney
date
hour=`date | cut -b 12-13`
minute=`date | cut -b 15-16`
if [ "$hour" -lt 7 ] ; then
hourrem=$((6 - hour))
else
hourrem=$((23 - hour + 7 ))
fi
minuterem=$((59 - minute))
hourmin=$((hourrem * 60))
Total=$((hourmin + minuterem))

echo "Remaining minutes to 7am is $Total"

Sample run:

# ./time-test.sh
Sat Nov 21 15:38:37 EST 2009
Remaining minutes to 7am is 921
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to calculate Network Address with shell script Adanos Programming 5 11-09-2008 11:37 AM
A shell script that will wget for 30 minutes, then stop? Rotwang Linux - General 7 06-12-2008 03:28 AM
Calculate average from csv file in shell script khairilthegreat Linux - Newbie 5 11-21-2007 01:57 PM
shell script to calculate the time a user is logged in racer_mec Linux - Newbie 2 02-08-2005 12:40 PM
Calculate in shell script Ephracis Linux - General 1 12-16-2004 12:11 PM


All times are GMT -5. The time now is 09:48 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration