icecave/chrono 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

icecave/chrono

Composer 安装命令:

composer require icecave/chrono

包简介

A date & time library that is decoupled from the system clock.

README 文档

README

Build Status Code Coverage Latest Version

Chrono is a PHP date & time library that is decoupled from the system clock.

composer require icecave/chrono

Rationale

Many date & time operations in the core PHP libraries require access to system state such as the current wall time, or resources such as timezone databases. These hard-wired dependencies can make it very difficult to write well-abstracted and testable code when dealing with time-sensitive operations.

Chrono provides a set of date & time classes that are completely decoupled from the system and hence behave consistently, regardless of system state and configuration (such as the date.timezone INI directive).

A SystemClock instance must be explicitly constructed before any global date & time operations are used. Classes that require use of a clock may take a ClockInterface as a dependency, improving decoupling and testability.

Concepts

  • Clock: A factory for chronological measurements.
  • Time: A chronological measurement with a time component.
  • Date: A chronological measurement with a date component.
  • Time Point: A discreet point on the time-continuum.
  • Time Span: An un-anchored span of time.
  • Interval: A span of time between two Time Points.

Implementations

  • System Clock: A factory for chronological measurements that uses the system clock.
  • Test Clock: A clock that can be manually manipulated for testing purposes.
  • Date: Represents a date. Models the Time Point and Date concepts.
  • Time of Day: Represents a time of day. Models the Time concept.
  • Date Time: Represents a time of day on specific date. Models the Time Point, Date and Time concepts.
  • Interval: A span of time between two Time Points. Models the Interval concept.
  • Month: A one month time span. Models the Interval concept.
  • Year: A one year time span. Models the Interval concept.
  • Duration: A time span measured in seconds with no beginning or end. Models the Time Span concept.
  • Period: A time span specified in component form (eg: 3 months, 4 days), models the Time Span concept.

Examples

Getting the current time

In order to get the current time you need to use a clock. Most of the time in production code you will use the SystemClock class, which uses the machine's current system time and time zone information.

use Icecave\Chrono\Clock\SystemClock;

// Construct a new system clock ...
$clock = new SystemClock;

// Obtain a DateTime instance representing the current date and time ...
$now = $clock->localDateTime();

// Obtain a Date instance representing the current date ...
$today = $clock->localDate();

// Obtain the current time of day ...
$timeOfDay = $clock->localTime();

Each of the clock methods shown above has a UTC counterpart. For example, to obtain the current time in UTC you can use the following code:

$nowUtc = $clock->utcDateTime();

String formatting

To produce a formatted string representing a Date, DateTime, TimeOfDay or TimeZone instance use the format() method.

The output is specified using the same format as PHP's built-in date() function.

$now = $clock->localDateTime();
$string = $now->format('Y-m-d H:i:s');

Casting the object as a string (or calling isoString()) produces an ISO-8601 string representation.

Unix timestamps

Date and DateTime instances can be produced from unix timestamps using the fromUnixTime() static method. The unix timestamp can be retrieved using unixTime().

$dateTime = DateTime::fromUnixTime(1367823963);
$timestamp = $dateTime->unixTime();

PHP native "DateTime" objects

Date and DateTime instances can be produced from native PHP DateTime instances using the fromNativeDateTime() static method, and can be converted to a native DateTime using nativeDateTime().

use DateTime as NativeDateTime;
use Icecave\Chrono\DateTime;

$dateTime = DateTime::fromNativeDateTime(new NativeDateTime);
$nativeDateTime = $dateTime->nativeDateTime();

icecave/chrono 适用场景与选型建议

icecave/chrono 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 195.47k 次下载、GitHub Stars 达 54, 最近一次更新时间为 2013 年 01 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「time」 「date」 「timer」 「clock」 「chrono」 「iso」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 icecave/chrono 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 icecave/chrono 我们能提供哪些服务?
定制开发 / 二次开发

基于 icecave/chrono 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 195.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 56
  • 点击次数: 29
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 54
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-01-22