readdate.lua

Sun, 21 Jun 2009 23:20:40 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 21 Jun 2009 23:20:40 +0100
changeset 19
0cdb35f96cb6
parent 10
6700d4722be7
child 22
707793a758c4
permissions
-rwxr-xr-x

readdate: Show the start and finish of the consumed section of the input

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
19
0cdb35f96cb6 readdate: Show the start and finish of the consumed section of the input
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
16 print(os.date("%c", ret).." ("..ret..")"..((#remaining>0 and (": "..remaining)) or "").." ["..start..","..finish.."]");
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