and I'm all out of bubble gum…

This is really just a quick hack: all it does is insert the correct timezone description in the header of an iCalendar feed. But if the server that is generating the iCalendar feed doesn’t do it, someone has to. The script generates a URL that can then be subscribed to by your Calendar reader of choice. (I’m running this on my server and using it daily to good effect, but decline to share bandwidth with the world for this one):

$filename = "calendar";
if (isset($_GET["url"]))
	$url = $_GET["url"];
	if (!isset($_GET["show_url"]))
		preg_match("|.+\/([^?]+)\??|", $url, $matches);
		if (isset ($matches[1]))
			$filename = $matches[1];
		$calendar = file_get_contents ($url);
		if ($calendar)
			//$output = preg_replace_callback ("/(DATE-TIME:)(\d{4,4})(\d{2,2})(\d{2,2})T(\d{2,2})(\d{2,2})(\d{2,2})[^Z]/", "adjustTimeZone", $calendar);
			$timezone = "X-WR-TIMEZONE:America/Los_Angeles\n" .
				"TZID:America/Los_Angeles\n" .
				"TZOFFSETFROM:-0800\n" .
				"DTSTART:20070311T020000\n" .
				"TZNAME:PDT\n" .
				"TZOFFSETTO:-0700\n" .
				"END:DAYLIGHT\n" .
				"TZOFFSETFROM:-0700\n" .
				"DTSTART:20071104T020000\n" .
				"TZNAME:PST\n" .
				"TZOFFSETTO:-0800\n" .
				"END:STANDARD\n" .
			$loc = strpos($calendar, "BEGIN:VEVENT");
			$output = substr($calendar, 0, $loc) . $timezone . substr($calendar, $loc, strlen($calendar));
		    header("Content-Type: text/calendar");
		    header("Content-Disposition: inline; filename=$filename-pacific-timezone.ics");
			echo $output;
<h1>vCalendar Time Zone timezone</h1>
	<p>This is quick script to "de-float" calendars in the vCalendar format which do not specify time zones for their events. This script will automatically add the Pacific time zone information to the calendar at the URL entered below. Copy-and-paste the resulting URL below into your calendar reader of choice. <a href="">The source of this script is freely available.</a></p>
	<form action="<?= $_SERVER["PHP_SELF"] ?>" method="get">
		<input type="hidden" name="show_url" value="" />
		<p>Calendar URL <input name="url" type="text" value="<?= $url ?/>" /></p>
		< ?php
				$newUrl = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"] . "?url=" . urlencode($url);
				echo "<p><a href=\"$newUrl\">$newUrl</a>";
		<p><input type="submit" value="Generate"/></p>

February 14th, 2011

Posted In: How To

Tags: , , ,