Categories
Computers

Question About Scheduling Posts

Alright, I’m going to write this down in the hopes that someone stumbles across this stuff with some kind of explanation.

I’ve been working on support for scheduling posts in my blog client. I thought I had something working- I used it to schedule 2 posts for the minute before and then right at midnight on New Years. Subsequent attempts to use the scheduling did not work. So I started searching for more details on what the date/time format needed to look like.

Here’s the original code snippet that handled scheduling posts:

timeStruct = time.strptime(timestamp, '%Y%m%d%h%m%S')
utctime = time.gmtime(time.mktime(timeStruct)
postStruct['dateCreated'] = xmlrpclib.DateTime(utctime)

Now, using this piece of code I could schedule a post and then check on it through the WP browser administration interface. The post would be scheduled for the proper time. But it would not get published.

So I found something on the ISO 8601 specification that XMLRPC requires. The first thing I noticed is that the date requires dashes between each element. Also, in addition to the ‘T’ separating date from time, there is a required timezone field. This field needs to be a ‘Z’ when using UTC time or a field specifiying hours and minutes before or after UTC time.

I checked the output of the xmlrpclib.DateTime function and found that it was missing the ‘Z’ designator and there were no dashes in the time string either. Looking at the python documentation, it’s supposed to be putting the date into ISO 8601 format. So this didn’t make any sense. I then went and looked at the xmlrpclib module. Apparently, all the function really does (aside from some simple conversion based on the input) is turns the field into an XML ‘datetime’ field as opposed to a ‘string’ field.

Seems very misleading to me. the ISO 8601 spec is specific about its format. To claim that the function was creating the proper format, when in fact it did not, was confusing. So to rectify the situation, I modified the code as follows:

timeStruct = time.strptime(timestamp, '%Y%m%d%H%M%S')
utctime = time.gmtime(time.mktime(timeStruct)
posttime = time.strftime('%Y-%m-%dT%H:%M:%SZ', utctime)
postStruct['dateCreated'] = xmlrpclib.DateTime(posttime)

Naturally, still no success. I found some code samples showing that WP XMLRPC didn’t use the dashes in the time. So I removed the dashes and tried again. That didn’t work either.

Basically fed up, I wrote one more test post and scheduled it for about 30 minutes into the future and walked away. My previous attempts were in the 5 to 10 minute future range because I wanted to see a result in a reasonable amount of time. This one worked.

Along the way, I also discovered that WP supports a date_created_gmt field which is for plunking in a UTC time value. I tried using that field as well and that seemed to work for post scheduling.

So at this point, my questions are:

  • Why does WP specify ISO 8601, when in fact it uses a bastardized form of it?
  • Along the same lines, why doesn’t using a properly formed IS 8601 field work? (it didn’t work with the dashes)
  • Why does python documentation say that it creates an ISO 8601 datetime field when in fact it does not according to the spec?
  • Is the date_created_gmt field portable, or is it WP specific?
  • Why are you still reading this?

Anyway, if anyone with know-how happens across this, I’d appreciate some clarification. Unfortunately, I don’t have much to offer in return other than a ‘Thank You.’

Leave a Reply

Your email address will not be published. Required fields are marked *