Skip to content

Commit

Permalink
#4 : support for merging different timezones + test suites
Browse files Browse the repository at this point in the history
(cherry picked from commit cbffe0b)
  • Loading branch information
zyzo authored and andibraeu committed Jul 18, 2015
1 parent a860eec commit 2636d51
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ics-collector/debugger/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function generateDataOptions() {
</select>
</div>
<textarea id="icstext" placeholder="Copy your ics / iCal text here" class="largeArea"></textarea>
<button id="importConfirmBtn" onclick="sendParseRequest();">Parse me !</button>
<button id="importConfirmBtn" onclick="sendParseRequest();">Feed me !</button>
</div>
<div id="mergePanel" class="hidden">
<textarea id="mergedIcsText" class="hidden" disabled></textarea>
Expand Down
26 changes: 24 additions & 2 deletions ics-collector/debugger/lib/class.iCalReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* UPDATE NOTES : @zyzo (htto://www.github.com/zyzo)
* - Add recurrence event counter
* - Add option disable recurrent events
* - Return more details for DATE/DATE-TIME property
*
*/

Expand Down Expand Up @@ -158,8 +159,10 @@ public function initLines($lines)
*/
public function addCalendarComponentWithKeyAndValue($component, $keyword, $value)
{
$explodedKeyword = null;
if (strstr($keyword, ';')) {
// Ignore everything in keyword after a ; (things like Language, etc)
$explodedKeyword = explode(';', $keyword);
$keyword = substr($keyword, 0, strpos($keyword, ';'));
}
if ($keyword == false) {
Expand All @@ -178,8 +181,27 @@ public function addCalendarComponentWithKeyAndValue($component, $keyword, $value
}

if (stristr($keyword, 'DTSTART') or stristr($keyword, 'DTEND') or stristr($keyword, 'EXDATE')) {
$keyword = explode(';', $keyword);
$keyword = $keyword[0];
//$keyword = $keyword[0];
$meta = array();
if (strlen($value) === 8) {
// DATE format 19970714
$meta['type'] = 'DATE';
} else {
// DATE-TIME format
$meta['type'] = 'DATE-TIME';
if (strstr('T', $value)) {
// format 2 : DATE with UTC time 19980119T070000Z
$meta['format'] = 'UTC-TIME';
} else {
// format 1 : DATE with local time 19980118T230000
$meta['format'] = 'LOCAL-TIME';
if (count($explodedKeyword) === 2 && stristr($explodedKeyword[1], 'TZID=')) {
// timezone specification exists
$meta['tzid'] = explode('=', $explodedKeyword[1])[1];
}
}
}
$value = array('meta' => $meta, 'value' => $value);
}

switch ($component) {
Expand Down
2 changes: 1 addition & 1 deletion ics-collector/debugger/lib/ics-merger-config.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DEFAULT_PRODID = "-//FOSSASIA//FOSSASIA Calendar//EN"
DEFAULT_TIME_ZONE => Europe/Berlin
DEFAULT_TIME_ZONE = Europe/Berlin
41 changes: 38 additions & 3 deletions ics-collector/debugger/lib/ics-merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ class IcsMerger {

private $inputs = array();
private $config = array();
private $defaultTimezone;
const CONFIG_FILENAME = 'ics-merger-config.ini';

public function __construct() {
$this->config = parse_ini_file(IcsMerger::CONFIG_FILENAME);
$this->defaultTimezone = new DateTimeZone($this->config['DEFAULT_TIME_ZONE']);
}

public function add($text) {
Expand All @@ -19,19 +21,19 @@ public function getResult() {
$result = array(
'VCALENDAR' => array(),
'VEVENTS' => array()
);
);
foreach ($this->inputs as $icsText) {
$ical = new ICal(explode("\n", $icsText), true);
//var_dump($ical);
foreach($ical->cal as $key => $value) {
switch($key) {
case 'VCALENDAR' :
$result['VCALENDAR'] = array_merge($result['VCALENDAR'], $value);
break;
case 'VEVENT' :
$result['VEVENTS'] = array_merge($result['VEVENTS'], $value);
$result['VEVENTS'] = array_merge($result['VEVENTS'], $this->processEvents($value));
break;
default :
// ignore others
break;
}
}
Expand All @@ -47,6 +49,39 @@ public function getResult() {
return $result;
}

private function processEvents($events) {
foreach($events as &$event) {
//echo "$key hahdsad" . PHP_EOL;
foreach ($event as $key => &$value) {
switch($key) {
// properties of type DATE / DATE-TIME
case 'DTSTART':
case 'DTEND' :
case 'RDATE' :
// only local datime needs conversion
if ($value['meta']['type'] == 'DATE-TIME' && $value['meta']['format'] == 'LOCAL-TIME') {
if (array_key_exists('tzid', $value['meta'])) {
$tz = new DateTimeZone($value['meta']['tzid']);
try {
$time = new DateTime($value['value'], $tz);
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
$time->setTimezone($this->defaultTimezone);
$value['value'] = $time->format('Ymd\THis');
}
}
break;
default :
// ignore others
break;
}
}
}
return $events;
}

public static function getRawText($icsMergerResult) {

$callback = function ($v, $k) {
Expand Down
69 changes: 69 additions & 0 deletions ics-collector/debugger/tests/IcsMergerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
require_once(realpath(dirname(__FILE__)) . '/../lib/ics-merger.php');

class IcsMergerTest extends PHPUnit_Framework_TestCase {

private $merger;

/**
* @before
*/
public function setupMerger() {
$this->merger = new IcsMerger();
}

public function testWithTwoFile() {
$this->merger->add(file_get_contents('../data/freifunk_0'));
$this->merger->add(file_get_contents('../data/freifunk_3'));
$result = $this->merger->getResult();
$this->assertEquals(count($result['VEVENTS']), 3);
/*
echo '<pre>';
var_dump($result);
echo '</pre>';
echo IcsMerger::getRawText($result);
*/
}

public function testConversionLocalTimeWithTimezone() {
$this->merger->add
("BEGIN:VCALENDAR
BEGIN:VEVENT
DTSTART;TZID=Asia/Saigon:20150527T213000
SUMMARY:Event 1
END:VEVENT
END:VCALENDAR
");
$this->merger->add
("BEGIN:VCALENDAR
BEGIN:VEVENT
DTSTART;TZID=EST:20150527T213000
SUMMARY:Event 2
END:VEVENT
END:VCALENDAR
");
$this->assertEquals($this->merger->getResult()['VEVENTS']['0']['DTSTART']['value'], '20150527T163000');
$this->assertEquals($this->merger->getResult()['VEVENTS']['1']['DTSTART']['value'], '20150528T033000');
}

public function testConversionLocalTimeWithoutTimezone() {
$this->merger->add
("BEGIN:VCALENDAR
BEGIN:VEVENT
DTSTART:20150527T213000
SUMMARY:Event 1
END:VEVENT
END:VCALENDAR
");
$this->merger->add
("BEGIN:VCALENDAR
BEGIN:VEVENT
DTSTART;TZID=EST:20150527T213000
SUMMARY:Event 2
END:VEVENT
END:VCALENDAR
");
$this->assertEquals($this->merger->getResult()['VEVENTS']['0']['DTSTART']['value'], '20150527T163000');
$this->assertEquals($this->merger->getResult()['VEVENTS']['1']['DTSTART']['value'], '20150528T023000');
}
}
12 changes: 0 additions & 12 deletions ics-collector/debugger/tests/ics-merger-test.php

This file was deleted.

0 comments on commit 2636d51

Please sign in to comment.