Tell if the given year is leap or common. The algorithm for detecting whether the year is leap includes a few divisibility tests. Take an extract in the pseudocode from Wikipedia: if (year is not divisible by 4) then (it is a common year)else if (year is not divisible by 100) then (it is a leap year)else if (year is not divisible by 400) then (it is a common … Continue reading “📘 Computing leap years in Perl 6”
Category: Chapter 5. Date and Time
📘 Datetime arithmetic in Perl 6
Find the difference between the two dates. Add a given number of days to the date. The DateTime class in Perl 6 defines the + and – operators, which can be used in combination with either another DateTime object or with the Duration object. Let us first find the difference between the two given dates: my $date1 = DateTime.new(‘2017-12-31T23:59:50’);my $date2 … Continue reading “📘 Datetime arithmetic in Perl 6”
📘 Formatted date in Perl 6
Print the current date in an even better format. In Perl 6, there is a built-in DateTime class. It is equipped with a few useful methods, so there’s no need to use external modules for many standard tasks. Save the current moment in the $now variable: my $now = DateTime.now; The easiest thing is to print the … Continue reading “📘 Formatted date in Perl 6”
📘 Current date and time in Perl 6
Print current date and time as an epoch and in a human-readable format. In Perl 6, the time function returns the current time as the Unix epoch: say time; The output is something like this: 1495785518. For manipulating dates and times, use the built-in DateTime class: say DateTime.now; The date is now in a more human-readable format, although … Continue reading “📘 Current date and time in Perl 6”