-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathReferenceTime.php
More file actions
137 lines (112 loc) · 3.18 KB
/
Copy pathReferenceTime.php
File metadata and controls
137 lines (112 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
/*
* This file is part of the PHP-CRON-EXPR package.
*
* (c) Jitendra Adhikari <jiten.adhikary@gmail.com>
* <https://github.com/adhocore>
*
* Licensed under MIT license.
*/
namespace Ahc\Cron;
use DateTime;
/**
* @method int minute()
* @method int hour()
* @method int monthDay()
* @method int month()
* @method int weekDay() 0 based day of week.
* @method int year()
* @method int day()
* @method int weekDay1() 1 based day of week.
* @method int numDays() Number of days in the month.
*/
class ReferenceTime
{
// The cron parts. (Donot change it)
const MINUTE = 0;
const HOUR = 1;
const MONTHDAY = 2;
const MONTH = 3;
const WEEKDAY = 4;
const YEAR = 5;
// Meta data parts.
const DAY = 6;
const WEEKDAY1 = 7;
const NUMDAYS = 8;
/** @var array The data */
protected $values = [];
/** @var array The Magic methods */
protected $methods = [];
public $timestamp;
public function __construct($time)
{
$this->reset($this->normalizeTime($time));
$this->methods = (new \ReflectionClass($this))->getConstants();
}
public function reset(int $timestamp)
{
$this->timestamp = $timestamp;
$this->values = $this->parse($timestamp);
}
public function add(int $sec)
{
$this->reset($this->timestamp + $sec);
}
public function addMonth()
{
$year = $this->values[self::YEAR];
$month = $this->values[self::MONTH] + 1;
if ($month > 12) {
[$year, $month] = [$year + 1, 1];
}
$new = "$year-$month-" . date('d H:i:s', $this->timestamp);
$this->reset(\strtotime($new));
}
public function addYear()
{
$year = $this->values[self::YEAR] + 1;
$new = "$year-" . date('m-d H:i:s', $this->timestamp);
$this->reset(\strtotime($new));
}
public function dateTime(): DateTime
{
return new DateTime(date('Y-m-d H:i:s', $this->timestamp));
}
public function __call(string $method, array $args): int
{
$method = \preg_replace('/^GET/', '', \strtoupper($method));
if (isset($this->methods[$method])) {
return $this->values[$this->methods[$method]];
}
// @codeCoverageIgnoreStart
throw new \BadMethodCallException("Method '$method' doesnot exist in ReferenceTime.");
// @codeCoverageIgnoreEnd
}
public function get(int $segment): int
{
return $this->values[$segment];
}
public function isAt($value, int $segment): bool
{
return $this->values[$segment] == $value;
}
protected function normalizeTime($time): int
{
if (empty($time)) {
$time = \time();
} elseif (\is_string($time)) {
$time = \strtotime($time);
} elseif ($time instanceof DateTime) {
$time = $time->getTimestamp();
}
return $time;
}
protected function parse(int $timestamp): array
{
$parts = \date('i G j n w Y d N t', $timestamp);
$parts = \explode(' ', $parts);
$parts = \array_map('intval', $parts);
return $parts;
}
}