Libraries‎ > ‎Library‎ > ‎Miscellaneous‎ > ‎

clock

BehaviourComposer: ignore everything before this.

Begin micro-behaviour:

Begin description:

CLOCK updates parameters you can use to schedule behaviours according to a real-world notion of time e.g. go to work at 6pm each weekday evening.

End description

CLOCK

Begin NetLogo code:

set the-minute the-minute + 1set the-minutes-from-start the-minutes-from-start + 1 if the-minute = 60 [    set the-hour the-hour + 1    set the-minute 0    ]if the-hour = 24 [   set the-hour 0   set the-days-from-start the-days-from-start + 1   set the-day-in-year the-day-in-year + 1   set the-weekday the-weekday + 1    ]if the-day-in-year = 366 [   set the-day-in-year 0    set the-years-from-start the-year-from-start + 1   ]if the-weekday = 7 [ set the-weekday 0 ]if the-weekday = 0 [ set the-weekday-text "monday" ]if the-weekday = 1 [ set the-weekday-text "tuesday" ]if the-weekday = 2 [ set the-weekday-text "wednesday" ]if the-weekday = 3 [ set the-weekday-text "thursday" ]if the-weekday = 4 [ set the-weekday-text "friday" ]if the-weekday = 5 [ set the-weekday-text "saturday" ]if the-weekday = 6 [ set the-weekday-text "sunday" ]ifelse the-weekday = 5 or the-weekday = 6    [ set the-weekend? true ]   [ set the-weekend? false ]

End NetLogo code

How this works

The behaviour simply increments the-minute global parameter variable by 1 each tick of the simulation. It then increments and restarts other values accordingly e.g. when minutes reaches 60 the-hour is incremented by 1 until the-hour reaches 24 when it is reset to zero to correspond with a day having past. It also keeps a record of how many minutes, hours, days and years have gone past since the clock was started. The final few lines of code map the numerical value of weekday to text e.g. 0 = Sunday and sets the-weekend? to true if it is a Saturday or Sunday.  

The behaviour includes the following list of time-related parameters:
  • the-minute
  • the-hour
  • the-day-in-year
  • the-weekday
  • the-weekday-text
  • the-weekend? 
  • the-minutes-from-start
  • the-hours-from-start
  • the-days-from-start
  • the-years-from-start
You may want to copy this behaviour and comment-out some sections to improve performance.

Don't forget to add the 'do repeatedly' enhancement to keep your clock running each tick!

Variants

You may want to copy this micro-behaviour and change the code:
  • So that the smallest time increment is shorter (e.g. a second) or longer (e.g. a day)
  • To add months and holidays
  • To stop the simulation at a specific date e.g. each year using something like if the-day-in-year = 365 [ stop ]

Related micro-behaviours

You can use CREATE-MONITOR micro-behaviour to display the clock parameters e.g.  the-hour. 

History

Implemented by Howard Noble, November 2011

BehaviourComposer: ignore everything after this.