diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2021-05-09 12:11:24 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2021-05-09 12:11:24 +0800 |
| commit | 7a0fc047ae3e6742da844b472e6acf2e88d20581 (patch) | |
| tree | f1096fda87ae58e4315d11376f873100eef30d99 /challenge-013 | |
| parent | e32de6c08d7fa5c32ed7b6c9ea74992141f855bd (diff) | |
| download | perlweeklychallenge-club-7a0fc047ae3e6742da844b472e6acf2e88d20581.tar.gz perlweeklychallenge-club-7a0fc047ae3e6742da844b472e6acf2e88d20581.tar.bz2 perlweeklychallenge-club-7a0fc047ae3e6742da844b472e6acf2e88d20581.zip | |
Practice date and time stuff
Diffstat (limited to 'challenge-013')
| -rw-r--r-- | challenge-013/cheok-yin-fung/README.md | 6 | ||||
| -rw-r--r-- | challenge-013/cheok-yin-fung/java/LastFriday.java | 31 | ||||
| -rw-r--r-- | challenge-013/cheok-yin-fung/perl/ch-1.pl | 27 |
3 files changed, 64 insertions, 0 deletions
diff --git a/challenge-013/cheok-yin-fung/README.md b/challenge-013/cheok-yin-fung/README.md new file mode 100644 index 0000000000..743a767c7d --- /dev/null +++ b/challenge-013/cheok-yin-fung/README.md @@ -0,0 +1,6 @@ +Goals: + +- Java: Get familiar with the Date and Time API, +- Perl: Get familiar with Time::Piece (and Time::Seconds) module, especially the strftime and strptime methods + +attempt on 9th May, 2021 diff --git a/challenge-013/cheok-yin-fung/java/LastFriday.java b/challenge-013/cheok-yin-fung/java/LastFriday.java new file mode 100644 index 0000000000..4ab9ba6355 --- /dev/null +++ b/challenge-013/cheok-yin-fung/java/LastFriday.java @@ -0,0 +1,31 @@ +// The Weekly Challenge - 013 +// Task 1 Last Friday +// attempt: May 09th, 2021 +// from: C.-Y. Fung +// Usage: java LastFriday [year] +import java.time.LocalDate; +import java.time.YearMonth; +import java.time.DayOfWeek; +public class LastFriday +{ + public static void main(String[] args) + { + int year = 2008; // default value + if (args.length != 0) + year = Integer.parseInt(args[0]); + if (year < 1900) { + System.out.println("Sorry, it's too long ago."); + System.exit(0); + } + System.out.println("Year: " + year + "\n"); + for (int month = 1; month <= 12; month++) { + YearMonth temp = YearMonth.of(year, month); + LocalDate target = + LocalDate.of(year , month , temp.lengthOfMonth()); + while ( target.getDayOfWeek() != DayOfWeek.FRIDAY ) { + target = target.minusDays(1); + } + System.out.println(target); + } + } +} diff --git a/challenge-013/cheok-yin-fung/perl/ch-1.pl b/challenge-013/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..d0b747ffb7 --- /dev/null +++ b/challenge-013/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +# The Weekly Challenge - 013 +# Task 1 Last Friday +# attempt: May 09th, 2021 +# from: C.-Y. Fung +# Usage: ch-1.pl [year] +# Java correspondence: LastFriday.java +use Time::Piece; +use Time::Seconds; +use strict; +use warnings; + +my $year = $ARGV[0] || 2008; +die "Sorry, it's too long ago." if $year < 1900; +print "Year: $year\n\n"; + +for my $month (1..12) { + my $first_day_of_nmonth = ($month != 12) ? + Time::Piece->strptime($year."-".($month+1)."-01", "%Y-%m-%d") + : Time::Piece->strptime(($year+1)."-01-01", "%Y-%m-%d"); + + my $target_day = ($first_day_of_nmonth -= ONE_DAY); + while ($target_day->day_of_week != 5) { + $target_day -= ONE_DAY; + } + print($target_day->strftime("%Y-%m-%d"), "\n"); +} |
