Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ManageSieve: make sieve_formattime() parse all time-formatters PHP does. #9655

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions plugins/managesieve/managesieve.js
Original file line number Diff line number Diff line change
Expand Up @@ -1007,10 +1007,14 @@ rcube_webmail.prototype.managesieve_tip_register = function (tips) {

// format time string
function sieve_formattime(hour, minutes) {
var i, c, h, time = '', format = rcmail.env.time_format || 'H:i';

for (i = 0; i < format.length; i++) {
c = format.charAt(i);
let time = '';
const 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 (let i = 0; i < format.length; i++) {
const c = format.charAt(i);
switch (c) {
case 'a':
time += hour >= 12 ? 'pm' : 'am';
Expand All @@ -1022,8 +1026,7 @@ function sieve_formattime(hour, minutes) {
break;
case 'g':
case 'h':
h = hour == 0 ? 12 : hour > 12 ? hour - 12 : hour;
time += (c == 'h' && hour < 10 ? '0' : '') + hour;
time += (c === 'h' && hour < 10 ? '0' : '') + (hour % 12 === 0 ? 12 : hour % 12);

break;
case 'G':
Expand All @@ -1040,6 +1043,30 @@ function sieve_formattime(hour, minutes) {
break;
case 's':
time += '00';

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

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

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;
case 'U': // Unix Timestamp
time += Math.round((new Date()).valueOf() / 1000);

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