1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popy\Calendar\Formatter\SymbolFormatter; |
4
|
|
|
|
5
|
|
|
use Popy\Calendar\FormatterInterface; |
6
|
|
|
use Popy\Calendar\Parser\FormatToken; |
7
|
|
|
use Popy\Calendar\Formatter\SymbolFormatterInterface; |
8
|
|
|
use Popy\Calendar\ValueObject\DateRepresentationInterface; |
9
|
|
|
use Popy\Calendar\ValueObject\DateTimeRepresentationInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Standard format, handling DateTimeRepresentationInterface. |
13
|
|
|
* |
14
|
|
|
* Not (yet) handling half-time format, so every time is shown as AM. |
15
|
|
|
*/ |
16
|
|
|
class StandardDateTime implements SymbolFormatterInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @inheritDoc |
20
|
|
|
*/ |
21
|
|
|
public function formatSymbol(DateRepresentationInterface $input, FormatToken $token, FormatterInterface $formatter) |
22
|
|
|
{ |
23
|
|
|
if (!$input instanceof DateTimeRepresentationInterface) { |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if ($token->isOne('a', 'A')) { |
28
|
|
|
// a Lowercase Ante meridiem and Post meridiem am or pm |
29
|
|
|
// A Uppercase Ante meridiem and Post meridiem AM or PM |
30
|
|
|
$res = $input->getTime()->canBeHalved(0) ? 'pm' : 'am'; |
31
|
|
|
return $token->is('a') ? $res : strtoupper($res); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if ($token->is('B')) { |
35
|
|
|
// B Swatch Internet time 000 through 999 |
36
|
|
|
return sprintf('%03d', $input->getTime()->getTransversal(0)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($token->isOne('g', 'h')) { |
40
|
|
|
// g 12-hour format of an hour without leading zeros 1 through 12 |
41
|
|
|
// h 12-hour format of an hour with leading zeros 01 through 12 |
42
|
|
|
$res = $input->getTime()->getHalved(0); |
43
|
|
|
if (!$res) { |
|
|
|
|
44
|
|
|
$res = (integer)floor($input->getTime()->getSize(0) / 2); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($token->is('h')) { |
48
|
|
|
return sprintf('%02d', $res); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return (string)$res; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($token->is('G')) { |
55
|
|
|
// G 24-hour format of an hour without leading zeros 0 through 23 |
56
|
|
|
return $input->getTime()->get(0); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($token->is('H')) { |
60
|
|
|
// H 24-hour format of an hour with leading zeros 00 through 23 |
61
|
|
|
return sprintf('%02d', $input->getTime()->get(0)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($token->is('i')) { |
65
|
|
|
// i Minutes with leading zeros 00 to 59 |
66
|
|
|
return sprintf('%02d', $input->getTime()->get(1)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($token->is('s')) { |
70
|
|
|
// s Seconds, with leading zeros 00 through 59 |
71
|
|
|
return sprintf('%02d', $input->getTime()->get(2)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($token->is('v')) { |
75
|
|
|
// v Milliseconds |
76
|
|
|
return sprintf('%03d', intval($input->getTime()->get(3))); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($token->is('µ')) { |
80
|
|
|
// Remaining microseconds |
81
|
|
|
return sprintf('%03d', intval($input->getTime()->get(4))); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: