Skip to content

Commit

Permalink
Managesieve: Parse all time and timezone formatters in sieve_formatti…
Browse files Browse the repository at this point in the history
…me() that PHP understands
  • Loading branch information
bennet0496 committed Nov 1, 2024
1 parent a4e50e3 commit 2352247
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
28 changes: 28 additions & 0 deletions plugins/managesieve/managesieve.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,9 @@ rcube_webmail.prototype.managesieve_tip_register = function (tips) {
function sieve_formattime(hour, minutes) {
var i, c, h, time = '', format = rcmail.env.time_format || 'H:i';

// Support all Time and Timezone related formatters from PHP
// https://www.php.net/manual/en/datetime.format.php
// Even if not all may make sense in this context
for (i = 0; i < format.length; i++) {
c = format.charAt(i);
switch (c) {
Expand Down Expand Up @@ -1040,6 +1043,31 @@ function sieve_formattime(hour, minutes) {
break;
case 's':
time += '00';

break;
case 'u': // Microseconds
time += '000000';

break;
case 'v': // Milliseconds
time += '000';

break;
case 'B': // Swatch Internet time: https://www.swatch.com/en-us/internet-time.html
s = (hour * 60 + minutes) * 60 - rcmail.env.server_timezone_info.Z + 3600;
time += s / (60 + 24.4);

break;
case 'e': // Timezone identifier
case 'I': // Whether the date is in daylight saving time
case 'O': // Difference to Greenwich time (GMT) without colon between hours and minutes
case 'P': // Difference to Greenwich time (GMT) with colon between hours and minutes
case 'p': // The same as P, but returns Z instead of +00:00
case 'T': // Timezone abbreviation, if known; otherwise the GMT offset
case 'Z': // Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.
time += rcmail.env.server_timezone_info[c];

break;
default:
time += c;
}
Expand Down
16 changes: 16 additions & 0 deletions plugins/managesieve/managesieve.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ public function init_ui()
$sieve_action = strpos($this->rc->action, 'plugin.managesieve') === 0;

if ($this->rc->task == 'mail' || $sieve_action) {
// Injection of Timezone information into the JS Frontend.
// All the specifiers may be included in $config['time_format']
// However not all are easily parseable in the JS world, especially
// when it comes to Timezone abbreviation
$tz = new DateTimeZone($this->rc->config->get('timezone'));
$dt = new DateTime('now', $tz);

$this->rc->output->set_env('server_timezone_info', [
'e' => $dt->format('e'),
'I' => $dt->format('I'),
'O' => $dt->format('O'),
'P' => $dt->format('P'),
'p' => version_compare(\PHP_VERSION, '8.0.0') >= 0 ? $dt->format('p') : $dt->format('P'),
'T' => $dt->format('T'),
'Z' => $dt->format('Z'),
]);
$this->include_script('managesieve.js');
}

Expand Down

0 comments on commit 2352247

Please sign in to comment.