brick/date-time
Composer 安装命令:
composer require brick/date-time
包简介
Date and time library
关键字:
README 文档
README
A powerful set of immutable classes to work with dates and times.
Introduction
This library builds an extensive API on top of the native PHP date-time classes, and adds missing concepts such as LocalDate, LocalTime, YearMonth, MonthDay, etc.
The classes follow the ISO 8601 standard for representing date and time concepts.
This component follows an important part of the JSR 310 (Date and Time API) specification from Java. Don't expect an exact match of class and method names though, as a number of differences exist for technical or practical reasons.
All the classes are immutable, they can be safely passed around without being affected.
Installation
This library is installable via Composer:
composer require brick/date-time
Requirements
This library requires PHP 8.2 or later.
Project status & release process
While this library is still under development, it is well tested and should be stable enough to use in production environments.
The current releases are numbered 0.x.y. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.), y is incremented.
When a breaking change is introduced, a new 0.x version cycle is always started.
It is therefore safe to lock your project to a given release cycle, such as 0.9.*.
If you need to upgrade to a newer release cycle, check the release history for a list of changes introduced by each further 0.x.0 version.
Overview
Main classes
The following classes/enums represent the date-time concepts:
DayOfWeek: a day-of-week such as Monday (enum)Duration: a duration measured in seconds and nanosecondsInstant: a point in time, with a nanosecond precisionInterval: a period of time between two instantsLocalDate: an isolated date such as2014-08-31LocalDateRange: an inclusive range of local dates, such as2014-01-01/2014-12-31LocalDateTime: a date-time without a time-zone, such as2014-08-31T10:15:30LocalTime: an isolated time such as10:15:30Month: a month-of-year such as January (enum)MonthDay: a combination of a month and a day, without a year, such as--12-31Period: a date-based amount of time, such as '2 years, 3 months and 4 days'TimeZoneOffset: an offset-based time-zone, such as+01:00TimeZoneRegion: a region-based time-zone, such asEurope/LondonYear: a year in the proleptic calendarYearMonth: a combination of a year and a month, such as2014-08ZonedDateTime: a date-time with a time-zone, such as2014-08-31T10:15:30+01:00. This class is conceptually equivalent to the nativeDateTimeclass
These classes belong to the Brick\DateTime namespace.
Clocks
All objects read the current time from a Clock implementation. The following implementations are available:
SystemClockreturns the system time; it's the default clockFixedClock: returns a pre-configured timeOffsetClock: adds an offset to another clockScaleClock: makes another clock fast-forward by a scale factor
These classes belong to the Brick\DateTime\Clock namespace.
In your application, you will most likely never touch the defaults, and always use the default clock:
use Brick\DateTime\LocalDate; use Brick\DateTime\TimeZone; echo LocalDate::now(TimeZone::utc()); // 2017-10-04
In your tests however, you might need to set the current time to test your application in known conditions. To do this, you can either explicitly pass a Clock instance to now() methods:
use Brick\DateTime\Clock\FixedClock; use Brick\DateTime\Instant; use Brick\DateTime\LocalDate; use Brick\DateTime\TimeZone; $clock = new FixedClock(Instant::of(1000000000)); echo LocalDate::now(TimeZone::utc(), $clock); // 2001-09-09
Or you can change the default clock for all date-time classes. All methods such as now(), unless provided with an explicit Clock, will use the default clock you provide:
use Brick\DateTime\Clock\FixedClock; use Brick\DateTime\DefaultClock; use Brick\DateTime\Instant; use Brick\DateTime\LocalDate; use Brick\DateTime\TimeZone; DefaultClock::set(new FixedClock(Instant::of(1000000000))); echo LocalDate::now(TimeZone::utc()); // 2001-09-09 DefaultClock::reset(); // do not forget to reset the clock to the system clock!
There are also useful shortcut methods to use clocks in your tests, inspired by timecop:
freeze()freezes time to a specific point in timetravelTo()travels to anInstantin time, but allows time to continue moving forward from theretravelBy()travels in time by aDuration, which may be forward (positive) or backward (negative)scale()makes time move at a given pace
Freeze the time to a specific point
use Brick\DateTime\DefaultClock; use Brick\DateTime\Instant; DefaultClock::freeze(Instant::of(2000000000)); $a = Instant::now(); sleep(1); $b = Instant::now(); echo $a, PHP_EOL; // 2033-05-18T03:33:20Z echo $b, PHP_EOL; // 2033-05-18T03:33:20Z DefaultClock::reset();
Travel to a specific point in time
use Brick\DateTime\DefaultClock; use Brick\DateTime\Instant; DefaultClock::travelTo(Instant::of(2000000000)); $a = Instant::now(); sleep(1); $b = Instant::now(); echo $a, PHP_EOL; // 2033-05-18T03:33:20.000342Z echo $b, PHP_EOL; // 2033-05-18T03:33:21.000606Z DefaultClock::reset();
Make time move at a given pace
use Brick\DateTime\DefaultClock; use Brick\DateTime\Instant; DefaultClock::travelTo(Instant::of(2000000000)); DefaultClock::scale(60); // 1 second becomes 60 seconds $a = Instant::now(); sleep(1); $b = Instant::now(); echo $a, PHP_EOL; // 2033-05-18T03:33:20.00188Z echo $b, PHP_EOL; // 2033-05-18T03:34:20.06632Z DefaultClock::reset();
As you can see, you can even combine travelTo() and scale() methods.
Be very careful to reset() the DefaultClock after each of your tests! If you're using PHPUnit, a good place to do this is in the tearDown() method.
Exceptions
The following exceptions can be thrown:
Brick\DateTime\DateTimeExceptionwhen an illegal operation is performedBrick\DateTime\Parser\DateTimeParseExceptionwhenparse()ing an invalid string representation
Doctrine mappings
You can use brick/date-time types in your Doctrine entities using the brick/date-time-doctrine package.
Contributing
Before submitting a pull request, you can check the code using the following tools. Your CI build will fail if any of the following tools reports any issue.
First of all, install dependencies:
composer install
Unit tests
Run PHPUnit tests:
vendor/bin/phpunit
Static analysis
Install PHPStan in its own folder:
composer install --working-dir=tools/phpstan
Run PHPStan static analysis:
tools/phpstan/vendor/bin/phpstan --configuration=tools/phpstan/phpstan.neon
Coding Style
Install Easy Coding Standard in its own folder:
composer install --working-dir=tools/ecs
Run coding style analysis checks:
tools/ecs/vendor/bin/ecs check --config tools/ecs/ecs.php
Or fix issues found directly:
tools/ecs/vendor/bin/ecs check --config tools/ecs/ecs.php --fix
Rector automated refactoring
Install Rector in its own folder:
composer install --working-dir=tools/rector
Run automated refactoring:
tools/rector/vendor/bin/rector --config tools/rector/rector.php
brick/date-time 适用场景与选型建议
brick/date-time 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.72M 次下载、GitHub Stars 达 362, 最近一次更新时间为 2014 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「time」 「date」 「timezone」 「period」 「datetime」 「duration」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 brick/date-time 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 brick/date-time 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 brick/date-time 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tools to convert between .NET and PHP date formats
Adds the EDTF data type to Wikibase
Datetime helpers for timezone handling
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
A powerful event calendar Tool for Laravel's Nova 5.
Official PHP SDK for the IPGeolocation.io IP Location API with single and bulk lookup support.
统计信息
- 总下载量: 3.72M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 366
- 点击次数: 43
- 依赖项目数: 62
- 推荐数: 3
其他信息
- 授权协议: MIT
- 更新时间: 2014-08-31
