DEV Community

cGIfl300
cGIfl300

Posted on

Seasons in Dart

Few lines of code to get the actual season, I absolutely needed that on my computer. Sure, you need too.

#!/usr/bin/env dart

class Translate {
  String spring;
  String summer;
  String autumn;
  String winter;
  Translate({this.spring = "Printemps",
            this.summer = "Été",
            this.autumn = "Automne",
            this.winter = "Hiver"});
  }

class LastSeasonDate {
  int month;
  int day;
  LastSeasonDate({this.month = 0, this.day = 0});
  }

Translate translate = Translate();

String getSeason() {
  DateTime now = DateTime.now();
  int day = now.day;
  int month = now.month;

  LastSeasonDate endWinterDate = LastSeasonDate(month: 3, day: 20);
  LastSeasonDate endSpringDate = LastSeasonDate(month: 6, day: 20);
  LastSeasonDate endSummerDate = LastSeasonDate(month: 9, day: 20);
  LastSeasonDate endAutumnDate = LastSeasonDate(month: 12, day: 20);

  if ((month == endWinterDate.month && day > endWinterDate.day)
      || (month < endSpringDate.month )
      || (month == endSpringDate.month && day < endSpringDate.day + 1)) {
    return translate.spring;
  } else if ((month == endSpringDate.month && day > endSpringDate.day)
      || (month < endSummerDate.month)
      || (month == endSummerDate.month && day < endSummerDate.day + 1)) {
    return translate.summer;
  } else if ((month == endSummerDate.month && day > endSummerDate.day)
      || (month < endAutumnDate.month)
      || (month == endAutumnDate.month && day < endAutumnDate.day + 1)) {
    return translate.autumn;
  } else {
    return translate.winter;
  }
}

void main() {
  print(getSeason());
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)