07-30-2005, 12:09 AM
Here's my entry (haven't really tested the actual logic, hope it works... have to get back to work ). Only tested with FreeBASIC (if it doesn't work in QB, I can change it, if that is important).
Code:
'' tzconv.bas - timezone converter
'' 29-Jul-2005 - DrV - initial version
option explicit
tzdata:
data "London", 0
data "Reykjavik", -1
data "Greenland", -2
data "Rio de Janeiro, Brazil", -3
data "Santiago de Chile", -4
data "New York", -5
data "Chicago", -6
data "Denver", -7
data "Seattle", -8
data "Alaska", -9
data "Honolulu", -10
data "Midway Island", -11
'' no GMT - 12
data "New Zealand", 12
data "Solomon Islands", 11
data "Sydney", 10
data "Tokyo", 9
data "Hong Kong and China", 8 '' obsolete? Hong Kong is once again part of China..
data "Bangkok", 7
data "Omsk, Russia", 6
data "Karachi", 5
data "Stalingrad", 4
data "Moscow", 3
data "Cairo", 2
data "Berlin", 1
type TimeZone
place as string * 22
gmtoffset as integer
end type
dim shared zones(23) as TimeZone
declare function pickzone(title as string) as integer
declare function gmtoffsettostring(gmtoffset as integer) as string
declare function picktime(title as string, h as integer, m as integer)
declare function zerostring(n as integer, chars as integer) as string
dim i as integer, s as string, n as integer
dim localzone as integer, remotezone as integer
dim localtime as string
dim h as integer, m as integer, d as integer
'' get data
restore tzdata
for i = 0 to ubound(zones)
read s, n
zones(i).place = s
zones(i).gmtoffset = n
next i
localzone = pickzone("Local time zone (arrow keys to select, Enter to accept)")
if localzone = -1 then end
remotezone = pickzone("Remote time zone (arrow keys to select, Enter to accept) ")
if remotezone = -1 then end
print "Local time zone: "; zones(localzone).place;
print gmtoffsettostring(zones(localzone).gmtoffset)
print "Remote time zone:"; zones(remotezone).place;
print gmtoffsettostring(zones(remotezone).gmtoffset)
print
if picktime("Enter local time (up/down keys to change field, left/right keys to select field, Enter to accept) (24-hour format):", h, m) = -1 then end
'' calculate remote time
h = h - zones(localzone).gmtoffset
h = h + zones(remotezone).gmtoffset
if h < 0 then
d = -1
h = h + 24
elseif h > 23 then
d = 1
h = h - 24
end if
print
print "Remote time: "; zerostring(h, 2); ":"; zerostring(m, 2); " ";
if d <> 0 then print zerostring(d, 1); " day"
print
print "Press any key to exit..."
do while len(inkey$) = 0
loop
function pickzone(title as string) as integer
dim i as integer, s as string
dim k as string
dim curr as integer
cls
locate 1, 1, 0 ' hide cursor
print title
for i = 0 to 23
locate 2 + i, 3
print zones(i).place;
locate 2 + i, 3 + 30 + 1
print gmtoffsettostring(zones(i).gmtoffset);
next i
curr = 0
locate curr + 2, 1
print ">";
do
k = inkey$
if len(k) = 1 then
if k = chr$(13) or k = chr$(10) then
pickzone = curr
exit do
elseif k = chr$(27) then ' escape
pickzone = -1
exit do
end if
elseif len(k) = 2 then
select case asc(right$(k, 1))
case 80, 77 ' down, right
if curr < ubound(zones) then
locate curr + 2, 1
print " ";
curr = curr + 1
locate curr + 2, 1
print ">";
end if
case 72, 75 ' up, left
if curr > 0 then
locate curr + 2, 1
print " ";
curr = curr - 1
locate curr + 2, 1
print ">";
end if
end select
end if
loop
exitfunc:
cls
locate 1, 1, 1 ' show cursor
end function
function gmtoffsettostring(gmtoffset as integer) as string
if gmtoffset > 0 then
gmtoffsettostring = "GMT +" + str$(gmtoffset)
else
gmtoffsettostring = "GMT -" + str$(-gmtoffset)
end if
end function
function picktime(title as string, h as integer, m as integer)
dim row as integer, hr as integer, mn as integer, ed as integer
dim k as string
print title
row = csrlin
hr = 0
mn = 0
ed = 0
locate row, 1, 0 ' hide cursor
print zerostring(hr, 2); ":"; zerostring(mn, 2)
print "^^";
do
k = inkey$
if len(k) = 1 then
if k = chr$(10) or k = chr$(13) then
h = hr
m = mn
picktime = 0
exit do
elseif k = chr$(27) then
picktime = -1
exit do
end if
elseif len(k) = 2 then
if ed = 0 then
select case asc(right$(k, 1))
case 77 ' right
ed = 1
locate row + 1, 1
print " ^^";
case 72 ' up
hr = hr + 1
if hr >= 24 then hr = 0
locate row, 1
print zerostring(hr, 2);
case 80 ' down
hr = hr - 1
if hr <= -1 then hr = 23
locate row, 1
print zerostring(hr, 2);
end select
else
select case asc(right$(k, 1))
case 75 ' left
ed = 0
locate row + 1, 1
print "^^ ";
case 72 ' up
mn = mn + 1
if mn >= 60 then mn = 0
locate row, 4
print zerostring(mn, 2);
case 80 ' down
mn = mn - 1
if mn <= -1 then mn = 60
locate row, 4
print zerostring(mn, 2);
end select
end if
end if
loop
locate row + 1, 1
print " ";
locate row + 1, 1, 1 ' show cursor
end function
function zerostring(n as integer, chars as integer) as string
dim s as string
s = ltrim$(rtrim$(str$(n)))
if len(s) < chars then
s = string$(chars - len(s), asc("0")) + s
end if
zerostring = s
end function