sportfinder / time
时间管理库,用于构建复杂的业务逻辑和日程安排
v1.0.1
2023-02-17 07:54 UTC
Requires
- php: >=7.1.3|8.*
Requires (Dev)
- phpunit/phpunit: ^8.5
README
对于多个项目,我需要管理时间、每日日程、每周日程等。我需要能够描述每周日程并从每周日程中生成具体的时间段。
这些类将帮助您
- 管理事件或任何具有时间和结束点的对象
- 计算手机使用情况及详细消费。
- 根据资源的使用情况定义资源是否可用
- 检测每周日程中的可用时间/空闲时间。
- 生成优化的每日/每周日程
使用此包的项目
灵感来源
- 在BePark的经历
接口与类
接口
namespace SportFinder\Time; interface DateSlotableInterface { public function getStart(): ?\DateTime; public function getEnd(): ?\DateTime; public function toDateSlot(): DateSlotInterface; }
namespace SportFinder\Time; interface DateSlotInterface extends DateSlotableInterface { public function contains($dateTimeOrDateSlot, $openLeft = false, $openRight = false): bool; public function equals(DateSlotInterface $dateSlot): bool; public function intersect(DateSlotableInterface $interval = null); public function subtract($dateSlot); public function getDuration($unit = Units::SECOND): int; public function hasTimeLeft(): bool; public function sub(\DateInterval $interval); public function add(\DateInterval $interval); }
namespace SportFinder\Time; interface ComparatorInterface { public function isBefore($dateTimeOrDateSlot, $intervalOpen = false): bool; public function isAfter($dateTimeOrDateSlot, $intervalOpen = false): bool; }
类
- DateSlot: 包含复杂的和有用的业务逻辑
- Units: 包含时间单位的最终类
- DateTime: \DateTime的一个特殊化实现,实现了ComparatorInterface和DateSlotableInterface
namespace SportFinder\Time; class DateSlot implements DateSlotInterface, DurationInterface, ComparatorInterface{}
namespace SportFinder\Time; class DateTime extends \DateTime implements ComparatorInterface, DateSlotableInterface{}
用法
创建对象
$from = \DateTime::createFromFormat('Y-m-d', '2020-01-01'); $to = \DateTime::createFromFormat('Y-m-d', '2020-01-31'); $dateSlot1 = new DateSlot($from, $to); $dateSlot2 = new DateSlot($from, $to); $dateSlot1 == $dateSlot2; // true
修改对象
$dateSlot1 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2020-01-01'), \DateTime::createFromFormat('Y-m-d', '2020-01-02')); $dateSlot2 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2020-01-02'), \DateTime::createFromFormat('Y-m-d', '2020-01-03')); $dateSlot1 == $dateSlot2; // false $dateSlot1->add(new \DateInterval('P1D')); $dateSlot1 == $dateSlot2; // true
比较
// [2020-01-01, 2020-01-02] $dateSlot1 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2020-01-01'), \DateTime::createFromFormat('Y-m-d', '2020-01-02')); // [2020-01-03, 2020-01-04] $dateSlot2 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2020-01-03'), \DateTime::createFromFormat('Y-m-d', '2020-01-04')); $dateSlot1->isBefore($dateSlot2); // true $dateSlot2->isAfter($dateSlot1); // true // [2020-01-01, 2020-01-02] $dateSlot1 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2020-01-01'), \DateTime::createFromFormat('Y-m-d', '2020-01-02')); // [2020-01-02, 2020-01-03] $dateSlot2 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2020-01-02'), \DateTime::createFromFormat('Y-m-d', '2020-01-03')); $dateSlot1->isBefore($dateSlot2); // [2020-01-01, 2020-01-02] < [2020-01-02, 2020-01-03] ? false $dateSlot1->isBefore($dateSlot2, true); // [2020-01-01, 2020-01-02[ < ]2020-01-02, 2020-01-03] ? true
交集
$dateSlot1 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2000-01-01'), \DateTime::createFromFormat('Y-m-d', '2000-01-31')); $dateSlot2 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '1999-01-01'), \DateTime::createFromFormat('Y-m-d', '2000-01-31')); $intersect = $dateSlot1->intersect($dateSlot2); $expected = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2000-01-01'), \DateTime::createFromFormat('Y-m-d', '2000-01-31')); $intersect == $expected; // True
减去
$year2000 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2000-01-01'), \DateTime::createFromFormat('Y-m-d', '2000-12-31')); $february2000 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2000-02-01'), \DateTime::createFromFormat('Y-m-d', '2000-03-01')); $november2000 = new DateSlot(\DateTime::createFromFormat('Y-m-d', '2000-11-01'), \DateTime::createFromFormat('Y-m-d', '2000-12-01')); $year2000->subtract($february2000); // [[2000-01-01, 2000-02-01], [2000-03-01, 2000-12-31]] $year2000->subtract([$february2000, $november2000]); // [[2000-01-01, 2000-02-01], [2000-03-01, 2000-11-01], [2000-12-01, 2000-12-31]]
开放问题与反思
- 应该使DateSlot不可变吗?(参照sub和add方法)
- 我们应该创建一个ImmutableDateSlot类吗?
贡献与联系方式
SportFinder是一家比利时公司。该平台基于Symfony。我们将尽可能多的发布代码到开源社区。我们刚开始接触开源贡献。
欢迎联系我们