All Projects → cakephp → Chronos

cakephp / Chronos

Licence: mit
A standalone DateTime library originally based off of Carbon

Projects that are alternatives of or similar to Chronos

Time.dart
⏰ Type-safe DateTime and Duration calculations, powered by extensions.
Stars: ✭ 363 (-69.11%)
Mutual labels:  library, time
Dateutil
Useful extensions to the standard Python datetime features
Stars: ✭ 1,706 (+45.19%)
Mutual labels:  library, time
Linear Time Picker
Gorgeous Android Time and Date picker library inspired by the Timely app
Stars: ✭ 613 (-47.83%)
Mutual labels:  library, time
Jhtalib
Technical Analysis Library Time-Series
Stars: ✭ 131 (-88.85%)
Mutual labels:  library, time
Python Progressbar
Progressbar 2 - A progress bar for Python 2 and Python 3 - "pip install progressbar2"
Stars: ✭ 682 (-41.96%)
Mutual labels:  library, time
Hazelcast Cpp Client
Hazelcast IMDG C++ Client
Stars: ✭ 67 (-94.3%)
Mutual labels:  library
Egeo
EGEO is the open-source UI library used to build Stratio's UI. It includes UI Components, Utilities, Services and much more to build user interfaces quickly and with ease. The library is distributed in AoT mode.
Stars: ✭ 69 (-94.13%)
Mutual labels:  library
Vue Clock2
vue clock component 😀
Stars: ✭ 67 (-94.3%)
Mutual labels:  time
Cordova Plugin Test Framework
Apache Cordova
Stars: ✭ 66 (-94.38%)
Mutual labels:  library
Calendarview
Calendar View Library
Stars: ✭ 71 (-93.96%)
Mutual labels:  library
Cscss
CSS parsing library for C# based on Mozilla Firefox code [MPL]
Stars: ✭ 70 (-94.04%)
Mutual labels:  library
Androidaudioconverter
Convert audio files inside your Android app easily. Supported formats: AAC, MP3, M4A, WMA, WAV and FLAC.
Stars: ✭ 1,156 (-1.62%)
Mutual labels:  library
Notifier
Notifications library made with VanillaJS.
Stars: ✭ 67 (-94.3%)
Mutual labels:  library
Bitcoin Elixir
Bitcoin tools and full node implementation in Elixir.
Stars: ✭ 70 (-94.04%)
Mutual labels:  library
Markdown Builder
1kb Markdown builder for Node.js
Stars: ✭ 67 (-94.3%)
Mutual labels:  library
Iso8601
Ruby parser to work with ISO8601 dateTimes and durations — http://en.wikipedia.org/wiki/ISO_8601
Stars: ✭ 70 (-94.04%)
Mutual labels:  time
Evalne
Source code for EvalNE, a Python library for evaluating Network Embedding methods.
Stars: ✭ 67 (-94.3%)
Mutual labels:  library
Anglesharp.js
👼 Extends AngleSharp with a .NET-based JavaScript engine.
Stars: ✭ 68 (-94.21%)
Mutual labels:  library
Retry
A Rust library to retry some code until its return value satisfies a condition.
Stars: ✭ 70 (-94.04%)
Mutual labels:  library
Andes
Python toolbox / library for power system transient dynamics simulation with symbolic modeling and numerical analysis 🔥
Stars: ✭ 68 (-94.21%)
Mutual labels:  library

CakePHP Chronos

Build Status Latest Stable Version Total Downloads Code Coverage Software License

Chronos aims to be a drop-in replacement for nesbot/carbon. It focuses on providing immutable date/datetime objects. Immutable objects help ensure that datetime objects aren't accidentally modified keeping data more predictable.

Installation

Installing with composer:

$ composer require cakephp/chronos

You can then use Chronos:

<?php
require 'vendor/autoload.php';

use Cake\Chronos\Chronos;

printf("Now: %s", Chronos::now());

Differences with nesbot/carbon

The biggest and main difference is that Chronos extends DateTimeImmutable instead of DateTime. Immutability for date values has proven to be a great way of avoiding bugs and reduce the amount of code, since developers don't have to manually copy the instance every time they need a change.

Another important feature it offers is the Date class, which is used for representing dates without time (calendar dates). Any time method called on this type of object is basically a no-op.

There are other implementation changes, but one that users might not notice is Chronos considers Monday as the start of the week instead of Sunday. This follows the ISO-8601 and current versions of PHP 5.6 and PHP 7.

A minor but still noticeable difference is that Chronos has no external dependencies, it is completely standalone.

Finally, Chronos is faster than Carbon as it has been optimized for the creation of hundreds of instances with minimal overhead.

Migrating from Carbon

First add cakephp/chronos to your composer.json:

php composer.phar require cakephp/chronos

By default Chronos includes a compatibility script that creates aliases for the relevant Carbon classes. This will let most applications upgrade with very little effort. If you'd like to permanently update your code, you will need to update imports and typehints. Assuming src contains the files you want to migrate, we could use the following to update files:

# Replace imports
find ./src -type f -name '*.php' -exec sed -i '' 's/use Carbon\\CarbonInterval/use Cake\\Chronos\\ChronosInterval/g' {} \;
find ./src -type f -name '*.php' -exec sed -i '' 's/use Carbon\\Carbon/use Cake\\Chronos\\Chronos/g' {} \;

# Replace typehints and extensions
find ./src -type f -name '*.php' -exec sed -i '' 's/CarbonInterval/ChronosInterval/g' {} \;
find ./src -type f -name '*.php' -exec sed -i '' 's/Carbon/Chronos/g' {} \;

At this point your code should mostly work as it did before. The biggest difference is that Chronos instances are immutable.

Immutable Object Changes

Immutable objects have a number of advantages:

  1. Using immutable objects is always free of side-effects.
  2. Dates and times don't accidentally change underneath other parts of your code.

With those benefits in mind, there are a few things you need to keep in mind when modifying immutable objects:

// This will lose modifications
$date = new Chronos('2015-10-21 16:29:00');
$date->modify('+2 hours');

// This will keep modifications
$date = new Chronos('2015-10-21 16:29:00');
$date = $date->modify('+2 hours');

Getting Mutable Objects

In the case that you need a mutable instance you can get one:

$time = new Chronos('2015-10-21 16:29:00');
$mutable = $time->toMutable();

$date = new Date('2015-10-21');
$mutable = $date->toMutable();

Converting Mutable Objects into Immutable ones.

If you have a mutable object and want an immutable variant you can do the following:

$time = new MutableDateTime('2015-10-21 16:29:00');
$fixed = $time->toImmutable();

$date = new MutableDate('2015-10-21');
$fixed = $date->toImmutable();

Calendar Dates

PHP only offers datetime objects as part of the native extensions. Chronos adds a number of conveniences to the traditional DateTime object and introduces a Date object. Date instances offer compatibility with the ChronosInterface, but have their time frozen to 00:00:00 and the timezone set to the server default timezone. This makes them ideal when working with calendar dates as the time components will always match.

use Cake\Chronos\Date;

$today = new Date();
echo $today;
// Outputs '2015-10-21'

echo $today->modify('+3 hours');
// Outputs '2015-10-21'

Like instances of Chronos, Date objects are also immutable. The MutableDate class provides a mutable variant of Date.

Documentation

A more descriptive documentation can be found at book.cakephp.org/chronos/2/en/.

API Documentation

API documentation can be found on api.cakephp.org/chronos.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].