2017 © Pedro Peláez
 

library chrono

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

image

icecave/chrono

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

  • Tuesday, December 13, 2016
  • by jmalloc
  • Repository
  • 3 Watchers
  • 54 Stars
  • 38,033 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 2 Forks
  • 29 Open issues
  • 13 Versions
  • 9 % Grown

The README.md

Chrono

Build Status Code Coverage Latest Version, (*1)

Chrono is a PHP date & time library that is decoupled from the system clock., (*2)

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., (*3)

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)., (*4)

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., (*5)

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., (*6)

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:, (*7)

$nowUtc = $clock->utcDateTime();

String formatting

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

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

$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., (*10)

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()., (*11)

$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()., (*12)

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

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

The Versions

13/12 2016

dev-develop

dev-develop https://github.com/IcecaveStudios/chrono

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

  Sources   Download

MIT

The Requires

 

The Development Requires

date time timer iso clock chrono

19/04 2016

dev-master

9999999-dev https://github.com/IcecaveStudios/chrono

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

  Sources   Download

MIT

The Requires

 

The Development Requires

date time timer iso clock chrono

19/04 2016

1.0.4

1.0.4.0 https://github.com/IcecaveStudios/chrono

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

  Sources   Download

MIT

The Requires

 

The Development Requires

date time timer iso clock chrono

31/05 2015

dev-feature/tzconv

dev-feature/tzconv https://github.com/IcecaveStudios/chrono

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

  Sources   Download

MIT

The Requires

 

The Development Requires

date time timer iso clock chrono

16/02 2015

1.0.3

1.0.3.0 https://github.com/IcecaveStudios/chrono

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

  Sources   Download

MIT

The Requires

 

The Development Requires

date time timer iso clock chrono

10/11 2014

1.0.2

1.0.2.0 https://github.com/IcecaveStudios/chrono

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

  Sources   Download

MIT

The Requires

 

The Development Requires

date time timer iso clock chrono

20/10 2014

1.0.1

1.0.1.0 https://github.com/IcecaveStudios/chrono

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

  Sources   Download

MIT

The Requires

 

The Development Requires

date time timer iso clock chrono

09/09 2014

1.0.0

1.0.0.0 https://github.com/IcecaveStudios/chrono

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

  Sources   Download

MIT

The Requires

 

The Development Requires

date time timer iso clock chrono

04/03 2014

0.4.0

0.4.0.0 https://github.com/IcecaveStudios/chrono

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

  Sources   Download

MIT

The Requires

 

The Development Requires

date time timer iso clock chrono

24/01 2014

0.3.0

0.3.0.0 https://github.com/IcecaveStudios/chrono

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

  Sources   Download

MIT

The Requires

 

The Development Requires

date time timer iso clock chrono

14/05 2013

0.2.0

0.2.0.0 https://github.com/IcecaveStudios/chrono

PHP date and time abstractions that are decoupled from the system clock.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

date time timer iso clock chrono

10/05 2013

0.1.1

0.1.1.0 https://github.com/IcecaveStudios/chrono

PHP date and time abstractions that are decoupled from the system clock.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

date time timer iso clock chrono

06/05 2013

0.1.0

0.1.0.0 https://github.com/IcecaveStudios/chrono

PHP date and time abstractions that are decoupled from the system clock.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

date time timer iso clock chrono