From 9e9c8f167b068fe506e633c71f2395f59bd8ce0b Mon Sep 17 00:00:00 2001 From: Mike Mason Date: Tue, 4 Dec 2007 21:55:36 +0000 Subject: Added timezone information to date.lua. Added fs.file_write to be able to add newlines at the end of the file(most strings don't have them) and also added a function to format.lua git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@408 ab2d0c66-481e-0410-8bed-d214d4d58bed --- lib/date.lua | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/format.lua | 9 ++++ lib/fs.lua | 2 + 3 files changed, 160 insertions(+) (limited to 'lib') diff --git a/lib/date.lua b/lib/date.lua index 2d8dab6..cdc6ad0 100644 --- a/lib/date.lua +++ b/lib/date.lua @@ -4,6 +4,7 @@ module(..., package.seeall) require("posix") require("format") +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 @@ -56,6 +57,123 @@ revdow = { ["sunday"] = 1, ["sun"] = 2, ["saturday"] = 7, ["sat"] =7 } +-- + usually denotes right of PM and - means left. It seems that /etc/TZ needs these +--reversed for alpine/busybox. difference in col 2 and 5 +--this list is not full. May need some more added. No Africa or Asia +--Abrr TZ,Real Offset, FullName, Location, What would be put in /etc/TZ(busybox needed offset) + +timezones = { + +{"A","+1","Alpha Time Zone","Military","Alpha-1"}, +{"ACDT","+10:30","Australian Central Daylight Time","Australia","ACDT-10:30"}, +{"ACST","+9:30","Australian Central Standard Time","Australia","ACST-9:30"}, +{"ADT","-3","Atlantic Daylight Time","North America","ADT+3"}, +{"AEDT","+11","Australian Eastern Daylight Time","Australia","AEDT-11"}, +{"AEST","+10","Australian Eastern Standard Time","Australia","AEST-10"}, +{"AKDT","-8","Alaska Daylight Time","North America","AKDT+8"}, +{"AKST","-9","Alaska Standard Time","North America","AKST+9"}, +{"AST","-4","Atlantic Standard Time","North America","AST+4"}, +{"AWDT","+9","Australian Western Daylight Time","Australia","AWDT-9"}, +{"AWST","+8","Australian Western Standard Time","Australia","AWST-8"}, + +{"B","+2","Bravo Time Zone","Military","Bravo-2"}, +{"BST","+1","British Summer Time","Europe","BST-1"}, + +{"C","+3","Charlie Time Zone","Military","Charlie-3"}, +{"CDT","-5","Central Daylight Time","North America","CDT+5"}, +{"CEDT","+2","Central European Daylight Time","Europe","CEDT-2"}, +{"CEST","+2","Central European Summer Time","Europe","CEST-2"}, +{"CET","+1","Central European Time","Europe","CET-1"}, +{"CST","+10:30","Central Summer(Daylight) Time","Australia","CST-10:30"}, +{"CST","+9:30","Central Standard Time","Australia","CST-9:30"}, +{"CST","-6","Central Standard Time","North America","CST+6"}, +{"CXT","+7","Christmas Island Time","Australia","CXT-7"}, + +{"D","+4","Delta Time Zone","Military","Delta-4"}, + +{"E","+5","Echo Time Zone","Military","Echo-5"}, +{"EDT","-4","Eastern Daylight Time","North America","EDT+4"}, +{"EEDT","+3","Eastern European Daylight Time","Europe","EEDT-3"}, +{"EEST","+3","Eastern European Summer Time","Europe","EEST-3"}, +{"EET","+2","Eastern European Time","Europe","EET-2"}, +{"EST","+11","Eastern Summer(Daylight) Time","Australia","EST-11"}, +{"EST","+10","Eastern Standard Time","Australia","EST-10"}, +{"EST","-5","Eastern Standard Time","North America","EST+5"}, + +{"F","+6","Foxtrot Time Zone","Military","Foxtrot-6"}, + +{"G","+7","Golf Time Zone","Military","Golf-7"}, +{"GMT","+0","Greenwich Mean Time","Europe","GMT+0"}, + +{"H","+8","Hotel Time Zone","Military","Hotel-8"}, +{"HAA","-3","Heure Avancée de l'Atlantique","North America","HAA+3"}, +{"HAC","-5","Heure Avancée du Centre","North America","HAC+5"}, +{"HADT","-9","Hawaii-Aleutian Daylight Time","North America","HADT+9"}, +{"HAE","-4","Heure Avancée de l'Est","North America","HAE+4"}, +{"HAP","-7","Heure Avancée du Pacifique","North America","HAP+7"}, +{"HAR","-6","Heure Avancée des Rocheuses","North America","HAR+6"}, +{"HAST","-10","Hawaii-Aleutian Standard Time","North America","HAST+10"}, +{"HAT","-2:30","Heure Avancée de Terre-Neuve","North America","HAT+2:30"}, +{"HAY","-8","Heure Avancée du Yukon","North America","HAY+8"}, +{"HNA","-4","Heure Normale de l'Atlantique","North America","HNA+4"}, +{"HNC","-6","Heure Normale du Centre","North America","HNC+6"}, +{"HNE","-5","Heure Normale de l'Est","North America","HNE+5"}, +{"HNP","-8","Heure Normale du Pacifique","North America","HNP+8"}, +{"HNR","-7","Heure Normale des Rocheuses","North America","HNR+7"}, +{"HNT","-3:30","Heure Normale de Terre-Neuve","North America","HNT+3:30"}, +{"HNY","-9","Heure Normale du Yukon","North America","HNY+9"}, + +{"I","+9","India Time Zone","Military","India-9"}, +{"IST","+1","Irish Summer Time","Europe","IST-1"}, + +{"K","+10","Kilo Time Zone","Military","Kilo-10"}, + +{"L","+11","Lima Time Zone","Military","Lima-11"}, + +{"M","+12","Mike Time Zone","Military","Mike-12"}, +{"MDT","-6","Mountain Daylight Time","North America","MDT+6"}, +{"MESZ","+2","Mitteleuroäische Sommerzeit","Europe","MESZ-2"}, +{"MEZ","+1","Mitteleuropäische Zeit","Europe","MEZ-1"}, +{"MST","-7","Mountain Standard Time","North America","MST+7"}, + +{"N","-1","November Time Zone","Military","November+1"}, +{"NDT","-2:30","Newfoundland Daylight Time","North America","NDT+2:30"}, +{"NFT","+11:30","Norfolk (Island) Time","Australia","NFT-11:30"}, +{"NST","-3:30","Newfoundland Standard Time","North America","NST+3:30"}, + +{"O","-2","Oscar Time Zone","Military","Oscar+2"}, + +{"P","-3","Papa Time Zone","Military","Papa+3"}, +{"PDT","-7","Pacific Daylight Time","North America","PDT+7"}, +{"PST","-8","Pacific Standard Time","North America","PST+8"}, + +{"Q","-4","Quebec Time Zone","Military","Quebec+4"}, + +{"R","-5","Romeo Time Zone","Military","Romeo+5"}, + +{"S","-6","Sierra Time Zone","Military","Sierra+6"}, + +{"T","-7","Tango Time Zone","Military","Tango+7"}, + +{"U","-8","Uniform Time Zone","Military","Uniform+8"}, +{"UTC","+0","Coordinated Universal Time","Europe","UTC+0"}, + +{"V","-9","Victor Time Zone","Military","Victor+9"}, + +{"W","-10","Whiskey Time Zone","Military","Whiskey+10"}, +{"WEDT","+1","Western European Daylight Time","Europe","WEDT-1"}, +{"WEST","+1","Western European Summer Time","Europe","WEST-1"}, +{"WET","+0","Western European Time","Europe","WET+0"}, +{"WST","+9","Western Summer(Daylight) Time","Australia","WST-9"}, +{"WST","+8","Western Standard Time","Australia","WST-8"}, + +{"X","-11","X-ray Time Zone","Military","X-ray+11"}, + +{"Y","-12","Yankee Time Zone","Military","Yankee+12"}, + +{"Z","+0","Zulu Time Zone","Military","Zulu+0"} + +} --os.time() will give seconds since 1970-epoch --os.date() will give formated time strings @@ -177,3 +295,34 @@ function abr_dow_num (search) return revdow[string.lower(search)] end +--tell me what TimeZone my system is set to + +function what_tz () + f = fs.read_file_as_array("/etc/TZ") + local tz = f[1] + return tz +end + +--change the timezone my system is set to + +function change_tz ( tz ) + --give us something like CET-1, this is busy box offset need to fix. + + tz = string.gsub(tz, "%+", "%%+") + tz = string.gsub(tz, "%-", "%%-") + tz = "^" .. tz .. "$" + result = {} + for a=1,table.maxn(date.timezones) do + c = string.match(date.timezones[a][5], tz) + if c ~= nil then result[#result +1] = c end + end + + if table.maxn(result) == 1 then + fs.write_file("/etc/TZ", result[1]) + mess = "Success" + else + mess = "Too many matches." + end + + return mess,date.what_tz() +end diff --git a/lib/format.lua b/lib/format.lua index 903a593..dff5019 100644 --- a/lib/format.lua +++ b/lib/format.lua @@ -39,6 +39,15 @@ function cap_begin_word ( a ) return data end +function search_replace (path , find, replace) + local f = fs.read_file_as_array(path) + local lines = {} + for a,b in ipairs(f) do + local c = string.gsub(b, find, replace) + lines[#lines + 1] = c end + return lines +end + -- This code comes from http://lua-users.org/wiki/SplitJoin -- -- example: format.table_to_string(", ", {"Anna", "Bob", "Charlie", "Dolores"}) diff --git a/lib/fs.lua b/lib/fs.lua index abcb03d..04990d7 100644 --- a/lib/fs.lua +++ b/lib/fs.lua @@ -56,6 +56,8 @@ end function write_file ( path, str ) local file = io.open(path, "w") + --append a newline char + str = str .. "\n" if ( file ) then file:write(str) file:close() -- cgit v1.2.3