3 # season_math_test.py - convert from month to season and vice versa
5 # Copyright (C) 2015 Antonio Ospite <ao2@ao2.it>
7 # This program is free software. It comes without any warranty, to
8 # the extent permitted by applicable law. You can redistribute it
9 # and/or modify it under the terms of the Do What The Fuck You Want
10 # To Public License, Version 2, as published by Sam Hocevar. See
11 # http://sam.zoy.org/wtfpl/COPYING for more details.
13 # The code assumes meteorological seasons.
14 # See http://en.wikipedia.org/wiki/Season
16 seasons_norhtern = ["Winter", "Spring", "Summer", "Autumn"]
17 seasons_southern = ["Summer", "Autumn", "Winter", "Spring"]
19 # Default seasons order is the one for the Northern hemisphere
20 seasons = seasons_norhtern
22 months = ["January", "February", "March", "April", "May", "June", "July",
23 "August", "September", "October", "November", "December"]
27 if month < 0 or month > 11:
28 raise ValueError("Invalid month %d" % month)
30 # Rotate forward by one month because Winter starts in December,
31 # and group by three months because each season is three months long.
32 return ((month + 1) % 12) // 3
35 def first_month_of_season(season):
36 if season < 0 or season > 3:
37 raise ValueError("Invalid season %d" % season)
39 # Expand by three because a new season starts every 3 months,
40 # and rotate back by one month because December is the first
42 return (season * 3 - 1) % 12
48 except Exception as e:
52 if __name__ == "__main__":
55 test("first_month_of_season(-1)")
56 test("first_month_of_season(4)")
60 for i, m in enumerate(months):
62 print "Month: %s. (%d) \tSeason: %s (%d)" % (m[0:3], i, seasons[j], j)
66 for i, s in enumerate(seasons):
67 j = first_month_of_season(i)
68 print "Season: %s (%d)\tFirst month: %s (%d)" % (s, i, months[j], j)