# Winter starts the previous year if the months are January (0) of February (1)
start_of_season(x) = strptime("%Y-%m-%d", "" . (int(tm_year(x)) - (int(tm_mon(x)) < 2)) . "-" . int(first_month_of_season(season(tm_mon(x))) + 1) . "-21")
+# Calculate the difference between two dates.
+months_between_dates(a, b) = int((tm_year(b) - tm_year(a)) * 12 + tm_mon(b) - tm_mon(a))
+
+# One season is 3 months.
+seasons_between_dates(a, b) = int(months_between_dates(a, b) / 3)
+
# Dummy plot to gather some stats from the dataset
set terminal unknown
plot 'data.dat' using 1:2
set yrange [ymin:ymax]
# Calculate the x sampling rate for the '+' plot below.
+# Have one sample per season, the +1 is to include the season of xmax.
# This way we draw only the strictly needed boxxys for the background.
-#
-# Divide by three_months in order to have intervals of three months each and
-# round x_samples to the next multiple of 3 because the samples will be
-# divided by 3 in the season() function to set the season color.
-x_samples = ceil((xmax - xmin) / three_months / 3) * 3
+x_samples = seasons_between_dates(xmin, xmax) + 1
set samples x_samples, 100
+# NOTE that the ticks will be placed at the beginning of months regardless of
+# the day of the month of xmin. See:
+# https://stackoverflow.com/questions/42212039/gnuplot-xtic-labels-mispositioning-with-dates
set xtics xmin,three_months
set terminal qt 0