Question

I'm writing up a command line utility that needs to take a duration parameter. Now my challenge is that the duration can span anywhere from minutes to months.

I had considered using just seconds, but passing in large numbers of seconds to communicate 1 month seemed unwieldy.

Are there any generally accepted ways of passing in a duration with such a high range?

Was it helpful?

Solution

The gnu date command does a very good job of this.

http://www.gnu.org/software/coreutils/manual/html_node/Examples-of-date.html

There is a ruby library with C code that parses these kinds of time specifications.

https://github.com/bbense/ruby-getdate

This one also seems very interesting

https://github.com/mojombo/chronic

Another convention that I've seen in more than one program is to use a letter qualifier after the number to indicate units other than seconds.

2m ( 2 minutes ) 
2h ( 2 hours )  
2d ( 2 days )  
2M ( 2 months )

OTHER TIPS

'at' and 'find' both have similar options.

find uses two different options like -ctime and -cmin to take days and minutes respectively. at will take many different forms (may have useful code you could copy) such as:

at now + 1 month
at 8am monday 

Those are the two command line utilities that come to mind immediately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top