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!

Need to count something using TECHNOLOGY?

Now you can! Thanks to my new Web 2.0 HTML5 Web App, you can count anything you want! Any time!

Check it out here!:

Countmaster 3000

But wait! There’s more! The count is saved on your computer, using delicious, delicious cookies, so you can come back to counting anytime! And remember…

With the Countmaster™ counting system, It’s your count that counts!™

Some restrictions apply. Any time only includes time spent online. Times not at your computer not included in calculation. Your results may vary. No purchase necessary.

Getting rid of the ugly “pause” icon in Parallels 6

Parallels Logo

Yes, that is a logo.

Recently, I installed Parallels 6 on my mac so I could run OneNote. (Why won’t you release a native port already, Microsoft?? It runs pretty well — in fact, with “Coherence” mode I can even make Windows apps run as if they were real Mac apps, complete with a Dock icon, drop shadows, and Exposé support!

However, there was one visual thing that constantly bothered me: every dock icon had what appeared to be a red pause icon. After some internet searching, I found that it was, in fact, not a pause icon at all, but the Parallels logo!

Luckily, it turns out to be pretty easy to remove!

  1. First: In “Configure…” under the “Virtual Machine” menu, go to your virtual machine options, and disable “share windows applications with mac”.
  2. Quit Parallels
  3. Open Finder, and navigate to the Applications folder on your hard drive.
  4. Find the Parallels Desktop icon, right click, and select “Show package contents”
  5. Once there, under the Contents/Resources folder, select the following files:
    • SharedAppDocumentIcon.icns
    • SharedAppIconMask_128.png
    • SharedAppIconOverlay.icns
  6. Move those files to somewhere as a backup.
  7. Now, download this set of replacement icons that I’ve created.
  8. Unzip those files and copy them to that Contents/Resources from before.
  9. Finally, start Parallels again, go back to the virtual machine configuration, and re-enable the “share windows applications with mac” checkbox.

And congratulations! You’ve just removed Parallels’s horrifying abomination of a logo from your Windows programs and file associations. The best part is, they probably paid a graphic design firm thousands of dollars for that logo!

But enough rambling — go enjoy running Windows programs on your mac!

Firesheep and the Advent of Usable Hacking

So apparently this is old news for some, but I only recently learned about Firesheep in a web security class. If you’re unfamiliar with the program, it’s a Firefox extension that allows you to steal the accounts (on many popular websites) of other users on your network! Of course, it only works if the network and website are unencrypted — in fact, the program’s primary purpose is ostensibly to raise awareness of the dangers of unencrypted network communication.

To use it, all you need to do is install it into Firefox. You then have the option of opening the Firesheep sidebar. Once the “Start Capturing” button is clicked, anyone else on your network who is accessing Facebook, Twitter, Yahoo! Mail, or any number of other sites will appear in the sidebar. From there, just double click on that entry, and congratulations, you’re now that person!

Even a child could do it!

Watch as I magically become the administrator of this blog! Oh, wait...

And yes, it’s that simple. Since it works best on unencrypted wireless networks, you could wreak havoc pretty easily if you felt like it.

So how does it work? When your computer is connected to a network, a lot of information reaches your computer that isn’t meant for you, but for other people on the network. This is especially common in wireless networks, since everyone’s data is flying everywhere. Because of this, it’s possible to simply “listen in” on the information hitting your computer that isn’t targeted at you, thereby grabbing raw data “packets” from other people.

However, what does one do with the packets? Several programs (such as WireShark) exist that allow you to graphically view the packets, but if you wanted to, say, log onto someone else’s Facebook account, you’d have to do a lot of extra work.

See, websites like Facebook are encrypted when you log on (to protect your password), but once you’re logged in, encryption is no longer enabled, and your identity is verified by a random number that Facebook hands your web browser each time you log in. In theory, no one else knows the number, so I can’t just say I’m you and defriend everyone. However, with packet sniffing, I can figure out the number, thus tricking Facebook into thinking that my computer is your computer.

The magic of Firesheep is that it automates this entire process, so I can just click and go, without poking through any lists packets and analyzing them. Perhaps I’m wrong, but this strikes me as the very first example of usable hacking — a hacking tool so easy to use that anyone who knows how to browse the internet could use it.

Why are modern watches exactly the same as they were in the 90s?

A Casio watch

This is a current top of the line "data" watch from Casio. It has room for 50 contacts, with 63 characters each. Astounding.

So, I was in the store the other day, and I noticed that all of the digital watches were nearly identical to the digital watches that were in the store when I was a kid!  There’s nearly no change!  Despite advances of inexpensive color screens, and small cameras, and all those other things on cellphones, watches still have segmented, black and white LCD screens, little buttons on the side, and a beeper.  Seriously, it’s 2010!

But, while researching this dearth of watches which fail to incorporate many of the advances in mobile tech made during the 21st century, I discovered the truth.  Such watches do, in fact exist.  But you won’t find them here.  Want to guess where you’ll find them?  Here’s a hint: what country manufactures every electronic device in existence?  That’s right!  China!

Quite a few unknown brands in China apparently build and sell…wait for it…cellphone watches.  And they are exactly what you think they are. GSM, touch screens, voice recorders, FM radios, cameras…the future is now!  And yet, no one in the US cares because no one wears watches anymore (because they were, fittingly enough, replaced by cellphones!)  (And I suppose there’s the fact that you’d look really weird talking into a watch.)

A watch phone

THE FUTURE IS NOW

Seven Two Off-Suit

I am proud to announce the second spoken word cover in my upcoming “album”! (see the previous post for details)

By request, I’ve covered Pokerface by Lady Gaga!  (As an interesting side note, prior to her Lady Gaga days, she was a singer/songwriter.  Quite an interesting musical turnaround.)  Furthermore, I’m not the first to do a spoken word version of the song; Christopher Walken did a reading of part of the song, which is also definitely worth a listen.

Anyway, without further historical context, here it is, complete with an orchestral backing track!

Seven Two Off-Suit [Pokerface by Lady Gaga]

Heated, then Cooled

So, have you ever listened to William Shatner’s spoken-word interpretations of various songs?  If you haven’t, I highly recommend checking out his cover of Common People, originally by Pulp.  Don’t even bother listening to the original; it’s got nothing on Shatner’s.  No, really.

Many of today’s pop songs, however, have not been (and probably never will be) covered by Shatner.  As such, there is a decided lack of spoken-word interpretations of modern pop music.  However…that’s all about to change.

It is with this in mind that I would like to present, for your review, this preview track from my upcoming “album” Transformed Pop.  Here we have a version of Katy Perry’s “Hot N Cold”, as interpreted by me.  Beethoven must be spinning in his grave.

Heated, then Cooled [click arrow to play]

1:40am Style Updated Yet Again!

The custom style sheet for the K2 WordPress theme used at this site, 1:40am, has been updated once again, this time to add compatibility with K2 1.0.  Admittedly, K2 1.0 isn’t as good looking as it was in the RC days, I think, but nonetheless upgrading is necessary…FOR PROGRESS!

Download the new version here.