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"