-
Notifications
You must be signed in to change notification settings - Fork 4
/
dt
executable file
·59 lines (42 loc) · 1.07 KB
/
dt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
use feature 'say';
GetOptions(
'help' => \my $help
);
if ($help) {
print "
dt Print today's date
dt 5 Print date in five days
dt 7w Print date in seven weeks
";
exit 0;
}
unless (@ARGV) {
print_date(time);
exit 0;
}
my $days_or_weeks = shift;
my $secs_per_day = 24*60*60;
if ($days_or_weeks =~ s/w//) {
print_date(time + $secs_per_day * $days_or_weeks * 7);
}
else {
print_date(time + $secs_per_day * $days_or_weeks);
}
sub year_month_day_weekday {
my $time = shift;
my ($second, $minute, $hour, $day_of_month, $month, $year, $day_of_week, $day_of_year, $daylight_saving_time) = localtime($time);
$month ++;
$year += 1900;
$month = sprintf("%02d", $month );
$day_of_month = sprintf("%02d", $day_of_month);
return ($year, $month, $day_of_month, ('So', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa')[$day_of_week]);
}
sub print_date {
my $time = shift;
my @ymdw = year_month_day_weekday($time);
say "$ymdw[0]-$ymdw[1]-$ymdw[2] ($ymdw[3])";
}