Skip to content

Commit

Permalink
gregorian calendat display year issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nczirjak-acdh committed Aug 19, 2024
1 parent af5d585 commit 3d1c7d3
Showing 1 changed file with 42 additions and 54 deletions.
96 changes: 42 additions & 54 deletions src/TwigExtension/ArcheTwigDateExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,39 @@
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class ArcheTwigDateExtension extends \Twig\Extension\AbstractExtension
{
public function getFilters()
{
return [ new \Twig\TwigFilter('archeTranslateDateFilter', function ($value, $dateformat) {
(isset($_SESSION['language'])) ? $lang = strtolower($_SESSION['language']) : $lang = "en";

if (strpos($value, "Z") !== false) {
$value = str_replace("Z", "", $value);
}
if ($this->checkYearIsMoreThanFourDigit($value)) {
return $this->notNormalDate($value, $lang, $dateformat);
}
return $this->returnFormattedDate($dateformat, $value, $lang);
})
class ArcheTwigDateExtension extends \Twig\Extension\AbstractExtension {

public function getFilters() {
return [new \Twig\TwigFilter('archeTranslateDateFilter', function ($value, $dateformat) {
(isset($_SESSION['language'])) ? $lang = strtolower($_SESSION['language']) : $lang = "en";

if (strpos($value, "Z") !== false) {
$value = str_replace("Z", "", $value);
}
if ($this->checkYearIsMoreThanFourDigit($value)) {
return $this->notNormalDate($value, $lang, $dateformat);
}
return $this->returnFormattedDate($dateformat, $value, $lang);
})
];
}

/**
* Gets a unique identifier for this Twig extension.
*/
public function getName()
{
public function getName() {
return 'arche_core_gui.twig_extension';
}

private function checkYearIsMoreThanFourDigit($value): bool
{
private function checkYearIsMoreThanFourDigit($value): bool {
$explode = explode("-", $value);
if (strlen($explode[0]) > 4) {
return true;
}
return false;
}

private function checkDateTimeValue($value)
{

private function checkDateTimeValue($value) {
$datetime = new \DateTime();
$newDate = $datetime->createFromFormat('Y-m-d', $value);
if ($newDate) {
Expand All @@ -57,67 +53,59 @@ private function checkDateTimeValue($value)
* @param type $lang
* @return string
*/
private function returnFormattedDate($dateformat, $value, $lang): string
{
/* we have to setup the timezone and locale, otherwise it will mix up the year */
ini_set('date.timezone', 'UTC');
ini_set('intl.default_locale', 'de_DE');
setlocale(LC_TIME, 'de_DE.utf8');

$value = $this->checkDateTimeValue($value);
$cal = \IntlCalendar::fromDateTime($value);

if ($cal === null) {
return "";
}
private function returnFormattedDate($dateformat, $value, $lang): string {

$date = new \DateTime($value);
// Determine if the date is before the Gregorian calendar was adopted.
$gregorian_start = new \DateTime('1582-10-15');

if ($date < $gregorian_start && (strtolower($dateformat) === 'yyyy' || strtolower($dateformat) === 'y')) {
// For dates before Gregorian calendar, directly extract the year.
return $date->format('Y');
}

if ($lang == 'de') {
return \IntlDateFormatter::formatObject($cal, $dateformat, 'de_DE');
}
ini_set('intl.default_locale', 'en_US');
setlocale(LC_TIME, 'en_US.utf8');
return \IntlDateFormatter::formatObject($cal, $dateformat, 'en_US');
$formatter = new \IntlDateFormatter($lang, \IntlDateFormatter::NONE, \IntlDateFormatter::NONE, null, null, $dateformat);
// Format the date and return the year.
return $formatter->format($date);
}


/**
* Return the befrore christ dates where we have 5 digit years numbers
* @param type $value
* @return string
*/
private function notNormalDate($value, $lang, $dateformat): string
{
private function notNormalDate($value, $lang, $dateformat): string {
ini_set('date.timezone', 'UTC');
ini_set('intl.default_locale', 'en_US');
setlocale(LC_TIME, 'en_US.utf8');

if ($lang == 'de') {
ini_set('intl.default_locale', 'de_DE');
setlocale(LC_TIME, 'de_DE.utf8');
}
$e = explode("-", $value);

$y = $e[0];
$m = $e[1];
$d = $e[2];

switch ($dateformat) {
case 'Y':
return $y;
case 'd-m-Y':
return $m.'-'.$d.'-'.$y;
return $m . '-' . $d . '-' . $y;
case 'd-m-y':
return $m.'-'.$d.'-'.$y;
return $m . '-' . $d . '-' . $y;
case 'd M Y':
return $d.'-'.date('M', $m).'-'.$y;
return $d . '-' . date('M', $m) . '-' . $y;
case 'd M y':
return $d.'-'.date('M', $m).'-'.$y;
return $d . '-' . date('M', $m) . '-' . $y;
case 'Y M d':
return $y.'-'.date('M', $m).'-'.$d;
return $y . '-' . date('M', $m) . '-' . $d;
case 'y M d':
return $y.'-'.date('M', $m).'-'.$d;
return $y . '-' . date('M', $m) . '-' . $d;
default:
return $y.'-'.$m.'-'.$d;
return $y . '-' . $m . '-' . $d;
}
}
}

0 comments on commit 3d1c7d3

Please sign in to comment.