All Projects → brick → Date Time

brick / Date Time

Licence: mit
Date and time library for PHP

Projects that are alternatives of or similar to Date Time

Time
Building a better date/time library for Swift
Stars: ✭ 1,983 (+1449.22%)
Mutual labels:  timezone, date-time
Swiftdate
🐔 Toolkit to parse, validate, manipulate, compare and display dates, time & timezones in Swift.
Stars: ✭ 6,661 (+5103.91%)
Mutual labels:  timezone, date-time
Windows Iana
A small tool to convert Windows time zones to IANA
Stars: ✭ 17 (-86.72%)
Mutual labels:  timezone
Luatz
Time, Date and Timezone library for lua
Stars: ✭ 92 (-28.12%)
Mutual labels:  timezone
Google Time Zone
Get time zones for coordinates
Stars: ✭ 54 (-57.81%)
Mutual labels:  timezone
Time Zone Proposal
A proposal for accurately computing local time in JavaScript
Stars: ✭ 25 (-80.47%)
Mutual labels:  timezone
Bottomsheetpickers
Third-party date and time pickers for Android.
Stars: ✭ 1,099 (+758.59%)
Mutual labels:  date-time
Geolocator
A utility for getting geo-location information via HTML5 and IP look-ups, geocoding, address look-ups, distance and durations, timezone information and more...
Stars: ✭ 598 (+367.19%)
Mutual labels:  timezone
Ticktock
A timezone data management library for the JVM and Android targeting java.time APIs in Java 8+
Stars: ✭ 122 (-4.69%)
Mutual labels:  timezone
Tz offset
Simple abstraction of a timezone offset
Stars: ✭ 35 (-72.66%)
Mutual labels:  timezone
Js Joda
🕑 Immutable date and time library for javascript
Stars: ✭ 1,298 (+914.06%)
Mutual labels:  timezone
Goodtimes
Java 8 Date/Time API enhancements for Groovy
Stars: ✭ 35 (-72.66%)
Mutual labels:  date-time
Date Info
API to let user fetch the events that happen(ed) on a specific date
Stars: ✭ 7 (-94.53%)
Mutual labels:  date-time
Time
A simplified approach to working with dates, times, and time zones.
Stars: ✭ 83 (-35.16%)
Mutual labels:  timezone
Paper Timezone
Polymer based timezone selection component
Stars: ✭ 19 (-85.16%)
Mutual labels:  timezone
Timezonepicker
A TimeZonePicker UIViewController similar to the iOS Settings app. Search and select from a range of cities and countries to find your most suitable time zone.
Stars: ✭ 109 (-14.84%)
Mutual labels:  timezone
Event Bot
📣 Discord Bot to make announcements about upcoming sessions for the Fellows using Google Calendar and Calendly
Stars: ✭ 21 (-83.59%)
Mutual labels:  timezone
Posix tz db
Generates POSIX timezones strings
Stars: ✭ 57 (-55.47%)
Mutual labels:  timezone
Moment Range
Fancy date ranges for Moment.js
Stars: ✭ 1,639 (+1180.47%)
Mutual labels:  date-time
Date Fns Timezone
Parsing and formatting date strings using IANA time zones for date-fns.
Stars: ✭ 118 (-7.81%)
Mutual labels:  timezone

Brick\DateTime

A powerful set of immutable classes to work with dates and times.

Build Status Coverage Status Latest Stable Version Total Downloads License

Introduction

This library builds an extensive API on top of the native PHP date-time classes, and adds missing concepts such as LocalDate, LocalTime, YearMonth, MonthDay, etc.

The classes follow the ISO 8601 standard for representing date and time concepts.

This component follows an important part of the JSR 310 (Date and Time API) specification from Java. Don't expect an exact match of class and method names though, as a number of differences exist for technical or practical reasons.

All the classes are immutable, they can be safely passed around without being affected.

Installation

This library is installable via Composer:

composer require brick/date-time

Requirements

This library requires PHP 7.1 or later.

Project status & release process

While this library is still under development, it is well tested and should be stable enough to use in production environments.

The current releases are numbered 0.x.y. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.), y is incremented.

When a breaking change is introduced, a new 0.x version cycle is always started.

It is therefore safe to lock your project to a given release cycle, such as 0.2.*.

If you need to upgrade to a newer release cycle, check the release history for a list of changes introduced by each further 0.x.0 version.

Overview

Main classes

The following classes represent the date-time concepts:

  • DayOfWeek: a day-of-week such as Monday
  • Duration: a duration measured in seconds and nanoseconds
  • Instant: a point in time, with a nanosecond precision
  • Interval: a period of time between two instants
  • LocalDate: an isolated date such as 2014-08-31
  • LocalDateRange: an inclusive range of local dates, such as 2014-01-01/2014-12-31
  • LocalDateTime: a date-time without a time-zone, such as 2014-08-31T10:15:30
  • LocalTime: an isolated time such as 10:15:30
  • Month: a month-of-year such as January
  • MonthDay: a combination of a month and a day, without a year, such as --12-31
  • Period: a date-based amount of time, such as '2 years, 3 months and 4 days'
  • TimeZoneOffset: an offset-based time-zone, such as +01:00
  • TimeZoneRegion: a region-based time-zone, such as Europe/London
  • Year: a year in the proleptic calendar
  • YearMonth: a combination of a year and a month, such as 2014-08
  • ZonedDateTime: a date-time with a time-zone, such as 2014-08-31T10:15:30+01:00. This class is conceptually equivalent to the native DateTime class

These classes belong to the Brick\DateTime namespace.

Clocks

All objects read the current time from a Clock implementation. The following implementations are available:

  • SystemClock returns the system time; it's the default clock
  • FixedClock: returns a pre-configured time
  • OffsetClock: adds an offset to another clock
  • ScaleClock: makes another clock fast forward by a scale factor

These classes belong to the Brick\DateTime\Clock namespace.

In your application, you will most likely never touch the defaults, and always use the default clock:

use Brick\DateTime\LocalDate;
use Brick\DateTime\TimeZone;

echo LocalDate::now(TimeZone::utc()); // 2017-10-04

In your tests however, you might need to set the current time to test your application in known conditions. To do this, you can either explicitly pass a Clock instance to now() methods:

use Brick\DateTime\Clock\FixedClock;
use Brick\DateTime\Instant;
use Brick\DateTime\LocalDate;
use Brick\DateTime\TimeZone;

$clock = new FixedClock(Instant::of(1000000000));
echo LocalDate::now(TimeZone::utc(), $clock); // 2001-09-09

Or you can change the default clock for all date-time classes. All methods such as now(), unless provided with an explicit Clock, will use the default clock you provide:

use Brick\DateTime\Clock\FixedClock;
use Brick\DateTime\DefaultClock;
use Brick\DateTime\Instant;
use Brick\DateTime\LocalDate;
use Brick\DateTime\TimeZone;

DefaultClock::set(new FixedClock(Instant::of(1000000000)));
echo LocalDate::now(TimeZone::utc()); // 2001-09-09

DefaultClock::reset(); // do not forget to reset the clock to the system clock!

There are also useful shortcut methods to use clocks in your tests, inspired by timecop:

  • freeze() freezes time to a specific point in time
  • travel() travels to a specific point in time, but allows time to continue moving forward from there
  • scale() makes time move at a given pace

Freeze the time to a specific point

use Brick\DateTime\DefaultClock;
use Brick\DateTime\Instant;

DefaultClock::freeze(Instant::of(2000000000));

$a = Instant::now(); sleep(1);
$b = Instant::now();

echo $a, PHP_EOL; // 2033-05-18T03:33:20Z
echo $b, PHP_EOL; // 2033-05-18T03:33:20Z

DefaultClock::reset();

Travel to a specific point in time

use Brick\DateTime\DefaultClock;
use Brick\DateTime\Instant;

DefaultClock::travel(Instant::of(2000000000));
$a = Instant::now(); sleep(1);
$b = Instant::now();

echo $a, PHP_EOL; // 2033-05-18T03:33:20.000342Z
echo $b, PHP_EOL; // 2033-05-18T03:33:21.000606Z

DefaultClock::reset();

Make time move at a given pace

use Brick\DateTime\DefaultClock;
use Brick\DateTime\Instant;

DefaultClock::travel(Instant::of(2000000000));
DefaultClock::scale(60); // 1 second becomes 60 seconds

$a = Instant::now(); sleep(1);
$b = Instant::now();

echo $a, PHP_EOL; // 2033-05-18T03:33:20.00188Z
echo $b, PHP_EOL; // 2033-05-18T03:34:20.06632Z

DefaultClock::reset();

As you can see, you can even combine travel() and scale() methods.

Be very careful to reset() the DefaultClock after each of your tests! If you're using PHPUnit, a good place to do this is in the tearDown() method.

Exceptions

The following exceptions can be thrown:

  • Brick\DateTime\DateTimeException when an illegal operation is performed
  • Brick\DateTime\Parser\DateTimeParseException when parse()ing an invalid string representation
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].