From dbddbd9b0cec1bfe8e92a39552b28ab2f11195d9 Mon Sep 17 00:00:00 2001 From: Mike Mason Date: Wed, 28 Nov 2007 15:26:13 +0000 Subject: added split and join to fs.lua and gave them function names so we don't have to strsplit = require split. Added a date string to table function in date.lua git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@378 ab2d0c66-481e-0410-8bed-d214d4d58bed --- lib/README | 2 +- lib/date.lua | 28 ++++++++++++++++++++++++++++ lib/fs.lua | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/lib/README b/lib/README index 038ec0d..0d650dc 100644 --- a/lib/README +++ b/lib/README @@ -2,7 +2,7 @@ Brief discription of libs so far. For more info see http://wiki.alpinelinux.org/w/index.php?title=ACF_Libraries date.lua - Date and Time functions -fs.lua - Many file type functions. May need to be renamed +fs.lua - Many file,string,and table type functions. May need to be renamed join.lua - Takes a table and given a delimeter makes it into a string pidof.lua - Process libraries not provided by LPOSIX split.lua - Take a delimeted string and makes it into a table diff --git a/lib/date.lua b/lib/date.lua index a7ed872..0a236cc 100644 --- a/lib/date.lua +++ b/lib/date.lua @@ -3,6 +3,7 @@ module(..., package.seeall) require("posix") +require("fs") --global for date formating see below for more information --Mon Nov 26 19:56:10 UTC 2007 looks like most systems use this @@ -89,6 +90,33 @@ function seconds_to_date (t) return g end +--Wed Nov 28 14:01:23 UTC 2007 +--os.date(date.format) put into a table +--year,month,day,hour,min,sec,isdst- may need a dst table to set this automatically +function string_to_table (str) + if str == nil then str = os.date(format) end + g = {} + temp = fs.string_to_table("%s",str) + month = abr_month_num(temp[2]) + g["month"] = month + day = temp[3] + g["day"] = day + --may do something with this if have a tz table ?? + tz = temp[5] + year = temp[6] + g["year"] = year + temp2 = fs.string_to_table(":",temp[4]) + hour = temp2[1] + g["hour"] = hour + min = temp2[2] + g["min"] = min + sec = temp2[3] + g["sec"] = sec + return g + +end + + --give dates in seconds and gives the difference in years,months,days,... --gives a table back with hour,min,month,sec,day,year to display something like --you have 10 years, 14 hours, 10 days to renew you certificate diff --git a/lib/fs.lua b/lib/fs.lua index 88df9f1..79d0cdc 100644 --- a/lib/fs.lua +++ b/lib/fs.lua @@ -155,3 +155,39 @@ function find ( what, where ) end end +-- This code comes from http://lua-users.org/wiki/SplitJoin +-- -- example: strjoin(", ", {"Anna", "Bob", "Charlie", "Dolores"}) +function table_to_string (delimiter, list) + local len = getn(list) + if len == 0 then + return "" + end + local string = list[1] + for i = 2, len do + string = string .. delimiter .. list[i] + end + return string +end + +-- This code comes from http://lua-users.org/wiki/SplitJoin +-- example: strsplit(",%s*", "Anna, Bob, Charlie,Dolores") +function string_to_table (delimiter, text) + local list = {} + local pos = 1 + -- this would result in endless loops + if string.find("", delimiter, 1) then + error("delimiter matches empty string!") + end + while 1 do + local first, last = string.find(text, delimiter, pos) + if first then -- found? + table.insert(list, string.sub(text, pos, first-1)) + pos = last+1 + else + table.insert(list, string.sub(text, pos)) + break + end + end + return list +end + -- cgit v1.2.3