<?php

$daysWarning 
5;

$feedHead = <<<EOXML
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Daylight savings notifications for %1\$s</title>
    <link>http://lab.brainonfire.net/feed/DST/generate.php</link>
    <description>$daysWarning days before your time zone's switch to or from daylight savings, a notice will appear in this feed.</description>

    <language>en</language>

    <atom:link href="http://lab.brainonfire.net/feed/DST/generate.php?zoneID=%2\$s" rel="self" type="application/rss+xml" />

EOXML;

$feedBody = <<<EOXML
    <item>
      <title>Switch to %2\$s on %3\$s</title>
      <link>http://lab.brainonfire.net/feed/DST/generate.php</link>
      <description>The timezone %1\$s switches to %2\$s on %3\$s. Remember to set your clock %4\$s by %5\$s!</description>
      <pubDate>%6\$s</pubDate>
      <guid isPermaLink="false">dsttransition:%1\$s=%3\$s</guid>
    </item>
EOXML;

$feedFoot = <<<EOXML
  </channel>
</rss>
EOXML;

function 
printBody($zoneID$trans$tz) {
  global 
$feedBody;
  
  
$oneSecBefore $trans['ts'] - 1;

  
$change = new DateTime("@{$trans['ts']}");
  
$change->setTimezone($tz);
  
$before = new DateTime("@{$oneSecBefore}");
  
$before->setTimezone($tz);
    
  
$minutesMove $change->getOffset() - $before->getOffset();
  
  
$direction = ($minutesMove "back" "forwards");
  
$minutesMove abs($minutesMove);
  
  
$hours floor($minutesMove/3600);
  
$minutes fmod($minutesMove3600);
  
  
$newTimeName = ($trans['isdst'] ? "daylight saving time" "standard time");
  
$dateStampSwitch $before->format('d M Y @ H:i T');
  
  
$increments = array();
  if(
$hours 0) {
    
$increments[] = sprintf('%d hour%s'$hours, ($hours == '' 's'));
  }
  if(
$minutes 0) {
    
$increments[] = sprintf('%d minute%s'$minutes, ($minutes == '' 's'));
  }
  
  
printf($feedBody,
         
htmlspecialchars($zoneID),
         
htmlspecialchars($newTimeName),
         
htmlspecialchars($dateStampSwitch),
         
htmlspecialchars($direction),
         
htmlspecialchars(implode(' and '$increments)),
         
htmlspecialchars(date('r'$oneSecBefore))
  );
}

function 
makeFeed($zoneID) {
  global 
$daysWarning$feedHead$feedFoot;
  
  
$tz = new DateTimeZone($zoneID);
  
  
$now = isset($_GET['now']) ? strtotime($_GET['now']) : false;
  if (!
$now) {
    
$now time();
  }
  
  foreach(
timezone_transitions_get($tz) as $trans) {
    if(
$trans['ts'] > $now) {
      break;
    }
  }
  
  
header('Content-Type: application/rss+xml; charset=UTF-8');
  
header('Cache-Control: public');
  
header('Expires: '.date('r'time() + 24 60 60));
  
  
printf($feedHeadhtmlspecialchars($zoneID), urlencode($zoneID));
  
  
$timestamp $trans['ts'];
  if(
$timestamp $now + ($daysWarning 24 60 60)) {
    
printBody($zoneID$trans$tz);
  }
  
  print(
$feedFoot);
}

function 
zoneToHTMLOption($name) {
  return 
sprintf('<option value="%1$s">%1$s</option>'htmlspecialchars($name));
}

function 
zoneIsAcceptable($name) {
  return 
strpos($name'/') !== false and
         
strpos($name'Etc/') !== and
         
strpos($name'SystemV/') !== 0;
}

function 
makeWizard() {
  
$zones DateTimeZone::listIdentifiers();
  
$zones array_filter($zoneszoneIsAcceptable);
  
sort($zones);
  
$sh_zoneOpts implode("\n\t\t\t\t   "array_map('zoneToHTMLOption'$zones));
  
  
?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  
      <title>Get a Daylight Saving Time feed!</title>
    </head>
    <body>
      <p>Be notified of when Daylight Saving Time is about to start or end in your
         region! Choose your location (if available) and subscribe to the feed
         (please only update daily, at most.) A notification will appear in the
         week before your region's switch.</p>

      <form method="GET" action="generate.php">
        <label for="zoneID">Region</label>
        <select name="zoneID" id="zoneID">
           <?php echo $sh_zoneOpts?>
        </select>    

        <button>Generate feed</button>
      </form>
  
      <p>Source code here: <a href="generate.phps">feed.phps</a></p>
    </body>
    </html>
  <?php
}

if(isset(
$_GET['zoneID'])) {
  
makeFeed($_GET['zoneID']);
} else {
  
makeWizard();
}

?>