added time-libs

This commit is contained in:
gtbu 2022-05-23 22:10:25 +02:00
parent 4f7afad29d
commit a29c2aaa04
4 changed files with 384 additions and 0 deletions

21
include/thirdparty/time/LICENSE.txt vendored Normal file
View file

@ -0,0 +1,21 @@
MIT License
https://github.com/arsensokolov/clock-tz.js
Copyright (c) 2020 Arsen Sokolov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

142
include/thirdparty/time/clock.js vendored Normal file
View file

@ -0,0 +1,142 @@
'use script';
class Clock {
defaultOptions = {
dateId: 'date',
monthId: 'month',
yearId: 'year',
hoursId: 'hours',
minutesId: 'minutes',
secondsId: 'seconds',
timeZoneOffset: null,
monthNames: [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
};
constructor(options) {
// options = {
// dateId: 'date',
// monthId: 'month',
// yearId: 'year',
// hoursId: 'hours',
// minutesId: 'minutes',
// secondsId: 'seconds',
// timeZoneOffset: +4.00,
// monthNames: [
// 'January',
// 'February',
// 'March',
// 'April',
// 'May',
// 'June',
// 'July',
// 'August',
// 'September',
// 'October',
// 'November',
// 'December'
// ]
// }
if (typeof options === 'undefined') {
options = this.defaultOptions;
}
const keys = Object.keys(this.defaultOptions);
for (const key of keys) {
if (typeof options[key] === 'undefined') {
options[key] = this.defaultOptions[key];
}
}
this.monthNames = options.monthNames;
// get all clock elements
this.dateElement = document.getElementById(options.dateId);
this.monthElement = document.getElementById(options.monthId);
this.yearElement = document.getElementById(options.yearId);
this.hoursElement = document.getElementById(options.hoursId);
this.minutesElement = document.getElementById(options.minutesId);
this.secondsElement = document.getElementById(options.secondsId);
// calculate timezone offset
if (options.timeZoneOffset != null) {
let d = new Date();
let tzDifference = options.timeZoneOffset * 60 + d.getTimezoneOffset();
this.offset = tzDifference * 60 * 1000;
}
}
render() {
let date = new Date();
if (typeof this.offset !== 'undefined') {
date = new Date(new Date().getTime() + this.offset);
}
if (this.secondsElement != null) {
let seconds = date.getSeconds();
if (seconds < 10) seconds = '0' + seconds;
this.secondsElement.innerText = '' + seconds;
}
if (this.hoursElement != null) {
let hours = date.getHours();
if (hours < 10) hours = '0' + hours;
if (this.hoursElement.innerText !== '' + hours) {
this.hoursElement.innerText = '' + hours;
}
}
if (this.minutesElement != null) {
let minutes = date.getMinutes();
if (minutes < 10) minutes = '0' + minutes;
if (this.minutesElement.innerText !== '' + minutes) {
this.minutesElement.innerText = '' + minutes;
}
}
if (this.dateElement != null) {
let day = date.getDate();
if (this.dateElement.innerText !== '' + day) {
this.dateElement.innerText = '' + day;
}
}
if (this.monthElement != null) {
let month_index = date.getMonth();
if (this.monthElement.innerText !== this.monthNames[month_index]) {
this.monthElement.innerText = this.monthNames[month_index];
}
}
if (this.yearElement != null) {
let year = date.getFullYear();
if (this.yearElement.innerText !== '' + year) {
this.yearElement.innerText = '' + year;
}
}
}
stop() {
clearInterval(this.timer);
}
start() {
this.render();
this.timer = setInterval(() => this.render(), 1000)
}
}

1
include/thirdparty/time/clock.min.js vendored Normal file
View file

@ -0,0 +1 @@
class Clock{defaultOptions={dateId:"date",monthId:"month",yearId:"year",hoursId:"hours",minutesId:"minutes",secondsId:"seconds",timeZoneOffset:null,monthNames:["January","February","March","April","May","June","July","August","September","October","November","December"]};constructor(e){void 0===e&&(e=this.defaultOptions);const t=Object.keys(this.defaultOptions);for(const n of t)void 0===e[n]&&(e[n]=this.defaultOptions[n]);if(this.monthNames=e.monthNames,this.dateElement=document.getElementById(e.dateId),this.monthElement=document.getElementById(e.monthId),this.yearElement=document.getElementById(e.yearId),this.hoursElement=document.getElementById(e.hoursId),this.minutesElement=document.getElementById(e.minutesId),this.secondsElement=document.getElementById(e.secondsId),null!=e.timeZoneOffset){let t=new Date,n=60*e.timeZoneOffset+t.getTimezoneOffset();this.offset=60*n*1e3}}render(){let e=new Date;if(void 0!==this.offset&&(e=new Date((new Date).getTime()+this.offset)),null!=this.secondsElement){let t=e.getSeconds();t<10&&(t="0"+t),this.secondsElement.innerText=""+t}if(null!=this.hoursElement){let t=e.getHours();t<10&&(t="0"+t),this.hoursElement.innerText!==""+t&&(this.hoursElement.innerText=""+t)}if(null!=this.minutesElement){let t=e.getMinutes();t<10&&(t="0"+t),this.minutesElement.innerText!==""+t&&(this.minutesElement.innerText=""+t)}if(null!=this.dateElement){let t=e.getDate();this.dateElement.innerText!==""+t&&(this.dateElement.innerText=""+t)}if(null!=this.monthElement){let t=e.getMonth();this.monthElement.innerText!==this.monthNames[t]&&(this.monthElement.innerText=this.monthNames[t])}if(null!=this.yearElement){let t=e.getFullYear();this.yearElement.innerText!==""+t&&(this.yearElement.innerText=""+t)}}stop(){clearInterval(this.timer)}start(){this.render(),this.timer=setInterval(()=>this.render(),1e3)}}

220
include/thirdparty/time/strftime.php vendored Normal file
View file

@ -0,0 +1,220 @@
<?php
namespace PHP81_BC;
use DateTime;
use DateTimeZone;
use DateTimeInterface;
use Exception;
use IntlDateFormatter;
use IntlGregorianCalendar;
use InvalidArgumentException;
/**
* Locale-formatted strftime using IntlDateFormatter (PHP 8.1 compatible)
* This provides a cross-platform alternative to strftime() for when it will be removed from PHP.
* Note that output can be slightly different between libc sprintf and this function as it is using ICU.
*
* Usage:
* use function \PHP81_BC\strftime;
* echo strftime('%A %e %B %Y %X', new \DateTime('2021-09-28 00:00:00'), 'fr_FR');
*
* Original use:
* \setlocale(LC_TIME, 'fr_FR.UTF-8');
* echo \strftime('%A %e %B %Y %X', strtotime('2021-09-28 00:00:00'));
*
* @param string $format Date format
* @param integer|string|DateTime $timestamp Timestamp
* @return string
* @author BohwaZ <https://bohwaz.net/>
* https://github.com/alphp/strftime MIT
*/
function strftime (string $format, $timestamp = null, ?string $locale = null) : string {
if (!($timestamp instanceof DateTimeInterface)) {
$timestamp = is_int($timestamp) ? '@' . $timestamp : (string) $timestamp;
try {
$timestamp = new DateTime($timestamp);
} catch (Exception $e) {
throw new InvalidArgumentException('$timestamp argument is neither a valid UNIX timestamp, a valid date-time string or a DateTime object.', 0, $e);
}
}
$timestamp->setTimezone(new DateTimeZone(date_default_timezone_get()));
if (empty($locale)) {
// get current locale
$locale = setlocale(LC_TIME, '0');
}
// remove trailing part not supported by ext-intl locale
$locale = preg_replace('/[^\w-].*$/', '', $locale);
$intl_formats = [
'%a' => 'EEE', // An abbreviated textual representation of the day Sun through Sat
'%A' => 'EEEE', // A full textual representation of the day Sunday through Saturday
'%b' => 'MMM', // Abbreviated month name, based on the locale Jan through Dec
'%B' => 'MMMM', // Full month name, based on the locale January through December
'%h' => 'MMM', // Abbreviated month name, based on the locale (an alias of %b) Jan through Dec
];
$intl_formatter = function (DateTimeInterface $timestamp, string $format) use ($intl_formats, $locale) {
$tz = $timestamp->getTimezone();
$date_type = IntlDateFormatter::FULL;
$time_type = IntlDateFormatter::FULL;
$pattern = '';
switch ($format) {
// %c = Preferred date and time stamp based on locale
// Example: Tue Feb 5 00:45:10 2009 for February 5, 2009 at 12:45:10 AM
case '%c':
$date_type = IntlDateFormatter::LONG;
$time_type = IntlDateFormatter::SHORT;
break;
// %x = Preferred date representation based on locale, without the time
// Example: 02/05/09 for February 5, 2009
case '%x':
$date_type = IntlDateFormatter::SHORT;
$time_type = IntlDateFormatter::NONE;
break;
// Localized time format
case '%X':
$date_type = IntlDateFormatter::NONE;
$time_type = IntlDateFormatter::MEDIUM;
break;
default:
$pattern = $intl_formats[$format];
}
// In October 1582, the Gregorian calendar replaced the Julian in much of Europe, and
// the 4th October was followed by the 15th October.
// ICU (including IntlDateFormattter) interprets and formats dates based on this cutover.
// Posix (including strftime) and timelib (including DateTimeImmutable) instead use
// a "proleptic Gregorian calendar" - they pretend the Gregorian calendar has existed forever.
// This leads to the same instants in time, as expressed in Unix time, having different representations
// in formatted strings.
// To adjust for this, a custom calendar can be supplied with a cutover date arbitrarily far in the past.
$calendar = IntlGregorianCalendar::createInstance();
$calendar->setGregorianChange(PHP_INT_MIN);
return (new IntlDateFormatter($locale, $date_type, $time_type, $tz, $calendar, $pattern))->format($timestamp);
};
// Same order as https://www.php.net/manual/en/function.strftime.php
$translation_table = [
// Day
'%a' => $intl_formatter,
'%A' => $intl_formatter,
'%d' => 'd',
'%e' => function ($timestamp) {
return sprintf('% 2u', $timestamp->format('j'));
},
'%j' => function ($timestamp) {
// Day number in year, 001 to 366
return sprintf('%03d', $timestamp->format('z')+1);
},
'%u' => 'N',
'%w' => 'w',
// Week
'%U' => function ($timestamp) {
// Number of weeks between date and first Sunday of year
$day = new DateTime(sprintf('%d-01 Sunday', $timestamp->format('Y')));
return sprintf('%02u', 1 + ($timestamp->format('z') - $day->format('z')) / 7);
},
'%V' => 'W',
'%W' => function ($timestamp) {
// Number of weeks between date and first Monday of year
$day = new DateTime(sprintf('%d-01 Monday', $timestamp->format('Y')));
return sprintf('%02u', 1 + ($timestamp->format('z') - $day->format('z')) / 7);
},
// Month
'%b' => $intl_formatter,
'%B' => $intl_formatter,
'%h' => $intl_formatter,
'%m' => 'm',
// Year
'%C' => function ($timestamp) {
// Century (-1): 19 for 20th century
return floor($timestamp->format('Y') / 100);
},
'%g' => function ($timestamp) {
return substr($timestamp->format('o'), -2);
},
'%G' => 'o',
'%y' => 'y',
'%Y' => 'Y',
// Time
'%H' => 'H',
'%k' => function ($timestamp) {
return sprintf('% 2u', $timestamp->format('G'));
},
'%I' => 'h',
'%l' => function ($timestamp) {
return sprintf('% 2u', $timestamp->format('g'));
},
'%M' => 'i',
'%p' => 'A', // AM PM (this is reversed on purpose!)
'%P' => 'a', // am pm
'%r' => 'h:i:s A', // %I:%M:%S %p
'%R' => 'H:i', // %H:%M
'%S' => 's',
'%T' => 'H:i:s', // %H:%M:%S
'%X' => $intl_formatter, // Preferred time representation based on locale, without the date
// Timezone
'%z' => 'O',
'%Z' => 'T',
// Time and Date Stamps
'%c' => $intl_formatter,
'%D' => 'm/d/Y',
'%F' => 'Y-m-d',
'%s' => 'U',
'%x' => $intl_formatter,
];
$out = preg_replace_callback('/(?<!%)%([_#-]?)([a-zA-Z])/', function ($match) use ($translation_table, $timestamp) {
$prefix = $match[1];
$char = $match[2];
$pattern = '%'.$char;
if ($pattern == '%n') {
return "\n";
}
elseif ($pattern == '%t') {
return "\t";
}
if (!isset($translation_table[$pattern])) {
throw new InvalidArgumentException(sprintf('Format "%s" is unknown in time format', $pattern));
}
$replace = $translation_table[$pattern];
if (is_string($replace)) {
$result = $timestamp->format($replace);
}
else {
$result = $replace($timestamp, $pattern);
}
switch ($prefix) {
case '_':
// replace leading zeros with spaces but keep last char if also zero
return preg_replace('/\G0(?=.)/', ' ', $result);
case '#':
case '-':
// remove leading zeros but keep last char if also zero
return preg_replace('/^0+(?=.)/', '', $result);
}
return $result;
}, $format);
$out = str_replace('%%', '%', $out);
return $out;
}