Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timezone calculator.
#34
Here's my entry (haven't really tested the actual logic, hope it works... have to get back to work Smile ). 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
Reply


Messages In This Thread
Timezone calculator. - by Moneo - 07-15-2005, 06:01 AM
Timezone calculator. - by DrV - 07-15-2005, 06:24 AM
Timezone calculator. - by Moneo - 07-15-2005, 11:29 PM
Timezone calculator. - by rpgfan3233 - 07-15-2005, 11:40 PM
Timezone calculator. - by Moneo - 07-15-2005, 11:56 PM
Timezone calculator. - by Deleter - 07-16-2005, 03:13 AM
Timezone calculator. - by rpgfan3233 - 07-16-2005, 04:43 AM
Timezone calculator. - by Moneo - 07-16-2005, 05:01 AM
Timezone calculator. - by rpgfan3233 - 07-16-2005, 05:13 AM
Timezone calculator. - by Moneo - 07-16-2005, 06:14 AM
Timezone calculator. - by Moneo - 07-16-2005, 11:58 PM
Re: Timezone calculator. - by Diroga - 07-17-2005, 10:21 AM
Timezone calculator. - by anarky - 07-17-2005, 01:07 PM
Timezone calculator. - by Moneo - 07-17-2005, 10:31 PM
Timezone calculator. - by Moneo - 07-20-2005, 12:14 AM
Timezone calculator. - by DrV - 07-20-2005, 12:22 AM
Timezone calculator. - by anarky - 07-20-2005, 05:07 PM
Timezone calculator. - by Moneo - 07-20-2005, 11:52 PM
Timezone calculator. - by rpgfan3233 - 07-21-2005, 12:13 AM
Timezone calculator. - by Moneo - 07-21-2005, 12:57 AM
Timezone calculator. - by rpgfan3233 - 07-21-2005, 01:37 AM
Timezone calculator. - by Moneo - 07-27-2005, 06:11 AM
Timezone calculator. - by rpgfan3233 - 07-27-2005, 10:11 AM
Timezone calculator. - by DrV - 07-27-2005, 05:50 PM
Timezone calculator. - by anarky - 07-27-2005, 06:04 PM
Timezone calculator. - by Moneo - 07-28-2005, 02:26 AM
Timezone calculator. - by anarky - 07-29-2005, 07:59 PM
Timezone calculator. - by rpgfan3233 - 07-29-2005, 08:44 PM
Timezone calculator. - by DrV - 07-29-2005, 08:50 PM
Timezone calculator. - by Moneo - 07-29-2005, 10:54 PM
Timezone calculator. - by Moneo - 07-29-2005, 11:00 PM
Timezone calculator. - by anarky - 07-29-2005, 11:01 PM
Timezone calculator. - by rpgfan3233 - 07-29-2005, 11:28 PM
Timezone calculator. - by DrV - 07-30-2005, 12:09 AM
Timezone calculator. - by anarky - 07-30-2005, 03:53 AM
Timezone calculator. - by Moneo - 07-30-2005, 05:13 AM
Timezone calculator. - by Moneo - 07-30-2005, 05:28 AM
Timezone calculator. - by rpgfan3233 - 07-30-2005, 07:35 AM
Timezone calculator. - by DrV - 07-30-2005, 09:37 AM
Timezone calculator. - by Moneo - 07-31-2005, 04:07 AM
Timezone calculator. - by anarky - 07-31-2005, 01:33 PM
Timezone calculator. - by Moneo - 08-01-2005, 04:32 AM
Timezone calculator. - by Moneo - 08-01-2005, 05:13 AM
Timezone calculator. - by rpgfan3233 - 08-01-2005, 08:29 AM
Timezone calculator. - by Moneo - 08-01-2005, 09:08 AM
Timezone calculator. - by anarky - 08-01-2005, 05:03 PM
Timezone calculator. - by Moneo - 08-02-2005, 03:41 AM
Timezone calculator. - by Moneo - 08-02-2005, 03:47 AM
Timezone calculator. - by Moneo - 08-05-2005, 06:36 AM
Timezone calculator. - by anarky - 08-07-2005, 04:05 PM
Timezone calculator. - by Moneo - 08-07-2005, 10:47 PM
Timezone calculator. - by anarky - 08-08-2005, 07:41 PM
Timezone calculator. - by Moneo - 08-09-2005, 11:25 PM
Timezone calculator. - by DrV - 08-09-2005, 11:31 PM
Timezone calculator. - by Moneo - 08-09-2005, 11:33 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)