Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse durations in simple English

I would like to find an actionscript library that can take strings like:

  • Two days
  • 2h
  • one month
  • a week

and parse them into duration (returning the time in some unit).

It seems like it's been done so many times before and I'd hate to implement such a thing myself.

If not in actionscript then in python (I can run this on the server side I guess).

(please notice I'm looking for parsing, not formatting..)

like image 460
Assaf Lavie Avatar asked Jan 12 '11 09:01

Assaf Lavie


1 Answers

Just for the record, Javascript will not work in Flex directly.Im sure you know this.

Now datejs is a really cool library.What you should really do is, use the advantages of JS in AS using the externalInterface class.

Use the following code in AS3, and make sure to include datejs.js , in the html wrapper, in which this AS3 generated swf will be used.

//--------CODE IN AS-----------------//
public function returnDate(date:String):void
{
    Alert.show(date);
}

ExternalInterface.addCallback("returnDate",returnDate);

public function parseDate(userInputString:String):void
{
   ExternalInterface.call("parseStringByUser",userInputString);
}

//------------CODE IN JS----------------//
function parseStringByUser(userInputString)
{
     var parsedDate= Date.parse(userInputString);
     //the line below calls the AS3 function , as AS3 itself exposed it using the ExternalInterface.
         returnDate(parsedDate);
}

For externalInterface details you can view : http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html

like image 119
Neeraj Avatar answered Nov 10 '22 07:11

Neeraj