Skip to content

Commit

Permalink
Use extensions to translate str to month number
Browse files Browse the repository at this point in the history
  • Loading branch information
FlashOnFire committed Oct 21, 2023
1 parent 72ee081 commit c564a39
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
46 changes: 32 additions & 14 deletions packages/polytechcolloscopeclient/lib/src/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,37 @@ class Consts {
Year.first: "$_base?page=colles_1A&id_etudiant=:id",
Year.second: "$_base?page=colles_2A&id_etudiant=:id"
};
}

static const Map monthsTranslation = {
"janvier": DateTime.january,
"février": DateTime.february,
"mars": DateTime.march,
"avril": DateTime.april,
"mai": DateTime.may,
"juin": DateTime.june,
"juillet": DateTime.july,
"août": DateTime.august,
"septembre": DateTime.september,
"octobre": DateTime.october,
"novembre": DateTime.november,
"décembre": DateTime.december
};
extension MonthsTranslation on String {
int get asMonthNumber {
switch (this) {
case "janvier":
return DateTime.january;
case "février":
return DateTime.february;
case "mars":
return DateTime.march;
case "avril":
return DateTime.april;
case "mai":
return DateTime.may;
case "juin":
return DateTime.june;
case "juillet":
return DateTime.july;
case "août":
return DateTime.august;
case "septembre":
return DateTime.september;
case "octobre":
return DateTime.october;
case "novembre":
return DateTime.november;
case "décembre":
return DateTime.december;
default:
throw StateError("Invalid month");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class PolytechColloscopeClient {

var dateParsed = RegExp(r"(\d{1,2}) (.{3,9})").firstMatch(date)!;
var day = dateParsed.group(1)!;
var month = Consts.monthsTranslation[dateParsed.group(2)];
var month = dateParsed.group(2)!.asMonthNumber;

var hourAndMinutesParsed =
RegExp(r"(\d{1,2}) h (\d{2})").firstMatch(hourAndMinute)!;
Expand Down

0 comments on commit c564a39

Please sign in to comment.