10

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..)

Assaf Lavie
  • 73,079
  • 34
  • 148
  • 203
  • fyi, found this: https://github.com/gitpan/Time-Duration-Parse/blob/master/lib/Time/Duration/Parse.pm Although it doesn't seem very robust from first glance – Assaf Lavie Jan 12 '11 at 15:21
  • Something like http://stackoverflow.com/questions/3451031/is-there-a-javascript-parser-like-date-js-for-time-estimates ? – Stobor Jan 31 '11 at 05:20
  • Yes, thanks. although the example code posted there is pretty simplistic. I was looking for something a bit more robust.. otherwise doing it myself isn't that much more work. – Assaf Lavie Jan 31 '11 at 11:25
  • Hi Assaf. Must be AS or Python. I know one project in pearl – Frank Feb 01 '11 at 08:36
  • found anything useful? – Dan Aug 31 '13 at 22:43

5 Answers5

1

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

Neeraj
  • 8,408
  • 8
  • 41
  • 69
  • I don't see anything in datejs's docs that suggest it can parse _durations_.. it parses dates. That doesn't really help me. – Assaf Lavie Feb 02 '11 at 17:55
  • http://code.google.com/p/datejs/wiki/APIDocumentation API reference for datejs What you can do is Date.today().months(2) - Date.today() returs number of milliseconds between today and the same date two months later. – Neeraj Feb 03 '11 at 09:10
  • Similarly, it will return things for Date.today().hours(2) Date.today.minutes(2) etc. – Neeraj Feb 03 '11 at 09:14
  • It can even go with things like Date.parse("2 hours") - Date.parse("now") which gives number of milliseconds between now and two hours later. – Neeraj Feb 03 '11 at 09:15
  • I see. So it assumes that any duration the user inputs is actually ".. from now"? If so it could work. – Assaf Lavie Feb 03 '11 at 10:47
  • yes something like Date.parse("2 h") returs the time two hours from now, and subtracting one date object from another returns the number of milliseconds between the two dates.You should read the API for more details and cool features. – Neeraj Feb 03 '11 at 10:50
  • evaluating it and kind of disappointed. it doens't understand that "60m" or "60 m" means 60 minutes. "70 seconds" yields some date in 1970... meh.. – Assaf Lavie May 25 '11 at 12:45
0

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.

macarthy
  • 3,074
  • 2
  • 23
  • 24
0

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.

milton
  • 988
  • 6
  • 19
0

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

Lucas Gabriel Sánchez
  • 40,116
  • 20
  • 56
  • 83
0

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

Community
  • 1
  • 1