time in qb

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

lostoros
Newbie
Posts: 5
Joined: Fri Sep 30, 2005 10:46 am

time in qb

Post by lostoros »

thanks for all your help with my last question....now i have another one

im now on to another project, writing (or trying) a football simulation game. Right now I can't figure out a good way to do the clock, any help is appreciated...
User avatar
matt2jones
Veteran
Posts: 80
Joined: Sat Feb 19, 2005 8:29 am
Location: elsewhere
Contact:

Post by matt2jones »

The TIMER function returns the current time of day, in seconds.

if you save the value of TIMER when the football game starts, then using simple arithmatic it's possible to find out how long the game's been going on, and to save game progress and restore later.

matt
Do not mistake Apathy for feeling Content.

http://www.disjointed.cjb.net - Short Storys
http://matt2jones.deviantart.com - Random Art
http://www.freewebs.com/matt2jones - WebComic
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

If you only need one second of accuracy, a simpler method is to used the TIME$ function.
T$ = TIME$

T$ will contain HH:MM:SS where the HH is in military time, i.e., 2 PM is 14. You still have to do some arithmetic to get the elapsed time in hours, minutes or seconds from some starting point.

The TIMER function returns a floating point value for the number of system clock ticks since midnight. These ticks are every 1/18 of a second. The conversion arithmetic could be a little harder.

Be aware that if you start your program just before midnight and it runs into the next day, then you have extra date/time considerations to add to your code.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
lostoros
Newbie
Posts: 5
Joined: Fri Sep 30, 2005 10:46 am

Post by lostoros »

how would i use that variable and do the conversion, can you describe that in more detail?
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

moneo wrote:T$ will contain HH:MM:SS where the HH is in military time, i.e., 2 PM is 14. You still have to do some arithmetic to get the elapsed time in hours, minutes or seconds from some starting point.
Also known as metric time.. wihch is what the rest of the world use.. :P

Code: Select all

t$ = TIME$
secs = VAL(RIGHT(t$, 2))
print "Seconds: "; secs
I have left this dump.
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

Actually, Z!re I do believe that most of the world uses AM and PM in homes... but I could be wrong.
Image
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Nathan1993 wrote:Actually, Z!re I do believe that most of the world uses AM and PM in homes... but I could be wrong.
You're wrong.
It's pretty much just americans..

Ofcourse, the rest of the world usually refer to time as AM and PM when talking to foreigners, because tardy americans are still stuck in imperial system and never bother learning anything :P

So if I'd say 21, you'd have no idea what I mean.. but if i say 9PM you know..

It's pretty easy.. :D


Ofcourse we all have 12 hour clocks.. And generally it's sub-understood if people mean morning or night when giving a time.. such as: The meeting is at 7

you can conclude it's 7 at night.. not in the morning, because the office open at 8.. etc


Official documents are in "military time"


Ya.. im done bashing americans now.. for a while atleast :P
I have left this dump.
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

lostoros wrote:how would i use that variable and do the conversion, can you describe that in more detail?
It depends on how you like to work with time.

If you use the TIMER function, then you will be working with a total number of seconds, containing a decimal value, which can go as high as 86,400 seconds minus 1 for any given day. This number of seconds is the number of seconds elapsed since midnight. So, when you get this value you must either store it into a SINGLE variable preserving any decimals, or into a LONG to ignore any decimals and still hold the large value.

So, if you use TIMER, then you save a starting time in seconds. Later, you get TIMER again, and subtract the starting time from this new value. The result is the number of seconds that elapsed from start to now. You can convert elapsed seconds to minutes by dividing the seconds by 60, with the remainder being the seconds. Conversion to hours works with 60 minutes per hour.

Again, it depends on how you want to handle the elapsed time. If you use the TIME$ function as I suggested earlier, you receive the time expressed as HH:MM:SS. I thought this might be easier than always converting from elapsed seconds as needed when you use TIMER.

Regardless of whether you chose to use TIMER or TIME$, you are still going to have code some hour/minute/second conversions. Think about it and decide what way makes more sense to you. Work out some paper and pencil examples.

Most people prefer using TIMER and converting from elapsed seconds to hours/minutes/seconds. Subtracting HH:MM:SS from HH:MM:SS can be a little tedious.

Here's a sample littile program for converting seconds to Hours/Minutes/Seconds. The HMS.SUB subroutine does the actual conversion. I wrote this in 1989 so please forgive the solid caps.

Code: Select all

REM Convert number of seconds to hours/minutes/seconds  (by Edward F. Moneo)

DIM ISEC AS DOUBLE
DIM HH   AS DOUBLE
DIM MM   AS INTEGER
DIM SS   AS INTEGER

CONST SECSMM =  60
CONST SECSHH =  3600

PRINT "Enter total seconds be converted ";
INPUT Z$
Z$=LTRIM$(RTRIM$(Z$))
IF Z$="" OR VAL(Z$)=0 THEN SYSTEM
GOSUB NUMONLY
IF EF=1 THEN SYSTEM
ISEC=VAL(Z$)
GOSUB HMS.SUB
PRINT "HOURS = ";HH
PRINT "MINS  = ";MM
PRINT "SECS  = ";SS
SYSTEM
REM **************************************************************************

HMS.SUB:
  IF ISEC >= SECSHH THEN
     HH = INT(ISEC/SECSHH)
     ISEC=ISEC-HH*SECSHH
  ELSE
     HH = 0
  END IF

  IF ISEC >= SECSMM THEN
     MM = INT(ISEC/SECSMM)
     ISEC=ISEC-MM*SECSMM
  ELSE
     MM = 0
  END IF
  SS = ISEC
RETURN

REM *
REM *** (NUMONLY) - CHECK FOR NUMERIC ONLY
REM *
NUMONLY:                    
  QZ$=" 1"+Z$+"1"
  IF STR$(VAL(QZ$))=QZ$ THEN EF=0 ELSE EF=1
RETURN
END
*****
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

@ Z!re: I know how to read it... :P ... But, I'm not a normal american either.... :roll: ,...

Current time here: 20:52.30 ... :wink:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

i know how to read it... military games + friends. plus one time i had a watch that was on the standard 24 hour time thingy...
Image
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

Nathan1993 wrote:plus one time i had a watch that was on the standard 24 hour time thingy...
Um,. 99% of all digital watches have that option.... :wink: .. Some with a User Interface(UI), others with a key combo.... I like the ones with the UI better.... :roll: :lol:

Anyway,.. nm... we are way off topic... :wink:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

yeah... ill answer his question using almighty psuedo code!

Code: Select all

get the time from timer
do your stuff
old time - current time = time passed
or, if you want a timer of some sort to be at 1, 1

Code: Select all

now! = timer
do stuff
locate 1,1: print int(timer - now!)
loop or whateva
puedo code... for the lazy man
Image
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Nathan1993 wrote:yeah... ill answer his question using almighty psuedo code!

Code: Select all

get the time from timer
do your stuff
old time - current time = time passed
Natt, Even if you use pseudocode, it has to make sense.
old time - current time = time passed
is backwards. It should be:
current time - old time = time passed.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

Its psuedo code. It doesnt matter. As long as you get the idea down.
Image
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Nathan1993 wrote:Its psuedo code. It doesnt matter. As long as you get the idea down.
Natt, let me clear this up for you.
Ideas are expressed textually, not in pseudo-code. When you posted:
old time - current time = time passed
This was pseudo-code, not just an idea, so it must be correct.
An idea would have been: "Compute the time passed."

For your information, the following is the first paragraph of what Cal Poly University and other universities are referring to as the Pseudocode Standard.

PSEUDOCODE STANDARD
Pseudocode is a kind of structured english for describing algorithms. It allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax. At the same time, the pseudocode needs to be complete. It describe the entire logic of the algorithm so that implementation becomes a rote mechanical task of translating line by line into source code.
*****
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

ah, I was spelling it wrong.

that may be what univeresities think, but screw them. They know nothing... well, they do... but in all the programming books I have pseudo code was used to get ideas on paper. It was used to plan things out so you dont get lost and say ??? is that.
Image
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Nath,

I try to give you sound advice and documented information, but you don't seem to want to accept it. Why?

Instead of discrediting information, why can't you say, "Hmmm, let me think about it, maybe they're right?"
*****
User avatar
Zamaster
Veteran
Posts: 174
Joined: Wed Jun 15, 2005 1:51 pm

Post by Zamaster »

Nathan says "Im sorry"
C:\DOS
C:\DOS\RUN
RUN\DOS\RUN
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

well, yeah... but they can have thier university standards and I can have mine.
Image
lostoros
Newbie
Posts: 5
Joined: Fri Sep 30, 2005 10:46 am

Post by lostoros »

ok im starting to follow you but bear with me here.

In between each play, the user will return to a screen that displays the scoreboard and plays that can be called. On the scoreboard, I want to have a ticking clock, one that literally, after every second, changes. Is this possible? I understand most of what you guys said earlier but I still don't know if I can get it to work....
Post Reply