Time Shortcode for Make P2s
A couple of weeks ago, Siobhan pointed out to me the difficulties in talking about times for IRC meetups on the various P2 blogs. She suggested a shortcode to make it easier to specify times.
So I wrote a first draft of a shortcode to make this a bit easier. It’s not perfect, but I figured to go ahead and tell people now, so that you all could go ahead and start banging on it and telling me what the problems are.
Usage: [time] some-time-format-here [/time]
By “some-time-format-here”, I’m being a bit generous. You can use pretty much any time format you can think of and it will attempt to figure it out. With extra emphasis on “attempt”.
Stuff like this all works:
- March 30, 2013 1pm UTC
- April 4th at 1pm
- April 4th around 1pm
- April 4th 2012 1pm
- next monday 1pm CDT
- 1pm UTC
Now, a few important points:
- If you don’t specify a timezone, you’ll probably get UTC. This is just for now, until I work out a better way.
- If it can’t figure it out, you’ll notice no change in your text. Hopefully.
- If it gets really confused, it’ll probably say January 1st, 1970.
- “Absolute” times are more likely to work than “relative” ones. Give a date, and year, and time, and timezone, and you’ll probably be good.
- Timezones matter in terms of daylight savings. GMT is not the same as BST (British Summer Time).
So, what’s the point? Well, if it can figure out what time you meant, then it’ll encode that and a bit of Javascript will then localize that time to the viewer of the make P2 in question on load, assuming their browser knows what timezone they’re in.
So when I post “April 3rd, 2013 6:30pm CDT” (my current time) in the time shortcode, it’ll show this instead: April 3rd, 2013 6:30pm CDT. What you see there depends on where you live.
Hopefully this will make it somewhat easier to schedule IRC meetups and such. Let me know when (not if) you break it. Happy to iterate.
Jerry Bates (JerrySarcastic) 11:37 pm on April 3, 2013 Permalink | Log in to Reply
Awesome! Thanks Otto!
GaryJ 1:23 am on April 4, 2013 Permalink | Log in to Reply
GMT isn’t scientifically defined, but glad to see a move to UTC output, the world standard for time since 1972…
Samuel Wood (Otto) 10:05 pm on April 4, 2013 Permalink | Log in to Reply
Well, if you prefer, it will happily understand tz-zoneinfo style timezones just fine too. I just figured writing out America/Chicago for a timezone was a bit overkill.
Paul 10:40 am on April 4, 2013 Permalink | Log in to Reply
you don’t know how helpful this is! thanks
Edward Caissie 1:27 pm on April 4, 2013 Permalink | Log in to Reply
… and where do we get a chance to look at the code directly? Sorting out the reader’s time is something that is haunting me in a plugin I have that uses dates extensively.
Being able to present different “content” based on the reader’s time would be extremely useful code to “borrow”
Samuel Wood (Otto) 1:33 pm on April 4, 2013 Permalink | Log in to Reply
Ain’t nothing magical to it. I’ll send you a copy if you want.
But basically, it first runs the time you give it through strtotime() with the post-date used as the “now” parameter, for handling of relative times. If that fails, then it uses date_parse() to try to figure out what time info you gave it, and the post-date to fill in the gaps. This lets it handle more types of relative times. Eventually, it gets an absolute time, hopefully. Then it returns the string with a time microformat wrapped around it, that looks like this: abbr class=”date” title=”2013-04-03T23:30:00+00:00″. It also returns it with the link to timeanddate.com.
Next, it inserts some javascript code into the footer of that page that searches for those abbr date items, parses that title into components, and uses the javascript Date object to convert that from UTC into the browser’s local time. The JS does the replacement of the string in the abbr into the localized time string. The plugin never needs to know the browser’s time, the JS code handles that.
Jen Mylo 1:10 am on April 20, 2013 Permalink | Log in to Reply
This sounds so scary to me after 5 years of dealing with time zones and travelers and proxies in the wordpress community. Will there be any indication that the time being displayed is a magic shortcode-determined time, or will it look like plain text? Maybe we could style it differently so it’s clear when it’s in use?
Samuel Wood (Otto) 2:39 am on April 20, 2013 Permalink | Log in to Reply
It is styled differently. Look at use of it in the post above. It’s got those dotted lines under it because of the abbr tag, it links to the timeanddate.com automatically.. When you hover over it, you’ll see the UTC version of the time. It’s fairly obvious, I’d say.
Also, it’s totally optional. If you don’t want to use it, don’t use the shortcode. I made it non-automatic for just this sort of reason. But I think it’s pretty robust, honestly. Try it out if you like. It works in comments too.
Ulrich 9:01 pm on April 22, 2013 Permalink | Log in to Reply
This time is being shown wrong in my timezone. I think it is to due with the daylight saving time. It should be 22:00.
http://pix.am/nk6c.png
http://make.wordpress.org/docs/2013/04/18/dont-forget-that-due-to-daylight-savings-the/
Samuel Wood (Otto) 9:07 pm on April 22, 2013 Permalink | Log in to Reply
No, it’s correct given what she wrote. She actually wrote the time in the shortcode as “18th April, 20:00 BST”. Thing is, “BST” (British Summer Time) is not the same as “UTC”.
It’s a quirk of people in England, I think. They always seem to call their time “UTC”, even when it’s not.
But it is converting correctly, given the time in the shortcode itself. Gotta put the right time in it to start with.
Samuel Wood (Otto) 9:15 pm on April 22, 2013 Permalink | Log in to Reply
A simpler way of dealing with timezones is simply to learn what “Zoneinfo” style timezone you’re in and use that all the time. Then it deals with daylight savings automatically.
I’m in the “America/Chicago” timezone. So I can do this:
Original: April 22, 2013 4:14pm America/Chicago
Shortcode: April 22, 2013 4:14pm America/Chicago
Much easier.