@@ -193,36 +193,80 @@ func calendar(w http.ResponseWriter, r *http.Request) {
193193 Zon = 10 // Default
194194 }
195195
196- type gridrow struct {
197- Date string
198- Moon riseset.RiseSet
199- Sun riseset.RiseSet
196+ // Determine which month to show, defaulting to the current month in the
197+ // user's timezone.
198+ zondur := time .Hour * time .Duration (Zon )
199+ now := time .Now ().Add (zondur )
200+
201+ year , err := strconv .Atoi (r .URL .Query ().Get ("year" ))
202+ if err != nil || year < 1 || year > 9999 {
203+ year = now .Year ()
204+ }
205+ month , err := strconv .Atoi (r .URL .Query ().Get ("month" ))
206+ if err != nil || month < 1 || month > 12 {
207+ month = int (now .Month ())
208+ }
209+
210+ // Previous / next month navigation (handles year rollovers).
211+ prevMonth , prevYear := month - 1 , year
212+ if prevMonth < 1 {
213+ prevMonth = 12
214+ prevYear --
215+ }
216+ nextMonth , nextYear := month + 1 , year
217+ if nextMonth > 12 {
218+ nextMonth = 1
219+ nextYear ++
200220 }
201221
202- var arow gridrow
203- var newdate time.Time
222+ // Today's date string in local time, used to highlight the current row.
223+ today := now .Format ("02-01-2006" )
224+
225+ // Last day of the requested month: day 0 of the following month.
226+ lastDay := time .Date (year , time .Month (month + 1 ), 0 , 0 , 0 , 0 , 0 , time .UTC ).Day ()
227+
228+ type gridrow struct {
229+ Date string
230+ Moon riseset.RiseSet
231+ Sun riseset.RiseSet
232+ IsToday bool
233+ }
204234
205235 type mypar struct {
206- Rows []gridrow
207- Lon float64
208- Lat float64
209- Zon float64
236+ Rows []gridrow
237+ Lon float64
238+ Lat float64
239+ Zon float64
240+ Year int
241+ Month int
242+ MonthName string
243+ PrevYear int
244+ PrevMonth int
245+ NextYear int
246+ NextMonth int
210247 }
211248
212249 var Passme mypar
213250 Passme .Lat = Lat
214251 Passme .Lon = Lon
215252 Passme .Zon = Zon
216-
217- var zondur time.Duration = time .Hour * time .Duration (Zon )
218- newdate = time .Now ().Add (zondur )
219-
220- for i := 0 ; i < 10 ; i ++ {
221- newdate = newdate .AddDate (0 , 0 , 1 )
222- arow .Date = newdate .Format ("02-01-2006" )
223- arow .Moon = riseset .Riseset (riseset .Moon , newdate , Lon , Lat , Zon )
224- arow .Sun = riseset .Riseset (riseset .Sun , newdate , Lon , Lat , Zon )
225- Passme .Rows = append (Passme .Rows , arow )
253+ Passme .Year = year
254+ Passme .Month = month
255+ Passme .MonthName = time .Month (month ).String ()
256+ Passme .PrevYear = prevYear
257+ Passme .PrevMonth = prevMonth
258+ Passme .NextYear = nextYear
259+ Passme .NextMonth = nextMonth
260+
261+ for day := 1 ; day <= lastDay ; day ++ {
262+ d := time .Date (year , time .Month (month ), day , 0 , 0 , 0 , 0 , time .UTC )
263+ dateStr := d .Format ("02-01-2006" )
264+ Passme .Rows = append (Passme .Rows , gridrow {
265+ Date : dateStr ,
266+ Moon : riseset .Riseset (riseset .Moon , d , Lon , Lat , Zon ),
267+ Sun : riseset .Riseset (riseset .Sun , d , Lon , Lat , Zon ),
268+ IsToday : dateStr == today ,
269+ })
226270 }
227271
228272 if templates != nil {
0 commit comments