Displaying time left to end of day, week, month?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Displaying time left to end of day, week, month?
# 1  
Old 11-21-2011
Question Displaying time left to end of day, week, month?

Hello,

basically I'm looking after a way of showing how many time is left to the end the day, week, month...

I can't seem to figure this out, so could one of you skilled guys tell me how should this be approached?

Examples:

Code:
Time left:

Day: 12h 12m 11s
Week: 4d 12h 12m 11s
Month: 8d 12h 12m 11s

or

Day: 11s
etc....

Thanks for all the help in advance, greetings!

EDIT:

Has to be BASH/SH, as I want to implement this function to one of my scripts where I can extract either time left for day or week or month etc.

Last edited by TehOne; 11-21-2011 at 11:25 AM..
# 2  
Old 11-21-2011
Code:
#! /usr/bin/perl

use strict;
use warnings;
use integer;
use Time::Local qw{ timelocal_nocheck };

sub timeleft($$@) {
    my $label = shift;
    my $now   = shift;
    my $then  = timelocal_nocheck(@_);

    my $delta_s  = $then - $now;
    my $delta_m  = $delta_s / 60; $delta_s %= 60;
    my $delta_h  = $delta_m / 60; $delta_m %= 60;
    my $delta_d  = $delta_h / 24; $delta_h %= 24;

    local $\ = '';
    local $, = '';

    print $label, ':';

    if (0 < $delta_d) {
        print ' ', $delta_d, 'd';
    }

    if (0 < $delta_d || 0 < $delta_m) {
        print ' ', $delta_h, 'h';
    }

    if (0 < $delta_d || 0 < $delta_m || 0 < $delta_h) {
        print ' ', $delta_m, 'm';
    }

    print ' ', $delta_s, "s\n";
}

my $NOW = time();
my @NOW = localtime($NOW);

$NOW[0] = 0;
$NOW[1] = 0;
$NOW[2] = 0;

my @EOD = @NOW;
   $EOD[3] += 1;

timeleft('  day', $NOW, @EOD);

my @EOW = @NOW;
   $EOW[3] += 7 - $NOW[6]; # wday;

timeleft(' week', $NOW, @EOW);

my @EOM = @NOW;
   $EOM[3]  = 1; # mday
   $EOM[4] += 1; # month

timeleft('month',  $NOW, @EOM);

---------- Post updated at 10:02 AM ---------- Previous update was at 10:01 AM ----------

Example output:
Code:
  day: 13h 57m 52s
 week: 5d 13h 57m 52s
month: 9d 13h 57m 52s

# 3  
Old 11-21-2011
Thank you m.d.ludwig but I'd need this to be within my bash/sh script, not perl.. Forgot to mention that in the first post, sorry!!!!!


Edit:

Or well preferable in bash but would nobody else step up with an alternative then I can always use your solution too! So again, thanks a lot!

Last edited by TehOne; 11-21-2011 at 11:31 AM..
# 4  
Old 11-21-2011
Try this...
Code:
#!/bin/bash

date_arr=( $(date '+%H %M %S %u %d') )

end_hour=$(echo "23-${date_arr[0]}"| bc)
end_min=$(echo "59-${date_arr[1]}"| bc)
end_sec=$(echo "60-${date_arr[2]}"| bc)

week_end=$(echo "7-${date_arr[3]}"| bc)

month_max=$(cal | awk '/^[0-9]/{d=$NF}END{print d}' )
month_end=$(echo "${month_max}-${date_arr[4]}"| bc)

eod="${end_hour}h ${end_min}m ${end_sec}s"

echo "Day       : $eod"
echo "Week      : ${week_end}d $eod"
echo "Month     : ${month_end}d $eod"

Output
Code:
root@bt:/tmp# date
Tue Nov 22 09:27:23 IST 2011
root@bt:/tmp# ./run
Day    : 14h 32m 37s
Week     : 5d 14h 32m 37s
Month     : 8d 14h 32m 37s

HTH
--ahamed

Last edited by ahamed101; 11-22-2011 at 12:19 AM..
# 5  
Old 11-22-2011
Very nice!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get day of week from epoch time

Need assistance . Below code gives me the date but I wanted output as day of the week (wday) . Code: use Time::Local; my $time=timelocal(1,2,3,9,11,2013); $theTime = localtime($time); print "$theTime\n"; Result: Mon Dec 9 03:02:01 2013 Wanted output as only Mon (2 Replies)
Discussion started by: ajayram_arya
2 Replies

2. Shell Programming and Scripting

Julian day to dates in YEAR-MONTH-DAY

hello, I have many files called day001, day002, day003 and I want to rename them by day20070101, day20070102, etc. I need to do it for several years and leap years as well. What is the best way to do it ? Thank you. (1 Reply)
Discussion started by: Ggg
1 Replies

3. Shell Programming and Scripting

How to add day of week at the end of each line that shows the date?

I have a file that looks like: file1: www_blank_com 20121008153552 www_blank_com 20121008162542 www_blank_com 20121009040540 www_blank_com 20121009041542 www_blank_com 20121010113548 www_blank_com 20121011113551 www_blank_com 20121012113542 I want the new file to show the day of... (3 Replies)
Discussion started by: castrojc
3 Replies

4. Shell Programming and Scripting

Day of the week or Month in a foreign language

Hey guys, i'm a very new shell script user. I've been looking everywhere for a proper script to display the day of the week or the month, accurately, in a foreign language of my choosing. Something where i can just type in the appropriate word in a foreign language in the script and get the... (2 Replies)
Discussion started by: ibizagreg
2 Replies

5. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

6. Shell Programming and Scripting

Code creates day 32 instead of 1st day of next month.

I am using the code below modified from a post I saw here regarding having the script write out future dates. The problem is that instead of making 8/1 it makes 7/32! Please help! yy=`date +%Y` mm=`date +%m` dd=`date +%d` echo "Today is : $yy $mm $dd" #!/usr/bin/ksh date '+%m... (5 Replies)
Discussion started by: libertyforall
5 Replies

7. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

8. UNIX for Dummies Questions & Answers

setting up cron job for month end week report

Hi all, Needs your help in scheduling a cron job for the below mentioned requirement. Just let me know if anybody has a similar job running as mentioned below: Month end reports - Report of all items created in a parent table that were created during the week. Presently this report runs... (3 Replies)
Discussion started by: Bhups
3 Replies

9. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies
Login or Register to Ask a Question