TehOne
1
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:
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.
#! /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:
day: 13h 57m 52s
week: 5d 13h 57m 52s
month: 9d 13h 57m 52s
TehOne
3
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!
Try this...
#!/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
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