Game Time gives the server an in-game clock and calendar, as opposed to the Time, Date and DateTime classes, which deal with the real world. The game calendar is the Gregorian one, with months of their usual lengths, leap years and weekdays, for the years from 1 to 9999. A moment of game time is a timestamp: the number of game seconds since midnight of 1 January 1970, negative for the moments before it. It has no time zone. GameTime:to_timestamp builds one out of a date, GameTime:get_date takes one apart, and GameTime:parse and GameTime:to_string convert between a timestamp and text such as '2020-01-01 08:00'.

The server owns the clock. It starts at the date of the 'gametime_start' config, advances one game minute every 'gametime_minute_length' real seconds and is saved with the rest of the persistent data, so it carries on after a restart or a change of map. While the server is empty and hibernates the clock stands still. With the 'gametime_real_time' config the clock shows the real date and time of the server instead; the game time that was saved is kept, and the clock goes back to it when the config is turned off. Staff set the clock with the SetTime command, code with GameTime:set.

The clock is networked as the moment it was last set together with its speed, in the 'gametime' networked global, and every client works the current time out by itself, so nothing is sent while the clock just runs. GameTime:now, GameTime:get_date and the format functions therefore work on the server and the client alike.

-- 'Wednesday, 1 January 2020, 08:00'
local text = GameTime:format()

-- Close the shops for the night.
function MyPlugin:GameHourPassed(current, previous)
  if current.hour == 22 then
    self:close_shops()
  end
end

The GameMinutePassed, GameHourPassed, GameDayPassed, GameMonthPassed and GameYearPassed hooks run on both realms as the clock rolls over, and GameTimeSet runs when the clock is set to another moment.

Players see the time and the date in the top right corner of the tab menu. With the Settings plugin loaded they can hide that clock and switch it to the 12-hour format; the ShouldDrawGameTime hook lets a schema hide it, and the PaintGameTimeClock theme hook lets a theme draw it.

Summary

Hooks

Puts the clock at the right end of the button bar of the tab menu.
Notices that the server has set the game clock to another moment, which is when the serial of the networked clock changes: makes the rollover hooks start over from the new time and runs GameTimeSet.
Checks eight times a second whether the game clock has rolled over.
Starts the game clock when the persistent data is loaded: at the real time of the server with the 'gametime_real_time' config, otherwise at the time that was saved, or at the 'gametime_start' config if nothing has been saved yet.
Applies the changes of the 'gametime_minute_length' and 'gametime_real_time' configs to the running clock.
Keeps a clock that follows the real time of the server on that time, by correcting it once it is two seconds or more away.
Registers the settings of the clock with the Settings plugin: 'gametime_clock', which shows or hides the clock of the tab menu, and 'gametime_twelve_hour', which switches the game time to the 12-hour format.
Saves the game time with the rest of the persistent data.

Methods

check_rollover()shared internal
Compares the game time with the last minute that was seen and runs the hooks of the minute, hour, day, month and year that have begun since.
Returns the amount of days in a month of the game calendar.
Formats the date and the time of day of a timestamp, such as 'Wednesday, 1 January 2020, 08:00'.
Formats the date of a timestamp, such as 'Wednesday, 1 January 2020', or '01/01/2020' in the short form.
Formats the time of day of a timestamp, such as '17:05' or '5:05 PM'.
Returns the clock as the server has networked it: the moment it was last set to and how fast it runs since.
Takes a timestamp apart into a date and a time of day of the game calendar.
get_day()shared
Returns the current day of the month of the game calendar.
Returns the current hour of the game clock.
Returns the current minute of the game clock.
Returns the current month of the game calendar.
Returns the name of a month the way it is written in a date.
Returns how fast the game clock runs.
Returns the real date and time of the server, in its own time zone, as a timestamp of the game calendar.
Returns the moment the game clock starts from when no game time has been saved: the 'gametime_start' config, or 08:00 of 1 January 2020 if the config cannot be read as a date.
Returns the current day of the week of the game calendar.
Returns the name of a day of the week.
Returns the current year of the game calendar.
Checks whether a year of the game calendar is a leap year.
Checks whether the clock is running: on the server once the saved data has been loaded, on the client once the clock has arrived from the server.
Checks whether the game clock follows the real date and time of the server, which is what the 'gametime_real_time' config makes it do.
Checks whether the numbers make up a date and a time of day that exist in the game calendar: whole numbers, a year from 1 to 9999, a month from 1 to 12, a day that the month has, an hour from 0 to 23 and a minute and a second from 0 to 59.
load()server
Reads the game time that GameTime:save has written for the schema.
now()shared
Returns the current game time.
Reads a timestamp from text: a date as 'YYYY-MM-DD', a time of day as 'HH:MM' or 'HH:MM:SS', or a date followed by a time of day.
read_clock(clock)shared internal
Works out the timestamp that a clock shows at the moment.
save()server
Writes the current game time to the data of the schema, as a date and a time of day.
Sets the game clock and saves the new time.
Puts the clock at a timestamp from this moment on and networks it to everyone.
Checks whether the clock of the tab menu should be drawn: the game clock has arrived from the server, the player has not turned the 'gametime_clock' setting off and the ShouldDrawGameTime hook does not object.
Writes a timestamp as 'YYYY-MM-DD HH:MM', which reads the same in every language and is what GameTime:parse takes.
Converts a date and a time of day of the game calendar to a timestamp.
Checks whether times are shown with a 12-hour clock by default.

Hooks

GameTime:AddTabMenuItems(menu)

client plugins/gametime/plugin/cl_hooks.lua:71

Implements hook AddTabMenuItems

Puts the clock at the right end of the button bar of the tab menu.

Parameters

  • menu Panel the tab menu

GameTime:GlobalNetVarChanged(key, old_value, new_value)

client plugins/gametime/plugin/cl_hooks.lua:52

Implements hook GlobalNetVarChanged

Notices that the server has set the game clock to another moment, which is when the serial of the networked clock changes: makes the rollover hooks start over from the new time and runs GameTimeSet. A clock that arrives for the first time, changes its speed or is corrected keeps its serial.

Parameters

  • key String name of the networked global that has changed
  • old_value Any previous value, the previous clock in the case of the game clock
  • new_value Any new value, the new clock in the case of the game clock

GameTime:LazyTick()

shared plugins/gametime/plugin/sh_plugin.lua:570

Implements hook LazyTick

Checks eight times a second whether the game clock has rolled over.

GameTime:LoadData()

server plugins/gametime/plugin/sv_plugin.lua:182

Implements hook LoadData

Starts the game clock when the persistent data is loaded: at the real time of the server with the 'gametime_real_time' config, otherwise at the time that was saved, or at the 'gametime_start' config if nothing has been saved yet.

GameTime:OnConfigSet(key, _old_value, new_value)

server plugins/gametime/plugin/sv_plugin.lua:210

Implements hook OnConfigSet

Applies the changes of the 'gametime_minute_length' and 'gametime_real_time' configs to the running clock. A new minute length changes the speed from the current moment on. Turning the real time on saves the game time and moves the clock to the real time of the server. Turning it off moves the clock back to the game time that was saved, the same moment it would start from after a restart, or to the 'gametime_start' config if nothing has been saved. Both count as setting the clock and run GameTimeSet. The new value is compared with the running clock rather than with the old value of the config, because a code refresh puts the configs back to their defaults without telling the plugin.

Parameters

  • key String config key
  • _old_value Any value the config had
  • new_value Any value the config is about to get

GameTime:OneSecond()

server plugins/gametime/plugin/sv_plugin.lua:237

Implements hook OneSecond

Keeps a clock that follows the real time of the server on that time, by correcting it once it is two seconds or more away. That happens when the server has been hibernating or when its own clock has been changed. A correction that takes the clock back by more than a minute counts as setting it, so that the rollover hooks start over.

GameTime:RegisterClientSettings()

client plugins/gametime/plugin/cl_hooks.lua:7

Implements hook RegisterClientSettings

Registers the settings of the clock with the Settings plugin: 'gametime_clock', which shows or hides the clock of the tab menu, and 'gametime_twelve_hour', which switches the game time to the 12-hour format. Only called when the Settings plugin is loaded.

GameTime:SaveData()

server plugins/gametime/plugin/sv_plugin.lua:195

Implements hook SaveData

Saves the game time with the rest of the persistent data.

Methods

GameTime:check_rollover()

shared internal plugins/gametime/plugin/sh_plugin.lua:491

Internal

This function is internal. It is not part of the public API and may change without notice.

Compares the game time with the last minute that was seen and runs the hooks of the minute, hour, day, month and year that have begun since. Every hook runs once per check, no matter how much game time has gone by, and only as the clock moves forward: setting it to another moment runs GameTimeSet instead. The client starts checking once the local player has been initialized.

GameTime:days_in_month(year, month)

shared plugins/gametime/plugin/sh_plugin.lua:125

Returns the amount of days in a month of the game calendar.

Parameters

  • year Number year, which decides the length of February
  • month Number month, from 1 to 12

Returns

  • Number amount of days, nil if there is no such month

GameTime:format(timestamp, twelve_hour, lang)

shared plugins/gametime/plugin/sh_plugin.lua:417

Formats the date and the time of day of a timestamp, such as 'Wednesday, 1 January 2020, 08:00'.

Parameters

  • timestamp Number optional, defaults to GameTime:now() game seconds since midnight of 1 January 1970
  • twelve_hour Boolean optional, defaults to GameTime:uses_twelve_hour() use the 12-hour clock
  • lang String optional, defaults to nil language code, the current language by default

Returns

  • String

See also

GameTime:format_date(timestamp, short, lang)

shared plugins/gametime/plugin/sh_plugin.lua:387

Formats the date of a timestamp, such as 'Wednesday, 1 January 2020', or '01/01/2020' in the short form. The order and the punctuation come from the language.

Parameters

  • timestamp Number optional, defaults to GameTime:now() game seconds since midnight of 1 January 1970
  • short Boolean optional, defaults to false write the date with numbers only
  • lang String optional, defaults to nil language code, the current language by default

Returns

  • String

GameTime:format_time(timestamp, twelve_hour, lang)

shared plugins/gametime/plugin/sh_plugin.lua:360

Formats the time of day of a timestamp, such as '17:05' or '5:05 PM'.

Parameters

  • timestamp Number optional, defaults to GameTime:now() game seconds since midnight of 1 January 1970
  • twelve_hour Boolean optional, defaults to GameTime:uses_twelve_hour() use the 12-hour clock
  • lang String optional, defaults to nil language code, the current language by default

Returns

  • String

GameTime:get_clock()

shared plugins/gametime/plugin/sh_plugin.lua:224

Returns the clock as the server has networked it: the moment it was last set to and how fast it runs since.

Returns

  • Map the clock with the fields: base (Number timestamp the clock was set to), anchor (Number CurTime() at which it was set), rate (Number game seconds that pass in a real second), real (Boolean whether it follows the real time of the server) and serial (Number that grows every time the clock is set to another moment); nil while the server has not started the clock or the client has not received it

GameTime:get_date(timestamp)

shared plugins/gametime/plugin/sh_plugin.lua:196

Takes a timestamp apart into a date and a time of day of the game calendar.

local date = GameTime:get_date()

if date.weekday >= 6 then
  print('It is the weekend, and '..date.hour..' o\'clock.')
end

Parameters

  • timestamp Number optional, defaults to GameTime:now() game seconds since midnight of 1 January 1970

Returns

  • Map the date with the fields: year, month (1 to 12), day (of the month), hour (0 to 23), minute, second (whole, 0 to 59), weekday (1 for Monday to 7 for Sunday) and timestamp (the timestamp that was taken apart)

See also

GameTime:get_day()

shared plugins/gametime/plugin/sh_plugin.lua:293

Returns the current day of the month of the game calendar.

Returns

  • Number

GameTime:get_hour()

shared plugins/gametime/plugin/sh_plugin.lua:305

Returns the current hour of the game clock.

Returns

  • Number hour, from 0 to 23

GameTime:get_minute()

shared plugins/gametime/plugin/sh_plugin.lua:311

Returns the current minute of the game clock.

Returns

  • Number minute, from 0 to 59

GameTime:get_month()

shared plugins/gametime/plugin/sh_plugin.lua:287

Returns the current month of the game calendar.

Returns

  • Number month, from 1 to 12

GameTime:get_month_name(month, lang)

shared plugins/gametime/plugin/sh_plugin.lua:319

Returns the name of a month the way it is written in a date.

Parameters

  • month Number month, from 1 to 12
  • lang String optional, defaults to nil language code, the current language by default

Returns

  • String translated name, an empty string if there is no such month

GameTime:get_rate()

shared plugins/gametime/plugin/sh_plugin.lua:264

Returns how fast the game clock runs.

Returns

  • Number game seconds that pass in a real second; 1 while the clock is not ready

GameTime:get_real_timestamp()

server plugins/gametime/plugin/sv_plugin.lua:48

Returns the real date and time of the server, in its own time zone, as a timestamp of the game calendar.

Returns

  • Number game seconds since midnight of 1 January 1970

GameTime:get_start()

server plugins/gametime/plugin/sv_plugin.lua:29

Returns the moment the game clock starts from when no game time has been saved: the 'gametime_start' config, or 08:00 of 1 January 2020 if the config cannot be read as a date.

Returns

  • Number game seconds since midnight of 1 January 1970

GameTime:get_weekday()

shared plugins/gametime/plugin/sh_plugin.lua:299

Returns the current day of the week of the game calendar.

Returns

  • Number 1 for Monday to 7 for Sunday

GameTime:get_weekday_name(weekday, lang)

shared plugins/gametime/plugin/sh_plugin.lua:333

Returns the name of a day of the week.

Parameters

  • weekday Number 1 for Monday to 7 for Sunday
  • lang String optional, defaults to nil language code, the current language by default

Returns

  • String translated name, an empty string if there is no such day

GameTime:get_year()

shared plugins/gametime/plugin/sh_plugin.lua:281

Returns the current year of the game calendar.

Returns

  • Number

GameTime:is_leap_year(year)

shared plugins/gametime/plugin/sh_plugin.lua:117

Checks whether a year of the game calendar is a leap year.

Parameters

  • year Number

Returns

  • Boolean

GameTime:is_ready()

shared plugins/gametime/plugin/sh_plugin.lua:231

Checks whether the clock is running: on the server once the saved data has been loaded, on the client once the clock has arrived from the server.

Returns

  • Boolean

GameTime:is_real_time()

shared plugins/gametime/plugin/sh_plugin.lua:273

Checks whether the game clock follows the real date and time of the server, which is what the 'gametime_real_time' config makes it do.

Returns

  • Boolean

GameTime:is_valid_date(year, month, day, hour, minute, second)

shared plugins/gametime/plugin/sh_plugin.lua:143

Checks whether the numbers make up a date and a time of day that exist in the game calendar: whole numbers, a year from 1 to 9999, a month from 1 to 12, a day that the month has, an hour from 0 to 23 and a minute and a second from 0 to 59.

Parameters

  • year Number
  • month Number
  • day Number
  • hour Number optional, defaults to 0
  • minute Number optional, defaults to 0
  • second Number optional, defaults to 0

Returns

  • Boolean

Reads the game time that GameTime:save has written for the schema.

Returns

  • Number game seconds since midnight of 1 January 1970, or nil if nothing has been saved yet or the saved date cannot be read

Returns the current game time.

-- The game time in two game hours.
local closing_time = GameTime:now() + 2 * 60 * 60

Returns

  • Number game seconds since midnight of 1 January 1970, with a fraction; 0 while the clock is not ready

See also

GameTime:parse(text, relative_to)

shared plugins/gametime/plugin/sh_plugin.lua:451

Reads a timestamp from text: a date as 'YYYY-MM-DD', a time of day as 'HH:MM' or 'HH:MM:SS', or a date followed by a time of day. What the text leaves out is taken from another moment: a date alone keeps its hour and minute, a time of day alone its date.

GameTime:parse('2020-01-01 08:00')
-- Half past nine in the evening of the current game day.
GameTime:parse('21:30')
-- The same time of day as now, on another date.
GameTime:parse('2021-06-15')

Parameters

  • text String text to read
  • relative_to Number optional, defaults to GameTime:now() timestamp that fills in what the text leaves out

Returns

  • Number game seconds since midnight of 1 January 1970, or nil if the text is not a date or a time that exists in the game calendar

See also

GameTime:read_clock(clock)

shared internal plugins/gametime/plugin/sh_plugin.lua:239

Internal

This function is internal. It is not part of the public API and may change without notice.

Works out the timestamp that a clock shows at the moment.

Parameters

Returns

  • Number game seconds since midnight of 1 January 1970

Writes the current game time to the data of the schema, as a date and a time of day. Nothing is written while the clock follows the real time of the server, so the game time that was saved before stays there for when the 'gametime_real_time' config is turned off again.

GameTime:set(timestamp)

server plugins/gametime/plugin/sv_plugin.lua:121

Sets the game clock and saves the new time. Refused while the clock follows the real time of the server.

-- Skip to the next morning.
local date = GameTime:get_date()

GameTime:set(GameTime:to_timestamp(date.year, date.month, date.day + 1, 8))

-- Set the clock from text.
local timestamp = GameTime:parse('2020-01-01 08:00')

if timestamp then
  GameTime:set(timestamp)
end

Parameters

  • timestamp Number game seconds since midnight of 1 January 1970, within the years from 1 to 9999

Returns

  • Boolean whether the clock has been set
  • String language phrase of the error if it has not: 'error.gametime.not_ready', 'error.gametime.real_time' or 'error.gametime.invalid'

See also

GameTime:set_clock(timestamp, rate, real_time, is_jump)

server internal plugins/gametime/plugin/sv_plugin.lua:64

Internal

This function is internal. It is not part of the public API and may change without notice.

Puts the clock at a timestamp from this moment on and networks it to everyone. A jump makes the rollover hooks start over from the new time and runs GameTimeSet; without it the hooks carry on as if the clock had simply run on.

Parameters

  • timestamp Number game seconds since midnight of 1 January 1970 that the clock shows from now on
  • rate Number game seconds that pass in a real second
  • real_time Boolean whether the clock follows the real time of the server
  • is_jump Boolean optional, defaults to false the clock is being set to another moment, as opposed to being started, changing its speed or being corrected

GameTime:should_draw_clock()

client plugins/gametime/plugin/cl_hooks.lua:29

Checks whether the clock of the tab menu should be drawn: the game clock has arrived from the server, the player has not turned the 'gametime_clock' setting off and the ShouldDrawGameTime hook does not object.

Returns

  • Boolean

GameTime:to_string(timestamp)

shared plugins/gametime/plugin/sh_plugin.lua:430

Writes a timestamp as 'YYYY-MM-DD HH:MM', which reads the same in every language and is what GameTime:parse takes.

Parameters

  • timestamp Number optional, defaults to GameTime:now() game seconds since midnight of 1 January 1970

Returns

  • String

GameTime:to_timestamp(year, month, day, hour, minute, second)

shared plugins/gametime/plugin/sh_plugin.lua:176

Converts a date and a time of day of the game calendar to a timestamp. The numbers are not checked: use GameTime:is_valid_date on anything that comes from a player. A day, hour, minute or second past its usual range runs on into the next month, day, hour or minute, which makes for simple date arithmetic; the month has to be from 1 to 12.

-- Noon of 24 December 2020.
local timestamp = GameTime:to_timestamp(2020, 12, 24, 12)

-- Eight in the morning of the next game day, also on the last day of a month.
local date = GameTime:get_date()
local morning = GameTime:to_timestamp(date.year, date.month, date.day + 1, 8)

Parameters

  • year Number year
  • month Number optional, defaults to 1 month, from 1 to 12
  • day Number optional, defaults to 1 day of the month
  • hour Number optional, defaults to 0 hour, from 0 to 23
  • minute Number optional, defaults to 0 minute, from 0 to 59
  • second Number optional, defaults to 0 second, from 0 to 59

Returns

  • Number game seconds since midnight of 1 January 1970

See also

GameTime:uses_twelve_hour()

shared plugins/gametime/plugin/sh_plugin.lua:347

Checks whether times are shown with a 12-hour clock by default. On the client that is the 'gametime_twelve_hour' setting of the player, which exists when the Settings plugin is loaded. The server always uses the 24-hour clock.

Returns

  • Boolean

Defined in