Mon, 19 Mar 2012 22:26:07 +0000
Add MIT/X11 licence
24 | 1 | ndp is a "natural date parser" library. It allows you to convert natural language user input to a more machine-friendly time value. |
2 | ||
3 | ## Demo | |
4 | An example utility is provided called readdate.lua, which reads lines from stdin, and | |
5 | prints the interpreted date along with any text that wasn't considered part of the time | |
6 | specification. | |
7 | ||
8 | ## Examples | |
9 | ||
10 | today | |
11 | => Sun Jun 21 23:45:57 2009 (1245624357) [?,?] | |
12 | ||
13 | feed the cat in the morning | |
14 | => Mon Jun 22 09:30:09 2009 (1245659409): feed the cat [13,28] | |
15 | ||
16 | go shopping next tuesday | |
17 | => Tue Jun 23 23:46:27 2009 (1245797187): go shopping next [17,25] | |
18 | ||
19 | service the car in September | |
20 | => Mon Sep 21 23:46:48 2009 (1253573208): service the car [16,29] | |
21 | ||
22 | Watch the games in July 2012 | |
23 | => Sat Jul 21 23:48:06 2012 (1342910886): Watch the games [16,29] | |
24 | ||
25 | ## Usage | |
26 | ||
27 | ...is simple :) | |
28 | ||
29 | To get a time from an input string, simply do: | |
30 | ||
31 | local ndp = require "ndp"; -- Load the ndp library | |
32 | local mytime = ndp.when("tomorrow"); -- Get the date tomorrow | |
33 | print(os.date("%c", mytime)); -- Print it out | |
34 | ||
35 | A slightly more advanced usage is extracting information in the input string which | |
36 | was not concerned with the time. This is useful for extracting things like "feed the cat" | |
37 | from "feed the cat tomorrow afternoon". ndp.when() returns 2 extra values which are the start | |
38 | and finish positions of the text it actually used in the string. By removing the text between | |
39 | these positions you will be left with the real data. | |
40 | ||
41 | local ndp = require "ndp"; -- Load the ndp library | |
42 | local mytime, startpos, endpos = ndb.when("tomorrow afternoon, feed the cat"); | |
43 | local message = (line:sub(1, start)..line:sub(finish, -1)) -- returns ", feed the cat" | |
44 | -- To make things a bit neater, we can use Lua's gsub function to trim leftover spaces and | |
45 | -- punctuation from the result: | |
46 | message = message:gsub("^[%s%p]+", ""):gsub("[%s%p]+$", ""); | |
47 | print(message); -- feed the cat |