Skip to content

Loading…

Add hour to DATE_ADD and DATE_SUB #354

Merged
merged 4 commits into from

3 participants

@makhov

Add getDateAddHourExpression and getDateSubHourExpression to use DATE_ADD and DATE_SUB in doctrine with hours.

@makhov makhov referenced this pull request in doctrine/doctrine2
Merged

Add hour to DATE_ADD and DATE_SUB #748

@makhov

Pull request to doctrine/doctrine2 — doctrine/doctrine2#748

@Ocramius
Doctrine member

@makhov these are DATE_ADD_HOUR and DATE_SUB_HOURS, not DATE_ADD and DATE_SUB... Isn't there a more consistent way of adding dates in general?

amakhov added some commits
@makhov

@Ocramius sorry, what do you mean?

@guilhermeblanco
Doctrine member

@Ocramius DATE_ADD and DATE_SUB are standardized to deal with date/time manipulation already.

@guilhermeblanco guilhermeblanco merged commit b4d80ab into doctrine:master

1 check passed

Details default The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 6, 2013
  1. Add hour to DATE_ADD and DATE_SUB

    amakhov committed
  2. Add hour to DATE_ADD and DATE_SUB. Test

    amakhov committed
  3. Fix tests

    amakhov committed
  4. Fix SQLite

    amakhov committed
This page is out of date. Refresh to see the latest.
View
30 lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
@@ -940,6 +940,36 @@ public function getDateDiffExpression($date1, $date2)
}
/**
+ * Returns the SQL to add the number of given hours to a date.
+ *
+ * @param string $date
+ * @param integer $hours
+ *
+ * @return string
+ *
+ * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
+ */
+ public function getDateAddHourExpression($date, $hours)
+ {
+ throw DBALException::notSupported(__METHOD__);
+ }
+
+ /**
+ * Returns the SQL to subtract the number of given hours to a date.
+ *
+ * @param string $date
+ * @param integer $hours
+ *
+ * @return string
+ *
+ * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
+ */
+ public function getDateSubHourExpression($date, $hours)
+ {
+ throw DBALException::notSupported(__METHOD__);
+ }
+
+ /**
* Returns the SQL to add the number of given days to a date.
*
* @param string $date
View
16 lib/Doctrine/DBAL/Platforms/DrizzlePlatform.php
@@ -68,6 +68,22 @@ public function getDateDiffExpression($date1, $date2)
/**
* {@inheritDoc}
*/
+ public function getDateAddHourExpression($date, $hours)
+ {
+ return 'DATE_ADD(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getDateSubHourExpression($date, $hours)
+ {
+ return 'DATE_SUB(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public function getDateAddDaysExpression($date, $days)
{
return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
View
16 lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
@@ -118,6 +118,22 @@ public function getDateDiffExpression($date1, $date2)
/**
* {@inheritDoc}
*/
+ public function getDateAddHourExpression($date, $hours)
+ {
+ return 'DATE_ADD(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getDateSubHourExpression($date, $hours)
+ {
+ return 'DATE_SUB(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public function getDateAddDaysExpression($date, $days)
{
return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
View
16 lib/Doctrine/DBAL/Platforms/OraclePlatform.php
@@ -113,6 +113,22 @@ public function getDateDiffExpression($date1, $date2)
/**
* {@inheritDoc}
*/
+ public function getDateAddHourExpression($date, $hours)
+ {
+ return '(' . $date . '+' . $hours . '/24)';
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getDateSubHourExpression($date, $hours)
+ {
+ return '(' . $date . '-' . $hours . '/24)';
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public function getDateAddDaysExpression($date, $days)
{
return '(' . $date . '+' . $days . ')';
View
16 lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
@@ -103,6 +103,22 @@ public function getDateDiffExpression($date1, $date2)
/**
* {@inheritDoc}
*/
+ public function getDateAddHourExpression($date, $hours)
+ {
+ return "(" . $date ." + (" . $hours . " || ' hour')::interval)";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getDateSubHourExpression($date, $hours)
+ {
+ return "(" . $date ." - (" . $hours . " || ' hour')::interval)";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public function getDateAddDaysExpression($date, $days)
{
return "(" . $date ." + (" . $days . " || ' day')::interval)";
View
16 lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
@@ -49,6 +49,22 @@ public function getDateDiffExpression($date1, $date2)
/**
* {@inheritDoc}
*/
+ public function getDateAddHourExpression($date, $hours)
+ {
+ return 'DATEADD(hour, ' . $hours . ', ' . $date . ')';
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getDateSubHourExpression($date, $hours)
+ {
+ return 'DATEADD(hour, -1 * ' . $hours . ', ' . $date . ')';
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public function getDateAddDaysExpression($date, $days)
{
return 'DATEADD(day, ' . $days . ', ' . $date . ')';
View
16 lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
@@ -133,6 +133,22 @@ public function getDateDiffExpression($date1, $date2)
/**
* {@inheritDoc}
*/
+ public function getDateAddHourExpression($date, $hours)
+ {
+ return "DATETIME(" . $date . ",'+". $hours . " hour')";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getDateSubHourExpression($date, $hours)
+ {
+ return "DATETIME(" . $date . ",'-". $hours . " hour')";
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public function getDateAddDaysExpression($date, $days)
{
return "DATE(" . $date . ",'+". $days . " day')";
View
4 tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
@@ -348,6 +348,8 @@ public function testDateArithmetics()
$p = $this->_conn->getDatabasePlatform();
$sql = 'SELECT ';
$sql .= $p->getDateDiffExpression('test_datetime', $p->getCurrentTimestampSQL()) .' AS diff, ';
+ $sql .= $p->getDateAddHourExpression('test_datetime', 3) .' AS add_hour, ';
+ $sql .= $p->getDateSubHourExpression('test_datetime', 3) .' AS sub_hour, ';
$sql .= $p->getDateAddDaysExpression('test_datetime', 10) .' AS add_days, ';
$sql .= $p->getDateSubDaysExpression('test_datetime', 10) .' AS sub_days, ';
$sql .= $p->getDateAddMonthExpression('test_datetime', 2) .' AS add_month, ';
@@ -359,6 +361,8 @@ public function testDateArithmetics()
$diff = floor( (strtotime('2010-01-01')-time()) / 3600 / 24);
$this->assertEquals($diff, (int)$row['diff'], "Date difference should be approx. ".$diff." days.", 1);
+ $this->assertEquals('2010-01-01 13:10', date('Y-m-d H:i', strtotime($row['add_hour'])), "Adding date should end up on 2010-01-01 13:10");
+ $this->assertEquals('2010-01-01 07:10', date('Y-m-d H:i', strtotime($row['sub_hour'])), "Subtracting date should end up on 2010-01-01 07:10");
$this->assertEquals('2010-01-11', date('Y-m-d', strtotime($row['add_days'])), "Adding date should end up on 2010-01-11");
$this->assertEquals('2009-12-22', date('Y-m-d', strtotime($row['sub_days'])), "Subtracting date should end up on 2009-12-22");
$this->assertEquals('2010-03-01', date('Y-m-d', strtotime($row['add_month'])), "Adding month should end up on 2010-03-01");
Something went wrong with that request. Please try again.