colinsheppard / time

A NetLogo extension that brings date/time utilities and discrete event scheduling to NetLogo
12 stars 13 forks source link

execute monthly task at end of month produces weird result #63

Open manentai opened 6 months ago

manentai commented 6 months ago

the repeating event schedule produces a weird result if you try to execute a task monthly at the end of the month

check this code:

extensions [time table ]

globals [ month-end start-date tick-date ]

to setup
  ca

  crt 1
  ;; holds last day for each month
  set month-end table:make
  table:put month-end 1 31
  table:put month-end 2 28
  table:put month-end 3 31
  table:put month-end 4 30
  table:put month-end 5 31
  table:put month-end 6 30
  table:put month-end 7 31
  table:put month-end 8 31
  table:put month-end 9 30
  table:put month-end 10 31
  table:put month-end 11 30
  table:put month-end 12 31

  set start-date time:create-with-format "01-01-2024" "dd-MM-YYYY"
  set tick-date time:anchor-to-ticks start-date 1 "day"
  time:anchor-schedule start-date 1 "day"

  reset-ticks
end

to go

  ;; find end of month for the select start month
  let month time:get "month" tick-date
  let day table:get month-end month
  let year time:get "year" tick-date
  let end-of-month time:create-with-format (word day ifelse-value month < 10 ["-0"]["-"] month "-" year) "dd-MM-YYYY"

  ;time:schedule-repeating-event-with-period turtle 0 [ -> print tick-date ] end-of-month 2 "month"
  time:schedule-repeating-event-with-period turtle 0 [ -> print tick-date ] end-of-month 1 "month"

  time:go-until 300
end

assuming the code is good, if you execute every two months, the end of the month is observed, while with monthly scheduling something weird happens and the end of the second month is always used instead the proper end of month

A workaround suggested here: https://stackoverflow.com/questions/77742465/execute-monthly-task-at-end-of-month-with-netlogo-timeschedule-repeating-event/77743656#77743656