Skip to content

Commit

Permalink
Merge pull request #1 from phillipsdata/update-namespace
Browse files Browse the repository at this point in the history
Changed namespace from `minphp\Date` to `Minphp\Date`
  • Loading branch information
clphillips committed Dec 11, 2015
2 parents eefbc57 + 5dee6c5 commit 5ef032d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 46 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# minphp/Date
# Minphp/Date

[![Build Status](https://travis-ci.org/phillipsdata/minphp-date.svg?branch=master)](https://travis-ci.org/phillipsdata/minphp-date) [![Coverage Status](https://coveralls.io/repos/phillipsdata/minphp-date/badge.svg)](https://coveralls.io/r/phillipsdata/minphp-date)

Expand All @@ -9,7 +9,7 @@ Date manipulation library.
Install via composer:

```sh
composer require minphp/date:dev-master
composer require minphp/date
```

## Basic Usage
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
"psr-4": {"minphp\\Date\\": "src"}
"psr-4": {"Minphp\\Date\\": "src"}
},
"autoload-dev": {
"psr-4": {"Minphp\\Date\\Tests\\": "tests"}
}
}
78 changes: 39 additions & 39 deletions src/Date.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace minphp\Date;
namespace Minphp\Date;

/**
* Provides methods useful in formatting dates and date timestamps.
Expand All @@ -18,7 +18,7 @@ class Date
const RFC3339 = "Y-m-d\TH:i:sP";
const RSS = "D, d M Y H:i:s O";
const W3C = "Y-m-d\TH:i:sP";

/**
* @var array Common date formats, predefined for PHP's date function, overwritable
* by the constructor
Expand All @@ -30,10 +30,10 @@ class Date
'year' => "Y",
'date_time' => "M d y g:i:s A",
);

private $timezone_from;
private $timezone_to;

/**
* Constructs a new Date component using the given date formats in $formats.
*
Expand All @@ -50,7 +50,7 @@ public function __construct(array $formats = null, $timezone_from = null, $timez
$this->setFormats($formats);
$this->setTimezone($timezone_from, $timezone_to);
}

/**
* Set the current time zone to be used during date calculations
*
Expand All @@ -64,7 +64,7 @@ public function setTimezone($from = null, $to = null)
$this->timezone_to = $to;
return $this;
}

/**
* Sets the formats to use as the pre-defined types.
*
Expand All @@ -81,7 +81,7 @@ public function setFormats(array $formats = null)
$this->formats = array_merge($this->formats, (array)$formats);
return $this;
}

/**
* Format a date using one of the date formats provided to the constructor,
* or predefined in this class.
Expand All @@ -94,7 +94,7 @@ public function cast($date, $format = "date")
{
return $this->format((isset($this->formats[$format]) ? $this->formats[$format] : $format), $date);
}

/**
* Format two dates to represent a range between them.
*
Expand All @@ -121,12 +121,12 @@ public function dateRange($start, $end, $formats = null)
'other' => "F j, Y"
)
);

$formats = $this->mergeArrays($default_formats, (array)$formats);

$s_date = date("Ymd", $this->toTime($start));
$e_date = date("Ymd", $this->toTime($end));

if ($s_date == $e_date) {
// Same day
return $this->format($formats['start']['same_day'], $start)
Expand All @@ -144,7 +144,7 @@ public function dateRange($start, $end, $formats = null)
return $this->format($formats['start']['other'], $start) . $this->format($formats['end']['other'], $end);
}
}

/**
* Format a date using the supply date string
*
Expand All @@ -162,17 +162,17 @@ public function format($format, $date = null)
if ($this->timezone_from !== null) {
$prev_timezone = $this->setDefaultTimezone($this->timezone_from);
}

$time = $this->toTime($date);

// Set the appropriate timezone
if ($this->timezone_to !== null) {
$this->setDefaultTimezone($this->timezone_to);
}

// Format the date
$formatted_date = date($format, $time);

// Restore the timezone value
if (isset($prev_timezone)) {
$this->setDefaultTimezone($prev_timezone);
Expand All @@ -181,7 +181,7 @@ public function format($format, $date = null)
}
return null;
}

/**
* Convert a date string to Unix time
*
Expand All @@ -195,7 +195,7 @@ public function toTime($date)
}
return $date;
}

/**
* Returns an array of months in key/value pairs
*
Expand All @@ -214,7 +214,7 @@ public function getMonths($start = 1, $end = 12, $key_format = "m", $value_forma
}
return $months;
}

/**
* Returns an array of keys in key/value pairs
*
Expand All @@ -233,7 +233,7 @@ public function getYears($start, $end, $key_format = "y", $value_format = "Y")
}
return $years;
}

/**
* Sets the default timezone
*
Expand All @@ -248,7 +248,7 @@ private function setDefaultTimezone($timezone)
}
return $cur_timezone;
}

/**
* Retrieve all timezones or those for a specific country
*
Expand All @@ -259,10 +259,10 @@ private function setDefaultTimezone($timezone)
*/
public function getTimezones($country = null)
{

// Hold the array of timezone data
$tz_data = array();

$accepted_zones = array_flip(array(
"Africa",
"America",
Expand All @@ -276,36 +276,36 @@ public function getTimezones($country = null)
"Pacific",
"UTC"
));

$listing = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country);
$num_listings = count($listing);

// Associate each timezone identifier with its meta data
for ($i=0; $i<$num_listings; $i++) {
// Convert timezone identifier to timezone array
$zone = new DateTimeZone($listing[$i]);
$zone_info = $zone->getTransitions(time(), time());

$timezone = $this->timezoneFromIdentifier($zone_info[0], $listing[$i]);
$primary_zone_name = isset($timezone['zone'][0]) ? $timezone['zone'][0] : false;

// Only allow accepted zones into the listing
if (!isset($accepted_zones[$primary_zone_name])) {
continue;
}

// Set the timezone to appear under its primary location
$tz_data[$primary_zone_name][] = $timezone;
}

// Sort each section by UTC offset
foreach ($tz_data as $zone => $data) {
$this->insertionSort($tz_data[$zone], "offset");
}

return $tz_data;
}

/**
* Constructs the timezone meta data using the given timezone and its identifier
*
Expand All @@ -326,24 +326,24 @@ public function getTimezones($country = null)
private function timezoneFromIdentifier(&$zone_info, $identifier)
{
$zone = explode('/', $identifier, 2);

$offset = isset($zone_info['offset']) ? $zone_info['offset'] : 0; // offset

$offset_h = str_pad(abs((int)($offset/3600)), 2, '0', STR_PAD_LEFT); // offset in hours
$offset_h = ($offset < 0 ? true : false ? "-" : "+") . $offset_h;
$offset_m = str_pad(abs((int)(($offset/60)%60)), 2, '0', STR_PAD_LEFT); // offset in mins

$timezone = array(
'id' => $identifier,
'name' => str_replace('_', ' ', isset($zone[1]) ? $zone[1] : $zone[0]),
'offset' => (int)$offset,
'utc' => $offset_h . ":" . $offset_m . (isset($zone_info['isdst']) && $zone_info['isdst'] ? " DST" : ""),
'zone' => $zone
);

return $timezone;
}

/**
* Insertion sort algorithm for numerically indexed arrays with string indexed elements.
* Will sort items in $array based on values in the $key index. Sorts arrays in place.
Expand All @@ -357,7 +357,7 @@ private static function insertionSort(&$array, $key)
self::insertSortInsert($array, $i, $array[$i], $key);
}
}

/**
* Insertion sort in inserter. Performs comparison and insertion for the given
* element within the given array.
Expand All @@ -375,7 +375,7 @@ private static function insertSortInsert(&$array, $length, $element, $key)
}
$array[$i+1] = $element;
}

/**
* Extends one array using another to overwrite existing values. Recursively merges
* data.
Expand All @@ -386,7 +386,7 @@ private static function insertSortInsert(&$array, $length, $element, $key)
*/
private function mergeArrays(array $arr1, array $arr2)
{

foreach ($arr2 as $key => $value) {
if (array_key_exists($key, $arr1) && is_array($value)) {
$arr1[$key] = $this->mergeArrays($arr1[$key], $arr2[$key]);
Expand Down
9 changes: 5 additions & 4 deletions tests/DateTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
namespace minphp\Date;
namespace Minphp\Date\Tests;

use \PHPUnit_Framework_TestCase;
use PHPUnit_Framework_TestCase;
use Minphp\Date\Date;

/**
* @coversDefaultClass \minphp\Date\Date
* @coversDefaultClass \Minphp\Date\Date
*/
class DateTest extends PHPUnit_Framework_TestCase
{
Expand All @@ -13,6 +14,6 @@ class DateTest extends PHPUnit_Framework_TestCase
*/
public function testConstruct()
{
$this->assertInstanceOf('minphp\Date\Date', new Date());
$this->assertInstanceOf('\Minphp\Date\Date', new Date());
}
}

0 comments on commit 5ef032d

Please sign in to comment.