Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timezone calculator.
#31
Quote:Psuedo code:
Code:
Display cities
Choose your city (this could be preset)
Find own timezone (also maybe preset)
Choose destination city
Find dest city timezone (Mine is in a database, but I am having some curious problems with that)
Calculate difference between own city and GMT, adjust time
Calculate difference betwen destination city and GMT, adjust time

I have not included DST anywhere in the world. Therefore times may vary. I bet you didn't know Kathmandu is in an ODD timezone (GMT+5:45)?

>anarky
Your psedo-code is a bit too high level when it comes to the "calculate difference" sections. Give us a little more detail.

Regarding Kathmandu, whereever that is, there are a bunch of places around the world that have partial hourly offsets. They want to be, or don't want to be in the same timezone with a neighboring country, city, or island.

Our program will put these places into one of the 24 timezone that the world's map indicates, with no adjustments.
*****
Reply
#32
Ill give you a zip in about 15 minutes. Tongue Expect this post to be modified.

>anarky
Screwing with your reality since 1998.
Reply
#33
Quote:RPGFAN,

Very good. The main part which calculates the time at the TO timezone, seems to be working.

Had some problems because of not knowing what the format of the input should be. Like the date, what format do you want?

By the way, we had decided that we were not going to calculate dates, only a day adjustment like -1, 0, +1.

I need to do a bit more testing. But in the meantime, you are a stong contender for winning this challenge. Good work! Big Grin
*****

Oops, sorry. I was looking at your first post for the rules. Tongue It said the dates were in mm/dd/yyyy format and time's were in hh:mm format.

Edit: For testing purposes, you can also use just HHMM or HMM for time and mmddyyyy for the date (no symbols necessary) since it strips the symbols out anyway. Alternatively, you could replace the DO-LOOP section for the date with this code if you want to use the current date:
[syntax="qbasic"]date = DATE$
FOR i = 1 TO LEN(date)
IF INSTR(key$, MID$(date, i, 1)) <> 0 THEN
dateStr$ = dateStr$ + MID$(date, i, 1)
END IF
NEXT i[/syntax]
Since DATE$ is automatically in mm/dd/yyyy format. . .
974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Reply
#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
#35
Quote:anarky,

Thanks for your exhaustive analysis of the timezone exceptions in Australia. As you can well imagine, no one is going to want to program these hairy exceptions into his program for Australia and any other non-standard timezone --- even if he happens to live there.
*****

Well, I did. And when you see how, you'd wish you had thought of it. Only my arithmatic is coded. The rest is in a user editable database.

Format for database goes as follows:

<Offset>, <number of cities>
<"city1">....<"city(numcities)"> etc
...
loops

Only at this point it's still buggy, and fully testing it is not complete. Stay tuned. Within the next 24 hours it will be finished and online!

>anarky
Screwing with your reality since 1998.
Reply
#36
Quote: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.....
Brilliant program, DrV. Everything works great! Big Grin

I particularly like the way you made all the input fields to be menu selectable, avoiding all the input validation, which in my version cost me about 50 lines of code.

It definitely doesn't work in QB, causing 14 compile errors. I tried fixing it but still got errors caused by statements such as:
function pickzone(title as string) as integer
which give a syntax error.

If you would, I personally would like a QB version, basically so I can understand it. It might help other members as well.

Even though it's in FB, your solution is definitely a contender for winning this challenge.
*****
Reply
#37
Quote:
Moneo Wrote:anarky,

Thanks for your exhaustive analysis of the timezone exceptions in Australia. As you can well imagine, no one is going to want to program these hairy exceptions into his program for Australia and any other non-standard timezone --- even if he happens to live there.
*****

Well, I did. And when you see how, you'd wish you had thought of it. Only my arithmatic is coded. The rest is in a user editable database.

Format for database goes as follows:

<Offset>, <number of cities>
<"city1">....<"city(numcities)"> etc
...
loops

Only at this point it's still buggy, and fully testing it is not complete. Stay tuned. Within the next 24 hours it will be finished and online!

>anarky

Sounds like you're turning this challenge into a thesis. Can't wait to see the final results. If the program uses a database, are you going to include the code to create and update this database? Otherwise, how can we test the timezone program?

BTW, how many cows are they milking in the dairy where you work? Do they milk twice or three times a day? What kind of equipment or setup do they have in the milking barn? In Australia, do you measure milk production by "pounds of butterfat" or by gallons or liters? What's your average daily production per cow? Just curious 'cause my family has a dairy, and I know enough to be dangerous.
*****
Reply
#38
Quote:It definitely doesn't work in QB, causing 14 compile errors. I tried fixing it but still got errors caused by statements such as:
function pickzone(title as string) as integer
which give a syntax error.

It's not too hard to convert to QB (I just did it). Mainly function name conversion is all that is needed.

Overall, nice program, DrV. It definitely could win this one. We'll have to see what response (and/or possibly modified code) Anarky comes up with.
974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Reply
#39
Here's a version for QB (should still work in FB too); I made a few small aesthetic changes.

(I didn't feel like writing validation code, so I made sure the input was always valid. Smile )

Code:
DECLARE FUNCTION pickzone% (title AS STRING)
DECLARE FUNCTION gmtoffsettostring$ (gmtoffset AS INTEGER)
DECLARE FUNCTION picktime% (title AS STRING, h AS INTEGER, m AS INTEGER)
DECLARE FUNCTION zerostring$ (n AS INTEGER, chars AS INTEGER)
'' tzconv.bas - timezone converter
'' 29-Jul-2005 - DrV - initial version
'' 29-Jul-2005 - DrV - modified to work with QB, plus some minor visual tweaks

''option explicit

DEFINT A-Z

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)
'DECLARE FUNCTION gmtoffsettostring$ (gmtoffset AS INTEGER)
'DECLARE FUNCTION picktime (title AS STRING, h AS INTEGER, m AS INTEGER)
'DECLARE FUNCTION zerostring$ (n AS INTEGER, chars AS INTEGER)

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
DIM number AS STRING

'' get data
RESTORE tzdata
FOR i = 0 TO UBOUND(zones)
   READ s, number
   n = VAL(number)
   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
PRINT "Enter local time (up/down keys to change field, left/right keys to select field, Enter to accept) (24-hour format)"
PRINT
IF picktime("Local time:", 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
COLOR 7
PRINT "Remote time: "
COLOR 15
PRINT zerostring$(h, 2); ":"; zerostring$(m, 2); " ";
IF d < 0 THEN
   PRINT "-";
ELSEIF d > 0 THEN
   PRINT "+";
ELSE
   PRINT
END IF
IF d <> 0 THEN PRINT zerostring$(ABS(d), 1); " day"
COLOR 7
PRINT
PRINT "Press any key to exit..."
DO WHILE LEN(INKEY$) = 0
LOOP

DEFSNG A-Z
FUNCTION gmtoffsettostring$ (gmtoffset AS INTEGER)
   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
   COLOR 15
   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 pickzone% (title AS STRING)
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 zerostring$ (n AS INTEGER, chars AS INTEGER)
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
#40
Quote:Here's a version for QB (should still work in FB too); I made a few small aesthetic changes......

Your QB version also works 100%. Very nice work! Big Grin

So far you have the only completely working version. We'll wait a bit to see if the other participants can come up with a good version before announcing the winner.
*****
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)