readdate.lua

Mon, 19 Mar 2012 22:26:07 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 19 Mar 2012 22:26:07 +0000
changeset 28
3ddc836b7845
parent 22
707793a758c4
permissions
-rwxr-xr-x

Add MIT/X11 licence

4
305e9dd7fc34 readdate: Make executable
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
1 #!/usr/bin/env lua
305e9dd7fc34 readdate: Make executable
Matthew Wild <mwild1@gmail.com>
parents: 3
diff changeset
2
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local ndp = require "ndp";
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local base;
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 for line in io.lines() do
10
6700d4722be7 readdate: Print any text unused by the time calculation
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
7 if line:match("^base:") then
6700d4722be7 readdate: Print any text unused by the time calculation
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
8 -- Set the unixtime to calculate relative to, eg. base:0
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 base = tonumber(line:match("^base:(%d+)$"));
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 else
10
6700d4722be7 readdate: Print any text unused by the time calculation
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
11 local ret, start, finish = ndp.when(line, base);
6700d4722be7 readdate: Print any text unused by the time calculation
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
12 local remaining = "";
6700d4722be7 readdate: Print any text unused by the time calculation
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
13 if start and finish then
6700d4722be7 readdate: Print any text unused by the time calculation
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
14 remaining = (line:sub(1, start)..line:sub(finish, -1)):gsub("^[%s%p]+", ""):gsub("[%s%p]+$", "");
6700d4722be7 readdate: Print any text unused by the time calculation
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
15 end
22
707793a758c4 readdate: Fix traceback when nothing is consumed from the input text
Matthew Wild <mwild1@gmail.com>
parents: 19
diff changeset
16 print(os.date("%c", ret).." ("..ret..")"..((#remaining>0 and (": "..remaining)) or "").." ["..(start or "?")..","..(finish or "?").."]");
0
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
44416491923e Initial commit of ndp, the natural date processing library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19

mercurial