LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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, 05:47 AM   #1
Glenn D.
Member
 
Registered: May 2009
Location: ACT - Australia
Distribution: Opensuse x86_64 (Latest)
Posts: 62

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, 08:22 AM   #2
indiajoe
Member
 
Registered: Jan 2009
Location: India
Distribution: Porteus atma
Posts: 84

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 08:27 AM.
 
Old 11-20-2009, 10: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, 10:40 PM   #4
Glenn D.
Member
 
Registered: May 2009
Location: ACT - Australia
Distribution: Opensuse x86_64 (Latest)
Posts: 62

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 10:37 AM
A shell script that will wget for 30 minutes, then stop? Rotwang Linux - General 7 06-12-2008 02:28 AM
Calculate average from csv file in shell script khairilthegreat Linux - Newbie 5 11-21-2007 12:57 PM
shell script to calculate the time a user is logged in racer_mec Linux - Newbie 2 02-08-2005 11:40 AM
Calculate in shell script Ephracis Linux - General 1 12-16-2004 11:11 AM


All times are GMT -5. The time now is 09:31 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