1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16namespace Fisharebest\Webtrees\Date; 17 18use Fisharebest\ExtCalendar\JulianCalendar; 19use Fisharebest\Webtrees\I18N; 20 21/** 22 * Definitions for the Julian Proleptic calendar 23 * (Proleptic means we extend it backwards, prior to its introduction in 46BC) 24 */ 25class JulianDate extends CalendarDate 26{ 27 /** @var bool True for dates recorded in new-style/old-style format, e.g. 2 FEB 1743/44 */ 28 private $new_old_style = false; 29 30 /** 31 * Create a date from either: 32 * a Julian day number 33 * day/month/year strings from a GEDCOM date 34 * another CalendarDate object 35 * 36 * @param array|int|CalendarDate $date 37 */ 38 public function __construct($date) 39 { 40 $this->calendar = new JulianCalendar(); 41 parent::__construct($date); 42 } 43 44 /** 45 * Most years are 1 more than the previous, but not always (e.g. 1BC->1AD) 46 * 47 * @param int $year 48 * 49 * @return int 50 */ 51 protected function nextYear(int $year): int 52 { 53 if ($year == -1) { 54 return 1; 55 } 56 57 return $year + 1; 58 } 59 60 /** 61 * Process new-style/old-style years and years BC 62 * 63 * @param string $year 64 * 65 * @return int 66 */ 67 protected function extractYear(string $year): int 68 { 69 if (preg_match('/^(\d\d\d\d)\/\d{1,4}$/', $year, $match)) { 70 // Assume the first year is correct 71 $this->new_old_style = true; 72 73 return (int) $match[1] + 1; 74 } 75 76 if (preg_match('/^(\d+) B\.C\.$/', $year, $match)) { 77 return - (int) $match[1]; 78 } 79 80 return (int) $year; 81 } 82 83 /** 84 * Generate the %Y format for a date. 85 * 86 * @return string 87 */ 88 protected function formatLongYear(): string 89 { 90 if ($this->y < 0) { 91 return /* I18N: BCE=Before the Common Era, for Julian years < 0. See http://en.wikipedia.org/wiki/Common_Era */ 92 I18N::translate('%s BCE', I18N::digits(-$this->y)); 93 } 94 95 if ($this->new_old_style) { 96 return I18N::translate('%s CE', I18N::digits(sprintf('%d/%02d', $this->y - 1, $this->y % 100))); 97 } 98 99 /* I18N: CE=Common Era, for Julian years > 0. See http://en.wikipedia.org/wiki/Common_Era */ 100 return I18N::translate('%s CE', I18N::digits($this->y)); 101 } 102 103 /** 104 * Generate the %E format for a date. 105 * 106 * @return string 107 */ 108 protected function formatGedcomYear(): string 109 { 110 if ($this->y < 0) { 111 return sprintf('%04d B.C.', -$this->y); 112 } 113 114 if ($this->new_old_style) { 115 return sprintf('%04d/%02d', $this->y - 1, $this->y % 100); 116 } 117 118 return sprintf('%04d', $this->y); 119 } 120} 121