-
Notifications
You must be signed in to change notification settings - Fork 315
/
ch-2.pl
98 lines (79 loc) · 2.18 KB
/
ch-2.pl
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/perl
# Test: ./ch2.pl
use strict;
use warnings;
use URI;
use Web::Scraper;
use feature qw /say/;
use String::Util qw /trim/;
use DateTime;
use Time::Seconds;
my $nov = DateTime->new(
year => 2019,
month => 11,
day => 1
);
my $dec = DateTime->new(
year => 2019,
month => 12,
day => 1
);
compare_lengths($nov, $dec);
# Compare the daylight lengths of 2 months
sub compare_lengths {
my ($date1, $date2) = @_;
# Get the data from the web
my $date1_data =
get_data($date1->month, $date1->year);
my $date2_data =
get_data($date2->month, $date2->year);
# Calculate totals
my $date1_total = calculate_daylight_total($date1_data);
my $date2_total = calculate_daylight_total($date2_data);
my $difference = $date1_total - $date2_total;
# Print the output
say $date1->month_abbr . ' ' . $date1->year . ' has ' .
Time::Seconds->new($date1_total)->pretty .
' of daylight.';
say $date2->month_abbr . ' ' . $date2->year . ' has ' .
Time::Seconds->new($date1_total)->pretty .
' of daylight.';
say "The difference is: " .
Time::Seconds->new($difference)->pretty . '.';
}
# Calculates the total daylight hours from data
sub calculate_daylight_total {
my ($data) = @_;
my $total = 0;
for my $time_string (@{$data}) {
$total += convert_time_to_seconds($time_string);
}
return $total;
}
# Convert hh::mm::ss to seconds
sub convert_time_to_seconds {
my ($hh, $mm, $ss) = split(':', shift);
return $hh * 3600 + $mm * 60 + 60;
}
# Get the data from the web
sub get_data {
my ($month, $year) = @_;
my @data;
# Scrape the date
my $url = 'https://www.timeanddate.com/sun/uk/london?' .
"month=$month&year=$year";
my $times = scraper {
process 'table[id="as-monthsun"] td', "times[]" => {
td_text => 'TEXT',
}
};
my $res = $times->scrape( URI->new( $url ) );
# Parse the times
for my $time (@{$res->{times}}) {
my $e_time = trim($time->{td_text});
push @data, $e_time
if ($e_time =~ /^(\d)+\:(\d)+\:(\d)+$/);
}
# Output the data
return \@data;
}