Skip to content
Browse files

Added toUtc and toTimeZone requires further testing once DateTime is …

…complete (#7).
  • Loading branch information...
1 parent 5820c64 commit c54b421357caa98cd1ebbf151b5aa9f5bc46701f @jmalloc jmalloc committed Jan 22, 2013
View
26 lib/Icecave/Chrono/CommonInterface.php
@@ -6,17 +6,21 @@
*/
interface CommonInterface extends Iso8601Interface
{
- // /**
- // * @param TimeZone $timeZone
- // *
- // * @return ComponentInterface
- // */
- // public function toTimeZone(TimeZone $timeZone);
- //
- // /**
- // * @return ComponentInterface
- // */
- // public function toUtc();
+ /**
+ * Convert this date/time to a different timezone.
+ *
+ * @param TimeZone $timeZone The target timezone
+ *
+ * @return ComponentInterface
+ */
+ public function toTimeZone(TimeZone $timeZone);
+
+ /**
+ * Convert this date/time to the UTC timezone.
+ *
+ * @return ComponentInterface
+ */
+ public function toUtc();
/**
* @return TimeZone The time zone of the time point.
View
42 lib/Icecave/Chrono/Date.php
@@ -64,6 +64,46 @@ public function day()
}
/**
+ * Convert this time to a different timezone.
+ *
+ * Note that this method returns a {@see DateTime} instance, and not a {@see Date}.
+ *
+ * @param TimeZone $timeZone The target timezone
+ *
+ * @return DateTime
+ */
+ public function toTimeZone(TimeZone $timeZone)
+ {
+ $this->typeCheck->toTimeZone(func_get_args());
+
+ $seconds = $timeZone->offset()
+ - $this->timeZone()->offset();
+
+ return new DateTime(
+ $this->year(),
+ $this->month(),
+ $this->day(),
+ 0,
+ 0,
+ $seconds
+ );
+ }
+
+ /**
+ * Convert this time to the UTC timezone.
+ *
+ * Note that this method returns a {@see DateTime} instance, and not a {@see Date}.
+ *
+ * @return DateTime
+ */
+ public function toUtc()
+ {
+ $this->typeCheck->toUtc(func_get_args());
+
+ return $this->toTimeZone(new TimeZone);
+ }
+
+ /**
* @return TimeZone The time zone of the time.
*/
public function timeZone()
@@ -111,7 +151,7 @@ public function unixTime()
$this->month(),
$this->day(),
$this->year()
- );
+ ) - $this->timeZone()->offset();
}
/**
View
30 lib/Icecave/Chrono/Time.php
@@ -60,6 +60,36 @@ public function seconds()
}
/**
+ * Convert this time to a different timezone.
+ *
+ * @param TimeZone $timeZone The target timezone
+ *
+ * @return Time
+ */
+ public function toTimeZone(TimeZone $timeZone)
+ {
+ $this->typeCheck->toTimeZone(func_get_args());
+
+ $seconds = $this->seconds()
+ + $timeZone->offset()
+ - $this->timeZone()->offset();
+
+ return new Time($this->hours(), $this->minutes(), $seconds);
+ }
+
+ /**
+ * Convert this time to the UTC timezone.
+ *
+ * @return Time
+ */
+ public function toUtc()
+ {
+ $this->typeCheck->toUtc(func_get_args());
+
+ return $this->toTimeZone(new TimeZone);
+ }
+
+ /**
* @return TimeZone The time zone of the time.
*/
public function timeZone()
View
8 test/suite/Icecave/Chrono/DateTest.php
@@ -70,6 +70,14 @@ public function testUnixTime()
$this->assertSame(1359676800, $this->_date->unixTime());
}
+ public function testUnixTimeWithTimeZone()
+ {
+ $timeZone = new TimeZone(36000, true);
+ $date = new Date(2013, 2, 1, $timeZone);
+
+ $this->assertSame(1359640800, $date->unixTime());
+ }
+
public function testIsoString()
{
$this->assertEquals('2013-02-01', $this->_date->isoString());

0 comments on commit c54b421

Please sign in to comment.
Something went wrong with that request. Please try again.