Entries Tagged as 'ramblings'

Wednesday, February 11th, 2009

PHP’s “strtotime” vs JAVA’s … nothing

PHP’s “strtotime” function is one of the coolest core functions of the language.  I never really noticed how great it was until today.

At my company, we import date formats from customers and need to store them in MySQL format. (’Y-m-d h:m:s’).  In PHP, you can pass strtotime just about anything and — with the help of the date function — convert that wiley timestamp to what you want.

Consider the following time formats:

  • 2004-02-12T15:19:21+00:00 (ISO 8601)
  • Thu, 21 Dec 2000 16:01:07 +0200 (RFC 2822)
  • Monday, January 1st
  • tomorrow
  • -1 week 2 days 4 hours 2 seconds

Let’s convert these to MySQL timestamps with PHP:

echo date(’Y-m-d h:m:s’, strtotime(’2004-02-12T15:19:21+00:00′));
echo date(’Y-m-d h:m:s’, strtotime(’Thu, 21 Dec 2000 16:01:07 +0200′));
echo date(’Y-m-d h:m:s’, strtotime(’Monday, January 1st’));
echo date(’Y-m-d h:m:s’, strtotime(’tomorrow’));
echo date(’Y-m-d h:m:s’, strtotime(’-1 week 2 days 4 hours 2 seconds’));

This automagically outputs:

2004-02-12 07:02:21
2000-12-21 06:12:07
2009-01-01 12:01:00
2009-02-12 12:02:00
2009-02-06 09:02:41

I remember the first time I can across strtotime and thought “wow - it will take in just about any format.”  Clearly, there are some limits (I can’t enter “orange” and get a date back) — but needless to say, the function is simple and powerful.

Back to today at work.

A developer (we’ve recently moved to being an all-Java shop) tells me he has to write a custom date parser for ingesting and converting partner’s date formats.  And every time we see a new date format, he has to add to his parser (then we have to build it, QA it, release it).

Poppy cock, I say!  Isn’t there something in Java like strtotime?  Turns out?  No.

I did my googling, nothing.  Then I turned to Twitter.  Watch as we go from “But you *can* do that in Java!” to “oh, well, you’d have to be expecting a certain date format” quicker than you can say “ISO 8601.”

  • Java Friend #1: “you can do that simply: String time = “12:31:24″; DateFormat sdf = new SimpleDateFormat(”hh:mm:ss”); String result = sdf.parse(time).toString();”
  • Java Friend #2: “Won’t SimpleDateFormat.parse do what you need?”
  • Me, to both: “Thanks for the suggestions. All the sample code I see is massive compared to the one-liner “strtotime.” Sigh. Ah well.”"
  • Java Friend #1 replies : “a one liner, but not as short as strtotime: System.out.println(new SimpleDateFormat(”EEE, dd MMM yyyy HH:mm:ss z”).parse(t));”"
  • I direct message #1: “Will SimpleDateFormat accept multiple input date formats: ISO 8601, RFC 2822, MySQL Timestamp/Datestamp, etc.?”
  • His reply:  “you need to know the date format coming into SimpleDateTime. What format is it?”
  • Me: “There’s the rub. Could be ISO 8601, RFC 2822, MySQL Timestamp/Datestamp, etc. Was hoping to not have to write for each possible…”
  • Him: “you’ll need to do something about the type of date strings you operating on to make an error free conversion”

<slaps forehead>

And there’s the problem — I don’t want to have to build in a switch for every known date format.  If partner XYZ wants to enter a non-standard format (”-1 week 2 days 4 hours 2 seconds”?), we shouldn’t need to refactor the code to parse this.

Now.  A disclaimer.  I am not a JAVA programmer.  I consider myself an intermediate PHP programmer.  Programming is not my job nor is it my dream to become a full-time developer.

However, I’ve done my google…and I’ve asked 4 different JAVA experts who all agree it can’t be done as cleanly as strototime.

That being said?  Booooo to you Java.  Boooooo.

Tags: > > > > >

Friday, January 16th, 2009

The Thin Line of Instant Messenger

In person, it’s quite easy to know who you’re talking to.  You’re at lunch or at a bar, and the person you’re looking at is the person you’re talking to.  Granted, there may be someone eavesdropping or the person you’re talking to may be calling the police because they have a restraining order against you … but the point is this: in order to change conversations, you must do something physically with your body: turn to the left or right, walk to another table, etc.

Additionally, if you pause a conversation, you don’t usually forget who you’re talking to when you’d like to restart communication.  Your eyes see who’s in front of you and they tell your brain, which usually (unless incredibly intoxicated) will deduce which person you’re conversing with.

The same cannot be said about instant messenger.  For me, sometimes, there is a very thin line between conversing between your friend and your boss.  And depending on which window your clumsy mouse has chosen to select, you may get a very different response.

Case in point:

capture

Tags: >

Friday, January 9th, 2009

Protest Fail

Ooo!  A map of Prop 8 donors! Go kick the crap out of them for participating in the electoral process.

Look, I hate discrimination as much as the next guy, but threatening violence in this situation serves no purpose.

Afterwards, go destroy a random business because someone you don’t know was murdered.

Tags: > >

Tuesday, January 6th, 2009

Today’s Tunes

What I’m listening to today at work — kind of all over the place, but it’s getting me through:

Tuesday, January 6th, 2009

Monday Update (on a Tuesday)

The Weekend that was: I moved!  What the crap!?  I haven’t lived this far from family in a long time.  We moved out Friday, drove up to Oakland, then unloaded Saturday.  Lots of stairs in our new place, so my legs are still pretty sore.

Where I am at the moment: At work in our San Francisco office.  Today was my first attempt at my new commute - driving to BART and riding into the city.  Pretty uneventful.  It’ll be nice once I find my iPod headphones so I can zone out on the train.

On my To Do list this week: Lots of stuff to do at work.  Now that I’m in this office full-time, I can actually stay on top of things better.  Hopefully, I can get my little office space setup soon.  At home, there are lots of things on my “honey-do” list.

Procrastinating about: Yard work.  Yup, I have a yard to take care of now.  Low on the priority list at this point, but it’d be nice to have a clean yard.

Music that seems to catch my attention this past week: MGMT.  Purchased Fleet Foxes, but haven’t had time to listen to it.  Listening to Last.fm at work right now.

Next Trip: I’d be happy if I didn’t take any sort of trip for at least a month.

How I’m feeling about this week:
Surreal

Friday, September 19th, 2008

I’m a PC. And I’m miserable.

i’m a PC - and i do boring, ordinary, every day things.  please make me an operating system that works like I do.  make it boring, ordinary, and lacking in innovation. oh, and let is SHRED my hard drive on startup, too…i LOVE that. oh, i almost forgot! could you make it somewhat stable until about 3 months of heavy usage — then let all the applications start colliding in registry hell. oh and let it take 5-10 minutes to boot up then, too. that’ll give me time to drink the crappy office coffee before i can actually do any work

Tags: > >