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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay