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-038 | |
| 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-038')
| -rw-r--r-- | challenge-038/cheok-yin-fung/README.md | 6 | ||||
| -rw-r--r-- | challenge-038/cheok-yin-fung/java/DateFinder.java | 65 | ||||
| -rw-r--r-- | challenge-038/cheok-yin-fung/perl/ch-1.pl | 32 |
3 files changed, 103 insertions, 0 deletions
diff --git a/challenge-038/cheok-yin-fung/README.md b/challenge-038/cheok-yin-fung/README.md new file mode 100644 index 0000000000..743a767c7d --- /dev/null +++ b/challenge-038/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-038/cheok-yin-fung/java/DateFinder.java b/challenge-038/cheok-yin-fung/java/DateFinder.java new file mode 100644 index 0000000000..5945b73f53 --- /dev/null +++ b/challenge-038/cheok-yin-fung/java/DateFinder.java @@ -0,0 +1,65 @@ +// The Weekly Challenge - 038 +// Task 1 Date Finder +// attempt: May 09th, 2021 +// from: C.-Y. Fung +// Usage: java DateFinder [7-digit string] + +import java.time.LocalDate; +import java.time.YearMonth; + +public class DateFinder +{ + public static void main(String[] args) + { + String myYear = "", in_str = ""; + try { + in_str = args[0]; + for (char numChar : in_str.toCharArray()) { + if (numChar < '0' || numChar> '9') { + System.out.println("Non-numeric character found."); + throw new IllegalArgumentException(); + } + } + switch(in_str.charAt(0)) { + case '1': myYear = "20"+in_str.substring(1,3); break; + case '2': myYear = "19"+in_str.substring(1,3); break; + default: throw new IllegalArgumentException(); + } + if (in_str.length() != 7) { + System.out.println("Input digits is not equal to 7."); + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException e) { + System.out.println("Ilegal argument."); + System.exit(0); + } catch (IndexOutOfBoundsException e) { + System.out.println("Usage: java DateFinder [7-digit string]"); + System.exit(0); + } + + String myMonth = in_str.substring(3,5); + int myMonth_i = Integer.parseInt(myMonth); + try { + if (myMonth_i > 12 || myMonth_i < 1) + throw new IllegalArgumentException(); + } catch (IllegalArgumentException e) { + System.out.println("Problem in Month (within 01-12) " + myMonth); + System.exit(0); + } + String myDay = in_str.substring(5,7); + int myDay_i = Integer.parseInt(myDay); + int myYear_i = Integer.parseInt(myYear); + try { + YearMonth temp = YearMonth.of(myYear_i, myMonth_i); + int daysInMonth = temp.lengthOfMonth(); + if (myDay_i > daysInMonth || myDay_i < 1) + throw new IllegalArgumentException(); + } catch (IllegalArgumentException e) { + System.out.println("Problem in date (last two digits) " + myDay); + System.exit(0); + } finally { + System.out.println(myYear + "-" + myMonth + "-" + myDay); + } + } + +} diff --git a/challenge-038/cheok-yin-fung/perl/ch-1.pl b/challenge-038/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..88f5e781f0 --- /dev/null +++ b/challenge-038/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl +# The Weekly Challenge - 038 +# Task 1 Date Finder +# attempt: May 09th, 2021 +# from: C.-Y. Fung +# Usage: ch-1.pl [7-digit string] +# Java correspondence: DateFinder.java +use Time::Piece; +use strict; +use warnings; + +my $str = $ARGV[0] || die "A 7-digit string is expected."; +die "Format Error: A 7-digit string is expected." if length $str != 7; + +$str =~ s/^1/20/; +$str =~ s/^2/19/; +die "Error: first digit must be 1 or 2! " if $str =~ /[^12]/; + +my $Y = substr($str, 0, 4); +my $Month = substr($str, 4, 2); +my $d = substr($str, 6, 2); + +die "Problem in month ($Month)! " + if $Month > 12 || $Month < 1; +my $check = Time::Piece->strptime(substr($str,0,6)."01" , "%Y%m%d"); +die "Problem in date (last two digits: $d)! " + if $d > $check->month_last_day || $d < 1; + +my $t = Time::Piece->strptime($str, "%Y%m%d"); + + +print $t->strftime("%Y-%m-%d"), "\n"; |
