Qbasicnews.com
December 10, 2019, 07:18:14 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 [2] 3 4
  Print  
Author Topic: Timezone calculator.  (Read 40569 times)
DrV
Na_th_an
*****
Posts: 1553



WWW
« Reply #15 on: July 19, 2005, 04:22:00 PM »

I would say the simplest way (just off the top of my head, at least) would be to convert the local time to GMT (by adding or subtracting the offset, except use the opposite sign as specified) and then converting from that GMT-relative time to the remote time (by adding or subtracting offset from GMT again, but with the correct sign).
Logged
anarky
Been there, done that
*****
Posts: 1231


The Blobworld Comics King


« Reply #16 on: July 20, 2005, 09:07:46 AM »

The whole goal behind this program is to find out what time it is and where at any given time. Not what timezone it is. Maybe I'm tired and didn't fully understand your post. Maybe I'm not.

I'll work on it tomorrow for you. It's very simple.

>anarky - Overworked: Underpayed.
Logged

Screwing with your reality since 1998.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #17 on: July 20, 2005, 03:52:57 PM »

Quote from: "DrV"
I would say the simplest way (just off the top of my head, at least) would be to convert the local time to GMT (by adding or subtracting the offset, except use the opposite sign as specified) and then converting from that GMT-relative time to the remote time (by adding or subtracting offset from GMT again, but with the correct sign).


DrV,
Not a bad idea. I understand it as a two step operation. First adjust the time at the FROM timezone to GMT 0. Second, adjust that for the number of hours from GMT 0 to the target TO timezone.
I think it could work.

I already have a solution which I'm in the process of testing, which basically does the operation in one step per each of the following 4 from/to cases:
FROM GMT+ ---> TO GMT+
FROM GMT+ ---> TO GMT-
FROM GMT- ---> TO GMT+
FROM GMT- ---> TO GMT-

Excluding the input and validation of the 3 input fields,
and excluding defining the timezone table,
the actual logic takes 30 lines of very simple code. Determining how do to this logic has taken me several days of analysis.

Also, it turns out that the timezone table is only needed to lookup and print the name of the city at each timezone. It is not needed for the logic, at least not in my version.


ANARKY,
Your right about the goal.
Let me try to express it a little clearer with this example:

If I'm in New York (GMT-5), and the time is 17:00,
What time is it in Tokyo (GMT+9)? --- Answer is 07:00
And, is it the same date? ------------- Answer is: add 1 to NY date.
BTW, the difference in time from NY is +14 hours.
*****
Logged
rpgfan3233
Ancient Guru
****
Posts: 617



« Reply #18 on: July 20, 2005, 04:13:32 PM »

Quote from: "Moneo"
If I'm in New York (GMT-5), and the time is 17:00,
What time is it in Tokyo (GMT-9)? --- Answer is 07:00

I think you mean GMT+9 for Tokyo. . . Smiley
Logged

974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Moneo
Na_th_an
*****
Posts: 1971


« Reply #19 on: July 20, 2005, 04:57:52 PM »

Quote from: "rpgfan3233"
Quote from: "Moneo"
If I'm in New York (GMT-5), and the time is 17:00,
What time is it in Tokyo (GMT-9)? --- Answer is 07:00

I think you mean GMT+9 for Tokyo. . . Smiley


Thanks, rpg,
You're absolutely right. :oops: I make the same mistake when I'm testing --- no wonder I get the wrong answer. :roll:

I'm glad someone is paying attention. Cheesy

BTW, your name, rpgfan3233, it couldn't possibly be rpg for RPG (Report Program Generator)?
*****
Logged
rpgfan3233
Ancient Guru
****
Posts: 617



« Reply #20 on: July 20, 2005, 05:37:33 PM »

Quote from: "Moneo"
BTW, your name, rpgfan3233, it couldn't possibly be rpg for RPG (Report Program Generator)?
*****

Sorry, but you are wrong. Smiley Why do you ask about a Report Program Generator? :Huh:
RPG (I got lazy and used all lowercase) - Role-Playing Game
Logged

974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Moneo
Na_th_an
*****
Posts: 1971


« Reply #21 on: July 26, 2005, 10:11:27 PM »

Hey guys, is anybody working on this timezone problem?

If anybody has something in the works, please say so, because I'm ready to post my own version. I don't want to steal your thunder.  :wink:

DrV, rgpfan, Deleter and anarky: You all seem to understand the problem. You must have developed some solution, no?

Remember, there's no best way to do this. I know of 3 ways, and finally picked the one that I was able to get working.

Please let me know, 'cause this thread is getting stale.
*****
Logged
rpgfan3233
Ancient Guru
****
Posts: 617



« Reply #22 on: July 27, 2005, 02:11:44 AM »

I haven't worked on it, but the code shouldn't be too long. I might post mine on Thursday (I'm busy all day Wednesday).
Logged

974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
DrV
Na_th_an
*****
Posts: 1553



WWW
« Reply #23 on: July 27, 2005, 09:50:23 AM »

I haven't written anything yet; if I don't post anything in, say, 2 days, feel free to post your own. I might get to it, but I wouldn't count on it (this 'time' business is the problem - or rather, not enough of it Smiley ).
Logged
anarky
Been there, done that
*****
Posts: 1231


The Blobworld Comics King


« Reply #24 on: July 27, 2005, 10:04:01 AM »

Heck, I'd forgotten about it. I really don't have the time myself, but I think I can work out a text one for you.

>anarky
Logged

Screwing with your reality since 1998.
Moneo
Na_th_an
*****
Posts: 1971


« Reply #25 on: July 27, 2005, 06:26:32 PM »

RPGFAN,
Ok, I'll wait until Thursday night.
If you need extra time, tell me.


DrV,
You should be able to whip something up by Thursday night.
Give it a shot. Forget all the frills, just work on computing the time at the TO timezone.


ANARKY,
You said: "I think I can work out a text one."
If that means a pseudo-code version, that's fine. We'd be interested in seeing you method, at least.

*****
Logged
anarky
Been there, done that
*****
Posts: 1231


The Blobworld Comics King


« Reply #26 on: July 29, 2005, 11:59:25 AM »

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
Logged

Screwing with your reality since 1998.
rpgfan3233
Ancient Guru
****
Posts: 617



« Reply #27 on: July 29, 2005, 12:44:22 PM »

Sorry, I forgot about this. I finished it, but forgot to post it.

It doesn't have comments, unfortunately, but it should be fairly simple. Most of the stuff is just error-checking. The actual calculations are only a few lines. No DST was included, as per the revised rules.

[syntax="qbasic"]CONST key$ = "0123456789"
DIM tZone1 AS STRING, date AS STRING, time AS STRING, tZone2 AS STRING
DIM cities(-11 TO 12) AS STRING

FOR i = -11 TO 12
    READ cities(i)
NEXT i

CLS
DO
    plusMinusCount = 0
    CLS
    LOCATE 1, 1
    INPUT "Enter the timezone you are currently in: ", tZone1
    FOR i = 1 TO LEN(tZone1)
        IF MID$(tZone1, i, 1) = "-" OR MID$(tZone1, i, 1) = "+" THEN
            plusMinusCount = plusMinusCount + 1
            IF i <> LEN(tZone1) - 1 AND i <> LEN(tZone1) - 2 THEN
                badZone = -1
            ELSE
                badZone = 0
            END IF
            IF plusMinusCount > 1 THEN badZone = -1: EXIT FOR
        END IF
    NEXT i
    IF badZone <> -1 THEN
        IF INSTR(tZone1, "GMT") <> 0 AND LEN(tZone1) < 7 THEN
            IF INSTR(tZone1, "-") = LEN(tZone1) - 2 OR INSTR(tZone1, "+") = LEN(tZone1) - 2 THEN
                badZone = 0
                plusMinusPos = LEN(tZone1) - 2
            ELSEIF INSTR(tZone1, "-") = LEN(tZone1) - 1 OR INSTR(tZone1, "+") = LEN(tZone1) - 1 THEN
                badZone = 0
                plusMinusPos = LEN(tZone1) - 1
            ELSE
                badZone = -1
            END IF
        ELSE
            badZone = -1
        END IF
        IF VAL(RIGHT$(tZone1, plusMinusPos - 1)) > 12 OR VAL(RIGHT$(tZone1, plusMinusPos - 1)) < -11 THEN
            badZone = -1
        END IF
    END IF
LOOP WHILE badZone = -1

DO
    dateStr$ = ""
    CLS
    LOCATE 1, 1
    PRINT "Enter the timezone you are currently in: "; tZone1
    INPUT "Enter the date you are in that timezone: ", 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
    IF LEN(dateStr$) = 8 THEN
        badDate = 0
    ELSE
        badDate = -1
    END IF
LOOP WHILE badDate = -1

DO
    timeStr$ = ""
    CLS
    LOCATE 1, 1
    PRINT "Enter the timezone you are currently in: "; tZone1
    PRINT "Enter the date you are in that timezone: "; date
    INPUT "Enter the time you are in that timezone: ", time
    FOR i = 1 TO LEN(time)
        IF INSTR(key$, MID$(time, i, 1)) <> 0 THEN
            timeStr$ = timeStr$ + MID$(time, i, 1)
        END IF
    NEXT i
    IF LEN(timeStr$) = 4 THEN
        IF VAL(LEFT$(timeStr$, 2)) < 24 AND VAL(RIGHT$(timeStr$, 2)) < 60 THEN
            badTime = 0
        ELSE
            badTime = -1
        END IF
    ELSEIF LEN(timeStr$) = 3 THEN
        timeStr$ = "0" + timeStr$
        IF VAL(LEFT$(timeStr$, 2)) < 24 AND VAL(RIGHT$(timeStr$, 2)) < 60 THEN
            badTime = 0
        ELSE
            badTime = -1
        END IF
    ELSE
        badTime = -1
    END IF
LOOP WHILE badTime = -1
DO
    plusMinusCount2 = 0
    CLS
    LOCATE 1, 1
    PRINT "Enter the timezone you are currently in: "; tZone1
    PRINT "Enter the date you are in that timezone: "; date
    PRINT "Enter the time you are in that timezone: "; time
    INPUT "Enter the timezone you want to find the time for: ", tZone2
    FOR i = 1 TO LEN(tZone2)
        IF MID$(tZone2, i, 1) = "-" OR MID$(tZone2, i, 1) = "+" THEN
            plusMinusCount2 = plusMinusCount2 + 1
            IF i <> LEN(tZone2) - 1 AND i <> LEN(tZone2) - 2 THEN
                badZone = -1
            ELSE
                badZone = 0
            END IF
            IF plusMinusCount2 > 1 THEN badZone = -1: EXIT FOR
        END IF
    NEXT i
    IF badZone <> -1 THEN
        IF INSTR(tZone2, "GMT") <> 0 AND LEN(tZone2) < 7 THEN
            IF INSTR(tZone2, "-") = LEN(tZone2) - 2 OR INSTR(tZone2, "+") = LEN(tZone2) - 2 THEN
                badZone = 0
                plusMinusPos2 = LEN(tZone2) - 2
            ELSEIF INSTR(tZone2, "-") = LEN(tZone2) - 1 OR INSTR(tZone2, "+") = LEN(tZone2) - 1 THEN
                badZone = 0
                plusMinusPos2 = LEN(tZone2) - 1
            ELSE
                badZone = -1
            END IF
        ELSE
            badZone = -1
        END IF
        IF VAL(RIGHT$(tZone2, plusMinusPos - 1)) > 12 OR VAL(RIGHT$(tZone2, plusMinusPos - 1)) < -11 THEN
            badZone = -1
        END IF
    END IF
LOOP WHILE badZone = -1
gmt = VAL(LEFT$(timeStr$, 2)) + (-1 * VAL(MID$(tZone1, plusMinusPos)))
IF gmt + VAL(MID$(tZone2, plusMinusPos2)) > 24 THEN
    MID$(dateStr$, 3, 2) = LTRIM$(STR$(VAL(MID$(dateStr$, 3, 2)) + 1))
    hour$ = LTRIM$(STR$((gmt + VAL(MID$(tZone2, plusMinusPos2))) - 24))
ELSEIF gmt + VAL(MID$(tZone2, plusMinusPos2)) < 0 THEN
    MID$(dateStr$, 3, 2) = LTRIM$(STR$(VAL(MID$(dateStr$, 3, 2)) - 1))
    hour$ = LTRIM$(STR$(24 - ABS(gmt + VAL(MID$(tZone2, plusMinusPos2)))))
ELSE
    hour$ = LTRIM$(STR$(gmt + VAL(MID$(tZone2, plusMinusPos2))))
END IF
finalTime$ = hour$ + ":" + RIGHT$(timeStr$, 2)
finalDate$ = LEFT$(dateStr$, 2) + "/" + MID$(dateStr$, 3, 2) + "/" + RIGHT$(dateStr$, 4)
PRINT tZone2, finalDate$, finalTime$, cities(VAL(MID$(tZone2, plusMinusPos2)))
END

DATA "Midway Island","Honolulu","Alaska","Seattle","Denver","Chicago"
DATA "New York","Santiago de Chile","Rio de Janeiro","Greenland","Reykjavik","London"
DATA "Berlin","Cairo","Moscow","Stalingrad","Karachi","Omsk, Russia"
DATA "Bangkok","Hong Kong, and China","Tokyo","Sydney","Solomon Islands","New Zealand"[/syntax]

Of course, there are probably bugs, but I didn't spend much time on it. Smiley
Logged

974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
DrV
Na_th_an
*****
Posts: 1553



WWW
« Reply #28 on: July 29, 2005, 12:50:32 PM »

Oops, forgot too  :oops: .  (Plus I managed to blow away the Windows bootloader again a couple days ago, ouch... Smiley )  I might whip something up tonight after I get home from work.
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #29 on: July 29, 2005, 02:54:38 PM »

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! Cheesy
*****
Logged
Pages: 1 [2] 3 4
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!