1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\System\DateTime; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the TYPO3 CMS project. |
6
|
|
|
* |
7
|
|
|
* It is free software; you can redistribute it and/or modify it under |
8
|
|
|
* the terms of the GNU General Public License, either version 2 |
9
|
|
|
* of the License, or any later version. |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please read the |
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
13
|
|
|
* |
14
|
|
|
* The TYPO3 project - inspiring people to share! |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
use DateTime; |
18
|
|
|
use DateTimeZone; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Testcase to check if the configuration object can be used as expected |
22
|
|
|
* |
23
|
|
|
* @author Hendrik Putzek <[email protected]> |
24
|
|
|
* @author Timo Hund <[email protected]> |
25
|
|
|
* @copyright (c) 2011-2016 Timo Schmidt <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class FormatService |
28
|
|
|
{ |
29
|
|
|
const SOLR_ISO_DATETIME_FORMAT = 'Y-m-d\TH:i:s\Z'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @see http://php.net/manual/de/function.date.php for formatting options |
33
|
|
|
* @param string $input the passed date string |
34
|
|
|
* @param string $inputFormat the input format that should be used for parsing |
35
|
|
|
* @param string $outputFormat The output format, when nothing is passed |
36
|
|
|
* $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] will be used or Y-m-d when nothing is configured |
37
|
|
|
* @param DateTimeZone|null $timezone |
38
|
|
|
* @return \DateTime|string |
39
|
|
|
*/ |
40
|
|
|
public function format($input = '', $inputFormat = 'Y-m-d\TH:i:s\Z', $outputFormat = '', $timezone = null) |
41
|
|
|
{ |
42
|
|
|
if ($outputFormat === '') { |
43
|
|
|
// when no value was passed we us the TYPO3 configured or fallback to Y-m-d |
44
|
|
|
$outputFormat = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// try to create DateTime object |
48
|
|
|
$timezone = $timezone ?? new DateTimeZone(date_default_timezone_get()); |
49
|
|
|
return $this->getFormattedDate($input, $inputFormat, $outputFormat, $timezone); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Converts a date from unix timestamp to ISO 8601 format. |
54
|
|
|
* |
55
|
|
|
* @param int $timestamp unix timestamp |
56
|
|
|
* @return string the date in ISO 8601 format |
57
|
|
|
*/ |
58
|
89 |
|
public function timestampToIso($timestamp) |
59
|
|
|
{ |
60
|
89 |
|
return date(self::SOLR_ISO_DATETIME_FORMAT, $timestamp); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Converts a date from ISO 8601 format to unix timestamp. |
65
|
|
|
* |
66
|
|
|
* @param string $isoTime date in ISO 8601 format |
67
|
|
|
* @return int unix timestamp |
68
|
|
|
*/ |
69
|
|
|
public function isoToTimestamp($isoTime): int |
70
|
|
|
{ |
71
|
|
|
$dateTime = \DateTime::createFromFormat( |
72
|
|
|
self::SOLR_ISO_DATETIME_FORMAT, |
73
|
|
|
$isoTime |
74
|
|
|
); |
75
|
|
|
return $dateTime ? (int)$dateTime->format('U') : 0; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Converts a date from unix timestamp to ISO 8601 format in UTC timezone. |
80
|
|
|
* |
81
|
|
|
* @param int $timestamp unix timestamp |
82
|
|
|
* @return string the date in ISO 8601 format |
83
|
|
|
*/ |
84
|
|
|
public function timestampToUtcIso($timestamp) |
85
|
|
|
{ |
86
|
|
|
return gmdate(self::SOLR_ISO_DATETIME_FORMAT, $timestamp); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Converts a date from ISO 8601 format in UTC timezone to unix timestamp. |
91
|
|
|
* |
92
|
|
|
* @param string $isoTime date in ISO 8601 format |
93
|
|
|
* @return int unix timestamp |
94
|
|
|
*/ |
95
|
|
|
public function utcIsoToTimestamp($isoTime): int |
96
|
|
|
{ |
97
|
|
|
$utcTimeZone = new \DateTimeZone('UTC'); |
98
|
|
|
$dateTime = \DateTime::createFromFormat( |
99
|
|
|
self::SOLR_ISO_DATETIME_FORMAT, |
100
|
|
|
$isoTime, |
101
|
|
|
$utcTimeZone |
102
|
|
|
); |
103
|
|
|
return $dateTime ? (int)$dateTime->format('U') : 0; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Applies the formatting using DateTime. |
108
|
|
|
* |
109
|
|
|
* @param string $input |
110
|
|
|
* @param string $inputFormat |
111
|
|
|
* @param string $outputFormat |
112
|
|
|
* @param DateTimeZone $timezone |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function getFormattedDate($input, $inputFormat, $outputFormat, DateTimeZone $timezone): string |
116
|
|
|
{ |
117
|
|
|
$formattedDate = DateTime::createFromFormat($inputFormat, $input, $timezone); |
118
|
|
|
if ($formattedDate) { |
|
|
|
|
119
|
|
|
return $formattedDate->format($outputFormat); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return (string)$input; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|