开发者

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 th开发者_如何学JAVAem 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..)


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


I'm guessing a javascript lib will work, since both AS and JS languages are from the same ECMA standard, with a few minor differences..

http://www.datejs.com/ does what you want in JS.


The standard *nix application date(1) is very good at parsing such specs, as you can see below (shell scripting).

$ date; date -d '1 week 2 days 2 hours'
Fri Feb  4 16:31:28 AMT 2011
Sun Feb 13 18:31:28 AMT 2011

Maybe you can take a look at the GNU coreutils source code for further enlightenment.


I think that if you're going to do it in AS3 the best option (as I see in the other answers) is to use datejs, porting JavaScript code to AS3 is not hard, and also you have the advantage of using ExternallInterface if you don't want to port it.

Remember that ActionScript is indeed a JavaScript clone so you can paste the code of datejs and change the little details of the syntax, but mantain the core intact


See Is there any python library for parsing dates and times from a natural language?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜