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 # Default seasons order is the one for the Northern hemisphere.
18 seasons_norhtern = ["Winter", "Spring", "Summer", "Fall"]
19 seasons_southern = ["Summer", "Fall", "Winter", "Spring"]
21 seasons = seasons_norhtern
23 months = ["January", "February", "March", "April", "May", "June", "July",
24 "August", "September", "October", "November", "December"]
28 if month < 0 or month > 11:
29 raise ValueError("Invalid month %d" % month)
31 # Rotate forward by one month because Winter starts in December,
32 # and group by three months because each season is three months long.
33 return ((month + 1) % 12) // 3
36 def first_month_of_season(season):
37 if season < 0 or season > 3:
38 raise ValueError("Invalid season %d" % season)
40 # Expand by three because a new season starts every 3 months,
41 # and rotate back by one month because December is the first
43 return (season * 3 - 1) % 12
49 except Exception as e:
53 if __name__ == "__main__":
56 test("first_month_of_season(-1)")
57 test("first_month_of_season(4)")
61 for i, m in enumerate(months):
63 print "Month: %s. (%d) \tSeason: %s (%d)" % (m[0:3], i, seasons[j], j)
67 for i, s in enumerate(seasons):
68 j = first_month_of_season(i)
69 print "Season: %s (%d)\tFirst month: %s (%d)" % (s, i, months[j], j)