Skip to content

Commit cb6c8d0

Browse files
committed
Add command line interpreter; fix recursive issue with output timezone
1 parent c10ac92 commit cb6c8d0

File tree

8 files changed

+439
-143
lines changed

8 files changed

+439
-143
lines changed

README.md

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,6 @@ DateTime or DateTimeImmutable classes which return the time
55
range. Can be used e.g. excellently for command line
66
arguments and options to make database queries with.
77

8-
## Installation
9-
10-
```bash
11-
composer require ixnode/php-date-parser
12-
```
13-
14-
```bash
15-
vendor/bin/php-date-parser -V
16-
```
17-
18-
```bash
19-
php-date-parser 0.1.0 (07-07-2023 22:23:01) - Björn Hempel <bjoern@hempel.li>
20-
```
21-
228
## Usage
239

2410
```php
@@ -28,11 +14,11 @@ use Ixnode\PhpDateParser\DateParser;
2814
### Date parser
2915

3016
```php
31-
$dateParser = (new DateParser('<2023-07-01'))->formatFrom('Y-m-d H:i:s');
32-
// null
17+
$dateParser = (new DateParser('2023-07-01'))->formatFrom('Y-m-d H:i:s');
18+
// 2023-07-01 00:00:00
3319

34-
$dateParser = (new DateParser('<2023-07-01'))->formatTo('Y-m-d H:i:s');
35-
// 2023-06-30 23:59:59
20+
$dateParser = (new DateParser('2023-07-01'))->formatTo('Y-m-d H:i:s');
21+
// 2023-07-01 23:59:59
3622
```
3723

3824
### Word parser
@@ -45,14 +31,14 @@ $dateParser = (new DateParser('today'))->formatTo('Y-m-d H:i:s');
4531
// 2023-07-07 23:59:59
4632
```
4733

48-
### Timezones
34+
### Date parser with timezones
4935

5036
```php
5137
$dateParser = (new DateParser('<2023-07-01', 'America/New_York'))->formatFrom('Y-m-d H:i:s', 'Europe/Berlin');
5238
// null
5339
5440
$dateParser = (new DateParser('<2023-07-01', 'America/New_York'))->formatTo('Y-m-d H:i:s', 'Europe/Berlin');
55-
// 2023-07-01 09:59:59
41+
// 2023-07-01 05:59:59
5642
```
5743
5844
## Parsing formats
@@ -158,6 +144,64 @@ $dateParser = (new DateParser('<2023-07-01', 'America/New_York'))->formatTo('Y-m
158144
| <nobr>`->getFromImmutable(DateTimeZone $dateTimeZoneOutput)`</nobr> | Returns the "from" date as `DateTimeImmutable` object. | `DateTimeImmutable\|null` |
159145
| <nobr>`->getToImmutable(DateTimeZone $dateTimeZoneOutput)`</nobr> | Returns the "to" date as `DateTimeImmutable` object. | `DateTimeImmutable\|null` |
160146
147+
## Installation
148+
149+
```bash
150+
composer require ixnode/php-date-parser
151+
```
152+
153+
```bash
154+
vendor/bin/php-date-parser --version
155+
```
156+
157+
```bash
158+
0.1.8 (2023-07-18 21:24:05) - Björn Hempel <bjoern@hempel.li>
159+
```
160+
161+
## Command line
162+
163+
```bash
164+
bin/console pdt --timezone-input=America/New_York --timezone-output=Europe/Berlin "<2023-07-01"
165+
```
166+
167+
or within your composer project
168+
169+
```bash
170+
vendor/bin/php-date-parser pdt --timezone-input=America/New_York --timezone-output=Europe/Berlin "<2023-07-01"
171+
```
172+
173+
```text
174+
175+
Given date time range: "<2023-07-01" (America/New_York)
176+
177+
+------------------------------------------+------------------+
178+
| Value | Given |
179+
+------------------------------------------+------------------+
180+
| Given date time range (America/New_York) | <2023-07-01 |
181+
| Timezone (input) | America/New_York |
182+
| Timezone (output) | Europe/Berlin |
183+
+------------------------------------------+------------------+
184+
185+
Parsed from given string (input):
186+
187+
+------+-------------+---------------------+---------------------+
188+
| Type | Format | UTC | America/New York |
189+
+------+-------------+---------------------+---------------------+
190+
| From | Y-m-d H:i:s | n/a | n/a |
191+
| To | Y-m-d H:i:s | 2023-07-01 03:59:59 | 2023-06-30 23:59:59 |
192+
+------+-------------+---------------------+---------------------+
193+
194+
Parsed output:
195+
196+
+------+-------------+---------------------+---------------------+
197+
| Type | Format | UTC | Europe/Berlin |
198+
+------+-------------+---------------------+---------------------+
199+
| From | Y-m-d H:i:s | n/a | n/a |
200+
| To | Y-m-d H:i:s | 2023-07-01 03:59:59 | 2023-07-01 05:59:59 |
201+
+------+-------------+---------------------+---------------------+
202+
203+
```
204+
161205
## Development
162206
163207
```bash

bin/console

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
/*
5+
* This file is part of the ixnode/php-date-parser project.
6+
*
7+
* (c) Björn Hempel <https://www.hempel.li/>
8+
*
9+
* For the full copyright and license information, please view the LICENSE.md
10+
* file that was distributed with this source code.
11+
*/
12+
13+
declare(strict_types=1);
14+
15+
require dirname(__DIR__).'/vendor/autoload.php';
16+
17+
use Ahc\Cli\Application;
18+
use Ixnode\PhpDateParser\Command\ParserCommand;
19+
20+
$versionFile = dirname(__DIR__).'/VERSION';
21+
22+
if (!file_exists($versionFile)) {
23+
throw new Exception(sprintf('Unable to find file "%s".', $versionFile));
24+
}
25+
26+
$versionFileModification = filemtime($versionFile);
27+
28+
$versionFileDate = date('Y-m-d H:i:s', $versionFileModification);
29+
30+
$version = trim(file_get_contents($versionFile));
31+
32+
$author = 'Björn Hempel <bjoern@hempel.li>';
33+
34+
$versionString = sprintf(
35+
'%s (%s) - %s',
36+
$version,
37+
$versionFileDate,
38+
$author
39+
);
40+
41+
$app = new Application('PHP Date Parser', $versionString);
42+
43+
$app->add(new ParserCommand(), 'pdt');
44+
45+
$app->handle($_SERVER['argv']);

bin/php-date-parser

Lines changed: 8 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#!/usr/bin/env bash
22

33
# ------------
4-
# PHP Date Parser - This library parses different strings to DateTime or DateTimeImmutable classes.
4+
# PHP Date Parser - This library parses various given date and time strings into
5+
# DateTime or DateTimeImmutable classes which return the time
6+
# range. Can be used e.g. excellently for command line
7+
# arguments and options to make database queries with.
58
#
69
# @author Björn Hempel <bjoern@hempel.li>
7-
# @version 0.1.0 (2023-07-07)
10+
# @version 0.1.0 (2023-07-20)
811
# ------------
912

1013
# ------------
@@ -33,119 +36,7 @@
3336
# Written by Björn Hempel <bjoern@hempel.li>.
3437
# ------------
3538

36-
# path configs
37-
PATH_SCRIPT_ABSOLUTE="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1; pwd -P)"
38-
PATH_WORKING="$PWD"
39+
SCRIPT_PATH=$(readlink -f "$0")
40+
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
3941

40-
# some environment variables
41-
ENV_SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")
42-
ENV_AUTHOR='Björn Hempel'
43-
ENV_EMAIL='bjoern@hempel.li'
44-
45-
# path files
46-
PATH_VERSION_RELATIVE="VERSION"
47-
48-
# =====
49-
#
50-
# Helper function: Get current version from file
51-
#
52-
# =====
53-
function getCurrentVersion()
54-
{
55-
local versionPath="$PATH_WORKING/$PATH_VERSION_RELATIVE"
56-
57-
if [ ! -f "$versionPath" ]; then
58-
echo && error "Version file \"$versionPath\" not found. Abort."
59-
echo && showVersion && showHelp && exit 1
60-
fi
61-
62-
# shellcheck disable=SC2155
63-
local version=$(cat "$versionPath")
64-
65-
echo "$version"
66-
}
67-
68-
# =====
69-
#
70-
# Helper function: Get current version date from file
71-
#
72-
# =====
73-
function getCurrentVersionDate()
74-
{
75-
local versionPath="$PATH_WORKING/$PATH_VERSION_RELATIVE"
76-
77-
if [ ! -f "$versionPath" ]; then
78-
echo && error "Version file \"$versionPath\" not found. Abort."
79-
echo && showVersion && showHelp && exit 1
80-
fi
81-
82-
date -r $versionPath "+%m-%d-%Y %H:%M:%S"
83-
}
84-
85-
# =====
86-
#
87-
# Helper function: show help
88-
#
89-
# =====
90-
function showHelp
91-
{
92-
cat "${BASH_SOURCE[0]}" | grep --color=never "# help:" | grep -v 'cat parameter' | sed 's/[ ]*# help:\([ ]\|\)//g' | sed "s~%scriptname%~$ENV_SCRIPT_NAME~g" | sed "s~\$PWD~\"$PWD\"~"
93-
}
94-
95-
# =====
96-
#
97-
# Helper function: show version
98-
#
99-
# =====
100-
function showVersion
101-
{
102-
local currentVersion=$(getCurrentVersion)
103-
local currentVersionDate=$(getCurrentVersionDate)
104-
105-
echo "$ENV_SCRIPT_NAME $currentVersion ($currentVersionDate) - $ENV_AUTHOR <$ENV_EMAIL>"
106-
}
107-
108-
# =====
109-
#
110-
# Task: Reads parameter
111-
#
112-
# =====
113-
function readParameter
114-
{
115-
# help:
116-
# help: Usage: %scriptname% [...options] [version]
117-
while [[ $# -gt 0 ]]; do
118-
case "$1" in
119-
120-
# help: -h, --help Shows this help.
121-
-h|--help)
122-
echo && showVersion && showHelp && exit 0
123-
;;
124-
125-
# help: -V, --version Shows the version number.
126-
-V|--version)
127-
showVersion && exit 0
128-
;;
129-
130-
# help:
131-
# collect all unknown parameters
132-
*)
133-
local parameter="$1"
134-
135-
if [[ "$parameter" = -* ]]; then
136-
echo && error "Unknown option \"$parameter\". Abort."
137-
echo && showVersion && showHelp && exit 1
138-
fi
139-
140-
PARAMETERS+=("$parameter")
141-
;;
142-
esac
143-
shift
144-
done
145-
}
146-
147-
# a) Read parameters
148-
readParameter "$@"
149-
150-
# b) Unsupported mode
151-
showHelp
42+
$SCRIPT_DIR/console "$@"

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
},
2424
"require": {
2525
"php": "^8.2",
26-
"ixnode/php-naming-conventions": "^0.1.1"
26+
"ixnode/php-naming-conventions": "^0.1.1",
27+
"adhocore/cli": "^v1.0.0"
2728
},
2829
"require-dev": {
2930
"phpunit/phpunit": "^9.5",

composer.lock

Lines changed: 71 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)