Permalink
Browse files

Merge remote-tracking branch 'dstockto/cs_timezone'

  • Loading branch information...
lornajane committed Jan 19, 2013
2 parents af3f4f1 + 6eed260 commit 43bf9372abcad4c0c677a706ff009f68a3f76373
Showing with 137 additions and 54 deletions.
  1. +137 −54 src/system/application/libraries/Timezone.php
@@ -1,59 +1,106 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
-
+<?php
+/**
+ * Timezone class
+ *
+ * PHP version 5
+ *
+ * @category Joind.in
+ * @package Configuration
+ * @copyright 2009 - 2012 Joind.in
+ * @license http://github.com/joindin/joind.in/blob/master/doc/LICENSE JoindIn
+ */
+if (!defined('BASEPATH')) {
+ exit('No direct script access allowed');
+}
/**
-* Class to work with data/time issues for events/talks
-*/
-class Timezone {
-
- private $CI = null;
-
- public function __construct() {
+ * Timezone class for date/timezone stuff on joind.in
+ *
+ * PHP version 5
+ *
+ * @category Joind.in
+ * @package Configuration
+ * @copyright 2009 - 2012 Joind.in
+ * @license http://github.com/joindin/joind.in/blob/master/doc/LICENSE JoindIn
+ */
+class Timezone
+{
+
+ protected $CI = null;
+
+ /**
+ * Instantiates class
+ */
+ public function __construct()
+ {
// Create an instance of our CI object
- $this->CI=&get_instance();
+ $this->CI = & get_instance();
}
-
- /**
- * Find the local time at the event...
- * @param $evt_offset integer Event's offset from UTC
- */
- private function _getEvtTime($evt_offset) {
- $here = new DateTimeZone(date_default_timezone_get());
- $hoffset= $here->getOffset(new DateTime("now", $here));
- $off = (time()-$hoffset)+($evt_offset*3600);
+
+ /**
+ * Find the local time at the event...
+ *
+ * @param integer $evt_offset Event's offset from UTC
+ *
+ * @return integer
+ */
+ private function _getEvtTime($evt_offset)
+ {
+ $here = new DateTimeZone(date_default_timezone_get());
+ $hoffset = $here->getOffset(new DateTime("now", $here));
+ $off = (time() - $hoffset) + ($evt_offset * 3600);
+
return $off;
}
- //----------------------------
-
+
/**
- * Determine if an event has started based on the start time(stamp)
- * and the timezone of the event
- * @param $eid integer Event ID
- * @param $edata[optional] array Event Data
- */
- public function hasEvtStarted($eid, $edata=null) {
- if (!$edata) {
- $this->CI->load->model('event_model','em');
- $edata=$this->CI->em->getEventDetail($eid);
+ * Determine if an event has started based on the start time(stamp)
+ * and the timezone of the event
+ *
+ * @param integer $eid Event ID
+ * @param array $edata [optional] Event Data
+ *
+ * @return boolean
+ */
+ public function hasEvtStarted($eid, $edata = null)
+ {
+ if (!$edata) {
+ $this->CI->load->model('event_model', 'em');
+ $edata = $this->CI->em->getEventDetail($eid);
}
- $at_event=$this->_getEvtTime($edata[0]->event_tz);
- return ($at_event>=$edata[0]->event_start) ? true : false;
+ $at_event = $this->_getEvtTime($edata[0]->event_tz);
+
+ return ($at_event >= $edata[0]->event_start) ? true : false;
}
-
+
/**
- * Determine if a talk for an event has started based on the time(stamp)
- * of the talk and the timezone of the event it belongs to
- * @param $tid integer Talk ID
- * @param $tdata[optional] array Talk Data
- */
- public function talkEvtStarted($tid, $tdata=null) {
- if (!$tdata) {
- $this->CI->load->model('talks_model','tm');
- $tdata=$this->CI->tm->getTalks($tid);
+ * Determine if a talk for an event has started based on the time(stamp)
+ * of the talk and the timezone of the event it belongs to
+ *
+ * @param integer $tid Talk ID
+ * @param array $tdata [optional] Talk Data
+ *
+ * @return boolean
+ */
+ public function talkEvtStarted($tid, $tdata = null)
+ {
+ if (!$tdata) {
+ $this->CI->load->model('talks_model', 'tm');
+ $tdata = $this->CI->tm->getTalks($tid);
}
+
return $this->hasEvtStarted($tdata[0]->event_id);
}
- public function getDatetimeFromUnixtime($unixtime, $timezone) {
+ /**
+ * Converts unix time to date time
+ *
+ * @param integer $unixtime Unix epoch time
+ * @param string $timezone Timezone
+ *
+ * @return DateTime
+ */
+ public function getDatetimeFromUnixtime($unixtime, $timezone)
+ {
$datetime = new DateTime("@$unixtime");
// if a timezone is specified, adjust times
@@ -64,7 +111,8 @@ public function getDatetimeFromUnixtime($unixtime, $timezone) {
}
$datetime->setTimezone($tz);
-/* Commented out as it appears to be causing a DST bug - see JOINDIN-169 in Jira
+ /* Commented out as it appears to be causing a DST bug
+ - see JOINDIN-169 in Jira
// How much wrong will ->format("U") be if I do it now, due to DST changes?
// Only needed until PHP Bug #51051 delivers a better method
$unix_offset1 = $tz->getOffset($datetime);
@@ -74,19 +122,54 @@ public function getDatetimeFromUnixtime($unixtime, $timezone) {
$timestamp = $unixtime + $unix_correction;
$datetime = new DateTime("@{$timestamp}");
$datetime->setTimezone($tz);
-*/
+ */
return $datetime;
}
- public function formattedEventDatetimeFromUnixtime($unixtime, $timezone, $format) {
+ /**
+ * Returns a formatted datetime string from a unix timestamp, timezone
+ * and format specification
+ *
+ * @param integer $unixtime Unix timestamp
+ * @param string $timezone Timezone
+ * @param string $format Time format
+ *
+ * @return string
+ */
+ public function formattedEventDatetimeFromUnixtime(
+ $unixtime,
+ $timezone,
+ $format
+ ) {
$datetime = $this->getDatetimeFromUnixtime($unixtime, $timezone);
- $retval = $datetime->format($format);
+ $retval = $datetime->format($format);
+
return $retval;
}
- public function UnixtimeForTimeInTimezone($timezone, $year, $month, $day, $hour, $minute, $second) {
-
+ /**
+ * Gets a unix timestamp for a time in a given timezone
+ *
+ * @param string $timezone Timezone
+ * @param integer $year Year
+ * @param integer $month Month
+ * @param integer $day Day
+ * @param integer $hour Hour
+ * @param integer $minute Minute
+ * @param integer $second Second
+ *
+ * @return string
+ */
+ public function UnixtimeForTimeInTimezone(
+ $timezone,
+ $year,
+ $month,
+ $day,
+ $hour,
+ $minute,
+ $second
+ ) {
$tz = new DateTimeZone($timezone);
// Get offset unix timestamp for start of event
@@ -95,23 +178,23 @@ public function UnixtimeForTimeInTimezone($timezone, $year, $month, $day, $hour,
$dateObj->setDate($year, $month, $day);
$dateObj->setTime($hour, $minute, $second);
- if (! isset($unix_correction)) {
+ if (!isset($unix_correction)) {
$unix_correction = 0;
}
$unixTimestamp = $dateObj->format("U") - $unix_correction;
-/* Commented out as it appears to be causing a DST bug - see JOINDIN-169 in Jira
+ /* Commented out as it appears to be causing a DST bug -
+ //see JOINDIN-169 in Jira
// How much wrong will ->format("U") be if I do it now, due to DST changes?
// Only needed until PHP Bug #51051 delivers a better method
$unix_offset1 = $tz->getOffset($dateObj);
$unix_offset2 = $tz->getOffset(new DateTime());
$unix_correction = $unix_offset1 - $unix_offset2;
$unixTimestamp = $unix_timestamp - $unix_correction;
-*/
-
+ */
return $unixTimestamp;
}
-
+
}

0 comments on commit 43bf937

Please sign in to comment.