Monthly Archive for November, 2011

Calculating GMT Offset from Local Time (PHP)

For a project I’m working on, I needed to calculate local sunrise and sunset times for a user, based on a timestamp.  Because this was a “web application”, it was a mix between server-side and client-side programming.  As such, I had the client providing its local time to the server, which would then choose a suitable background image based on the user’s location.

Initially, I used the hour at face value — but once I started calculating sunrise and sunset times (using the php date_sunrise and date_sunset functions), I needed to specify a GMT offset.  I initially implemented it this way:

$gmt_offset = $hour - intval(date('H'));

However, I found that, while this worked for some situations, it came up with wrong answers if one of the times had rolled over into a different day.  After some experimentation, I found that, for GMT+ time zones, adding 24 to the value (only if the sum would not exceed 24) returned the correct offset.  Likeise, for GMT- time zones, subtracting 24 from the value (only if the difference would not be less than -23) would produce the correct offset.

// works for all GMT+ times
if($offset + 24 < 24) {
  $offset += 24;
}

// works for all GMT- times
if($offset - 24 > -24) {
  $offset -= 24;
}

Eventually, I realized that the program could tell whether we were looking for a GMT+ or GMT- time zone by examining the date and checking whether it was before or after the specified timestamp. (In effect, checking if the day differential was due to GMT being the next day or previous day.)  As such, we finally get this code, which works for all times and time zones:

$gmt_offset = intval(date('H', $timestamp)) - intval(date('H'));

//correct for anomalies that appear when it's the next or previous day in GMT
if(time() < $timestamp) { // then we're in GMT+
    if($gmt_offset + 24 < 24) {
      $gmt_offset += 24;
    }
  } else if(time() > $timestamp) { //then we're in GMT-
    if($gmt_offset - 24 > -24) {
      $gmt_offset -= 24;
    }
}

How to Stop iTunes from Opening Automatically When You Press the Play/Pause Key on your Mac

Picture showing media keys on a macbook pro

So, way back in 2009, I bought myself a Macbook Pro, mainly because I liked the all-metal case and backlit keyboard.  (Seriously, it’s surprisingly difficult to find a Windows laptop with a backlit keyboard — go on, try it.  It was even harder in 2009.)  Now, as you probably are aware, Apple loves iTunes, their music management application.  I, however, do not love iTunes, and much prefer crazy-customizable music-playing apps on Windows, like Foobar2000 and Winamp.  I tried installing Cog, the closest thing I could find to Foobar2000.  Now, I’m also a huge fan of the media keys (play/pause, next/prev track) on the keyboard, and so compatibility with those is a must.  While Cog was indeed compatiable with the media keys, I found that Apple had implemented a convenient “feature” in Mac OS 10.6 and newer– if you press play/pause when iTunes is not running, iTunes will start itself and start playing.  Needless to say, if you’re trying to use any other music player, including Spotify and VLC, this can be very irritating.

Luckily, someone managed to fix the issue!

  1. Download the fix from here – read the original forum post here [credit to this guy for finding it!]
  2. Open the DMG file
  3. Inside, run “MMFix” (I’ve tested it on Snow Leopard (Mac OS 10.6) and Lion (Mac OS 10.7), and it works correctly on both.)
  4. …and enjoy pressing the play/pause button on your keyboard without iTunes ruining your day!
Note that, if you want to undo this fix, simply run “MMFix” again, and it will restore things to the way they were originally.

Creating Google Chrome Web Application Shortcuts on the Mac

Screenshot of Google Chrome, showing grayed-out "create application shortcut" menu item

Why, Google?

Google Chrome, Google’s foray into the browser market, started on Windows, leaving Mac OS and Linux users wondering what happened to them.  Luckily, about a year later, the first beta versions of Chrome for Mac started to appear.  However, one feature that had been in the Windows version since the beginning was missing — the ability to create desktop shortcuts to open your favorite web applications in their own window.   Even more strangely, the “File” menu still contaned a menu item for “Create Application Shortcut…”, but it was grayed out.  One would think that this implied an eventual implementation of this feature…

Fast forward to the present day — about two years have passed, and Chrome for Mac OS has progressed from version “5.0″ (the first Mac version) to version “15″.  However, that menu item remains as grayed-out as it ever was!  This, of course, makes one wonder if Google has any plans to enable the feature.  Luckily, even if they don’t, there is now a way to create Chrome web application shortcuts on your mac!  These shortcuts act just like “real” applications, can run alongside Chrome without interference and are even compatible with Mission Control/Exposé and Spaces!

The Procedure

  1. Grab the tool from this page – the download link is where is says “(download the updated version from here)”
  2. Run the tool, specify the name and URL of the web app, and select a square PNG file as an icon
  3. The application will be created in your Applications folder.  Drag it to your dock, if you’d like.
  4. …and you’re done!

Google Chrome, running alongside apps for Remember the Milk, Gmail, and Google Calendar!