From 6c5766c62eb970c4c413597ccde4f38ec39054bc Mon Sep 17 00:00:00 2001 From: Ghom <42542238+Ghommie@users.noreply.github.com> Date: Tue, 12 Oct 2021 15:32:30 +0200 Subject: [PATCH] timepiece circuit module (#62039) Adds a circuit component that outputs the station time in chosen format and unit of time when triggered. The options are currently 24-hour and 12-hour for the time format, hours, minutes and seconds for the unit of time. The component is called timepiece because clock was already taken by another component that sends signal outputs at intervals. --- code/__HELPERS/time.dm | 16 +++++ .../research/designs/wiremod_designs.dm | 5 ++ code/modules/research/techweb/all_nodes.dm | 1 + .../wiremod/components/utility/timepiece.dm | 68 +++++++++++++++++++ tgstation.dme | 1 + 5 files changed, 91 insertions(+) create mode 100644 code/modules/wiremod/components/utility/timepiece.dm diff --git a/code/__HELPERS/time.dm b/code/__HELPERS/time.dm index a47a3aab017..9edb40bfd03 100644 --- a/code/__HELPERS/time.dm +++ b/code/__HELPERS/time.dm @@ -103,3 +103,19 @@ GLOBAL_VAR_INIT(rollovercheck_last_timeofday, 0) /proc/daysSince(realtimev) return round((world.realtime - realtimev) / (24 HOURS)) + +/** + * Converts a time expressed in deciseconds (like world.time) to the 12-hour time format. + * the format arg is the format passed down to time2text() (e.g. "hh:mm" is hours and minutes but not seconds). + * the timezone is the time value offset from the local time. It's to be applied outside time2text() to get the AM/PM right. + */ +/proc/time_to_twelve_hour(time, format = "hh:mm:ss", timezone = TIMEZONE_UTC) + time = MODULUS(time + (timezone - GLOB.timezoneOffset) HOURS, 24 HOURS) + var/am_pm = "AM" + if(time > 12 HOURS) + am_pm = "PM" + if(time > 13 HOURS) + time -= 12 HOURS // e.g. 4:16 PM but not 00:42 PM + else if (time < 1 HOURS) + time += 12 HOURS // e.g. 12.23 AM + return "[time2text(time, format)] [am_pm]" diff --git a/code/modules/research/designs/wiremod_designs.dm b/code/modules/research/designs/wiremod_designs.dm index cd349dee038..0ba15d00897 100644 --- a/code/modules/research/designs/wiremod_designs.dm +++ b/code/modules/research/designs/wiremod_designs.dm @@ -108,6 +108,11 @@ id = "comp_speech" build_path = /obj/item/circuit_component/speech +/datum/design/component/timepiece + name = "Timepiece Component" + id = "comp_timepiece" + build_path = /obj/item/circuit_component/timepiece + /datum/design/component/tostring name = "To String Component" id = "comp_tostring" diff --git a/code/modules/research/techweb/all_nodes.dm b/code/modules/research/techweb/all_nodes.dm index b76496f42b1..c594216f764 100644 --- a/code/modules/research/techweb/all_nodes.dm +++ b/code/modules/research/techweb/all_nodes.dm @@ -240,6 +240,7 @@ "comp_string_contains", "comp_tempsensor", "comp_textcase", + "comp_timepiece", "comp_tonumber", "comp_tostring", "comp_typecast", diff --git a/code/modules/wiremod/components/utility/timepiece.dm b/code/modules/wiremod/components/utility/timepiece.dm new file mode 100644 index 00000000000..6bc89faff0d --- /dev/null +++ b/code/modules/wiremod/components/utility/timepiece.dm @@ -0,0 +1,68 @@ +#define COMP_TIMEPIECE_TWENTYFOUR_HOUR "24-Hour" +#define COMP_TIMEPIECE_TWELVE_HOUR "12-Hour" +#define COMP_TIMEPIECE_SECONDS "Seconds" +#define COMP_TIMEPIECE_MINUTES "Minutes" +#define COMP_TIMEPIECE_HOURS "Hours" + +/** + * # Timepiece Component + * + * returns the current station time. + */ +/obj/item/circuit_component/timepiece + display_name = "Timepiece" + desc = "A component that outputs the current station time. The text output port is used for time formats while the numerical output port is used for units of time." + circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL + + /// The time format of the text output + var/datum/port/input/option/format + /// The unit of time for the numerical output + var/datum/port/input/option/time_unit + /// The output for 24/12 hour formats + var/datum/port/output/text_output + /// seconds, minutes, hours. + var/datum/port/output/num_output + +/obj/item/circuit_component/timepiece/populate_ports() + text_output = add_output_port("Time Format", PORT_TYPE_STRING) + num_output = add_output_port("Unit of Time", PORT_TYPE_NUMBER) + +/obj/item/circuit_component/timepiece/populate_options() + var/static/format_options = list( + COMP_TIMEPIECE_TWENTYFOUR_HOUR, // Station time is expressed in 24-h in the status tab. So this is the default. + COMP_TIMEPIECE_TWELVE_HOUR, + ) + format = add_option_port("Time Format", format_options) + var/static/unit_options = list( + COMP_TIMEPIECE_HOURS, + COMP_TIMEPIECE_MINUTES, + COMP_TIMEPIECE_SECONDS, + ) + time_unit = add_option_port("Unit of Time", unit_options) + +/obj/item/circuit_component/timepiece/input_received(datum/port/input/port) + var/time + + switch(format.value) + if(COMP_TIMEPIECE_TWENTYFOUR_HOUR) + time = station_time_timestamp() + if(COMP_TIMEPIECE_TWELVE_HOUR) + time = time_to_twelve_hour(station_time()) + + text_output.set_output(time) + + switch(time_unit.value) + if(COMP_TIMEPIECE_HOURS) + time = round(station_time() / (1 HOURS)) + if(COMP_TIMEPIECE_MINUTES) + time = round(station_time() / (1 MINUTES)) + if(COMP_TIMEPIECE_SECONDS) + time = round(station_time() / (1 SECONDS)) + + num_output.set_output(time) + +#undef COMP_TIMEPIECE_TWENTYFOUR_HOUR +#undef COMP_TIMEPIECE_TWELVE_HOUR +#undef COMP_TIMEPIECE_SECONDS +#undef COMP_TIMEPIECE_MINUTES +#undef COMP_TIMEPIECE_HOURS diff --git a/tgstation.dme b/tgstation.dme index 65e0ea801fb..0193ccbeac2 100644 --- a/tgstation.dme +++ b/tgstation.dme @@ -3921,6 +3921,7 @@ #include "code\modules\wiremod\components\utility\clock.dm" #include "code\modules\wiremod\components\utility\delay.dm" #include "code\modules\wiremod\components\utility\router.dm" +#include "code\modules\wiremod\components\utility\timepiece.dm" #include "code\modules\wiremod\components\utility\typecast.dm" #include "code\modules\wiremod\components\utility\typecheck.dm" #include "code\modules\wiremod\components\variables\getter.dm"