mawebdk/utc-datetime 问题修复 & 功能扩展

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

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

mawebdk/utc-datetime

最新稳定版本:2.0.1

Composer 安装命令:

composer require mawebdk/utc-datetime

包简介

Representation of a UTC date and time with easy mocking of current date and time for testing.

README 文档

README

This package contains a representation of a UTC date and time with easy mocking of current date and time for testing.

Use UtcDateTime and UtcDateTimeImmutable to handle UTC date and times.

UtcDateTime and UtcDateTimeImmutable behaves the same, except:

  • When calling modification methods on a UtcDateTime object, the object is modified itself.
  • When calling modification methods on a UtcDateTimeImmutable object, a new object will be returned.

Usage

Create a new UtcDateTime object with the current UTC date and time.

Note: Method now() uses Clock to get current date and time to enable support for mocking of current date and time.

$UtcDateTime = UtcDateTime::now();

Create a new UtcDateTime object with a given Unix timestamp with optional milliseconds or microseconds.

$utcDateTime = UtcDateTime::createFromUnixTimestamp(unixTimestamp: $unixTimestamp);
$utcDateTime = UtcDateTime::createFromUnixMilliTimestamp(unixMilliTimestamp: $unixMilliTimestamp);
$utcDateTime = UtcDateTime::createFromUnixMicroTimestamp(unixMicroTimestamp: $unixMicroTimestamp);

Get Unix timestamp with optional milliseconds or microseconds from a UtcDateTime object.

$unixTimestamp      = $utcDateTime->getUnixTimestamp;
$unixMilliTimestamp = $utcDateTime->getUnixMilliTimestamp;
$unixMicroTimestamp = $utcDateTime->getUnixMicroTimestamp;

Get MySQL date and time with microseconds format from a UtcDateTime object.

$mysqlDateTime6 = $utcDateTime->formatMysqlDateTime6();

Mocking of current UTC date and time

Use ReflectionClass to set the Clock singleton to a mocked class implementing ClockInterface.

/**
 * @throws Exception
 * @throws UtcDateTimeException
 */
public function testNow_Mocking()
{
    $mockedDateTimeImmutable = DateTimeImmutable::createFromFormat(
        format: 'Y-m-d H:i:s.u',
        datetime: '1999-12-31 23:59:59.999999',
        timezone: new DateTimeZone(timezone: 'UTC')
    );

    $mockedClock = $this->createMock(type: ClockInterface::class);
    $mockedClock
        ->method(constraint: 'now')
        ->willReturn($mockedDateTimeImmutable);

    $reflectionClass = new ReflectionClass(objectOrClass: Clock::class);
    $reflectionClass->setStaticPropertyValue(name: 'singleton', value: $mockedClock);

    $this->assertSame(
        expected: $mockedDateTimeImmutable->format(format: 'Y-m-d H:i:s.u'),
        actual: UtcDateTimeImmutable::now()->formatMysqlDateTime6()
    );
}

Remember to reset singleton to ensure usage of correct date and time in subsequent tests.

protected function tearDown(): void
{
    // Reset singleton to ensure usage of correct date and time in subsequent tests.
    $reflectionClass = new ReflectionClass(objectOrClass: Clock::class);
    $reflectionClass->setStaticPropertyValue(name: 'singleton', value: null);
}

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2025-02-23