1212
1313class ctwc_helper
1414{
15- /** @var \phpbb\user */
16- protected $ user ;
17-
18- /** @var \phpbb\template\template */
19- protected $ template ;
20-
21- /** @var \phpbb\language\language */
22- protected $ language ;
15+ protected object $ user ;
16+ protected object $ template ;
17+ protected object $ language ;
2318
2419 /**
2520 * Constructor
@@ -39,7 +34,7 @@ public function __construct
3934 /**
4035 * Options to pick a timezone and date/time
4136 */
42- public function timezone_select ($ prefix = '' , $ default = '' )
37+ public function timezone_select (string $ prefix = '' , string $ default = '' ): void
4338 {
4439 $ default_offset = '' ;
4540
@@ -110,6 +105,9 @@ public function timezone_select($prefix = '', $default = '')
110105
111106 /*
112107 * Creates an array of variables for the SelectBox macro
108+ *
109+ * The variable $cfg_value is a union type array|string
110+ * Not specified for reasons of compatibility with php 7
113111 */
114112 public function select_struct ($ cfg_value , array $ options ): array
115113 {
@@ -135,7 +133,7 @@ public function select_struct($cfg_value, array $options): array
135133 /*
136134 * Creates an array of dateformat options for the SelectBox macro
137135 */
138- public function set_select_template_vars ($ date_format , $ template_var )
136+ public function set_select_template_vars (string $ date_format , string $ template_var ): void
139137 {
140138 $ this ->language ->add_lang (['info_acp_ctwc ' , ], 'imcger/currenttime ' );
141139
@@ -154,4 +152,59 @@ public function set_select_template_vars($date_format, $template_var)
154152 $ template_var => $ this ->select_struct ($ date_format , $ dateformat_options ),
155153 ]);
156154 }
155+
156+ /*
157+ * Changes the placeholder in the date/time string for the JavaScript clock class
158+ */
159+ public function generate_datetime_str (string $ replacement , string $ format ): string
160+ {
161+ $ pattern = [
162+ '/(?<! \\\\)g/ ' , // hour in 12-hour format; without leading zero 1 to 12
163+ '/(?<! \\\\)G/ ' , // hour in 24-hour format; without leading zero 0 to 23
164+ '/(?<! \\\\)h/ ' , // hour in 12-hour format; with leading zero 01 to 12
165+ '/(?<! \\\\)H/ ' , // hour in 24-hour format; with leading zero 00 to 23
166+ '/(?<! \\\\)i/ ' , // minutes; with leading zero 00 to 59
167+ '/(?<! \\\\)s/ ' , // seconds; with leading zero 00 to 59
168+ '/(?<! \\\\)a/ ' , // Lowercase Ante meridiem and Post meridiem am or pm
169+ '/(?<! \\\\)A/ ' , // Uppercase Ante meridiem and Post meridiem AM or PM
170+ '/(?<! \\\\)y/ ' , // A two digit representation of a year
171+ '/(?<! \\\\)Y/ ' , // A full numeric representation of a year
172+ '/(?<! \\\\)n/ ' , // Numeric representation of a month, without leading zeros 1 through 12
173+ '/(?<! \\\\)m/ ' , // Numeric representation of a month, with leading zeros 01 through 12
174+ '/(?<! \\\\)M/ ' , // A short textual representation of a month, three letters Jan through Dec
175+ '/(?<! \\\\)jS/ ' , // Day of the month with suffix and without leading zeros 1st to 31st
176+ '/(?<! \\\\)j/ ' , // Day of the month without leading zeros 1 to 31
177+ '/(?<! \\\\)d/ ' , // Day of the month, 2 digits with leading zeros 01 to 31
178+ '/(?<! \\\\)D/ ' , // A textual representation of a day, three letters Mon through Sun
179+ '/(?<! \\\\)z1/ ' , // The day of the year (starting from 1) 1 through 366
180+ '/(?<! \\\\)z/ ' , // The day of the year (starting from 0) 0 through 365
181+ '/(?<! \\\\)W0S/ ' , // Week number of year, weeks starting on Sunday 1st to 53rd
182+ '/(?<! \\\\)W7S/ ' , // Weeks starting on Sunday, weeks count from 1st Januar 1st to 54rd
183+ // with suffix
184+ '/(?<! \\\\)WS/ ' , // ISO 8601 week number of year, weeks starting on Monday 1st to 53rd
185+ '/(?<! \\\\)W0/ ' , // Week number of year, weeks starting on Sunday 1 to 53
186+ '/(?<! \\\\)W7/ ' , // Weeks starting on Sunday, weeks count from 1st Januar 1 to 54
187+ '/(?<! \\\\)W/ ' , // ISO 8601 week number of year, weeks starting on Monday 1 to 53
188+ '/(?<! \\\\)l/ ' , // A full textual representation of the day of the week Sunday through Saturday
189+ '/(?<! \\\\)F/ ' , // A full textual representation of a month January through December
190+ '/(?<! \\\\)O/ ' , // Difference to Greenwich time (GMT) without colon Example: +0200
191+ '/(?<! \\\\)P/ ' , // Difference to Greenwich time (GMT) with colon Example: +02:00
192+ ];
193+
194+ $ ct_replacement = [
195+ '{\g} ' , '{\G} ' , '{\h} ' , '{\H} ' , '{\i} ' , '{\s} ' , '{\a} ' , '{\A} ' , '{\y} ' , '{\Y} ' , '{\n} ' ,
196+ '{\m} ' , '{\M} ' , '{\j\S} ' , '{\j} ' , '{\d} ' , '{\D} ' , '{\z1} ' , '{\z} ' , '{\W0\S} ' , '{\W7\S} ' , '{\W\S} ' ,
197+ '{\W0} ' , '{\W7} ' , '{\W} ' , '{\l} ' , '{\F} ' , '{\O} ' , '{\P} ' ,
198+ ];
199+
200+ $ wc_replacement = [
201+ '{g} ' , '{G} ' , '{h} ' , '{H} ' , '{i} ' , '{s} ' , '{a} ' , '{A} ' , '{y} ' , '{Y} ' , '{n} ' ,
202+ '{m} ' , '{M} ' , '{jS} ' , '{j} ' , '{d} ' , '{D} ' , '{z1} ' , '{z} ' , '{W0S} ' , '{W7S} ' , '{WS} ' ,
203+ '{W0} ' , '{W7} ' , '{W} ' , '{l} ' , '{F} ' , '{O} ' , '{P} ' ,
204+ ];
205+
206+ $ replacement = $ replacement == 'ct ' ? $ ct_replacement : $ wc_replacement ;
207+
208+ return preg_replace ($ pattern , $ replacement , $ format );
209+ }
157210}
0 commit comments