Yesterdays Date??

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

Post Reply
Mike Alexander
Coder
Posts: 19
Joined: Fri Dec 01, 2006 8:27 pm
Location: Temecula, Ca
Contact:

Yesterdays Date??

Post by Mike Alexander »

Ok... Now for part two of my problem...

Here is some simple code I put together to overcome a Y2K issue with another program that I do not own the source for...

TODAY$=DATE$
MONTH$=LEFT$(DATE$,2)
DAY$=MID$(DATE$,4,2)
YEAR$=RIGHT$(DATE$,2)
TODAY$=MONTH$+DAY$+"1"+YEAR$+".LOG"


This will give me a date (sorta) with appended .log

Now, how can I take today's date and convert it to yesterdays date? I can not just go YDAY$=Date$-1 so what is the trick???
http://ultimate.4alexanders.com
telnet://ubbs2006.synchro.net
bungytheworm
Veteran
Posts: 288
Joined: Sat Feb 18, 2006 4:02 pm

Post by bungytheworm »

Your problem is that your variables are Strings.
String is text and "text" + "text" = "texttext".
You need to use VAL function to get your variables as numerical values.

If you have problems with VAL function, let us know.
Mike Alexander
Coder
Posts: 19
Joined: Fri Dec 01, 2006 8:27 pm
Location: Temecula, Ca
Contact:

Post by Mike Alexander »

E.K.Virtanen wrote:Your problem is that your variables are Strings.
String is text and "text" + "text" = "texttext".
You need to use VAL function to get your variables as numerical values.

If you have problems with VAL function, let us know.
I finally got it... I ended up borrowing some code I found at QBasic.com and revised it for what I needed. It ain't all that pretty, but it works good.

Unbelievable that one would need so many lines of code to get yesterday's date. :cry:
http://ultimate.4alexanders.com
telnet://ubbs2006.synchro.net
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

You're right, getting yesterday's date is not a simple matter. You need to have a table or array of the number of days in each month, plus when you're dealing with February, you need to have logic to determine leap year.

I've done this before, so why don't you post the code that you have for this and I'll take a look at it.

Regards..... Moneo
Mike Alexander
Coder
Posts: 19
Joined: Fri Dec 01, 2006 8:27 pm
Location: Temecula, Ca
Contact:

Post by Mike Alexander »

moneo wrote:You're right, getting yesterday's date is not a simple matter. You need to have a table or array of the number of days in each month, plus when you're dealing with February, you need to have logic to determine leap year.

I've done this before, so why don't you post the code that you have for this and I'll take a look at it.

Regards..... Moneo
You have already seen the bulk of my code... You wrote it :-)

I found a copy of your work on QBasic.com and I copied and pasted that. Then I added my stuff into your code... Nice piece of work for something which seems so easy... That is an excellent think to make into a library type function...

Here is what it ended up as... See any room for improvement? My stuff is in Red Bold...
My adpoted Code errr Hijacked Code wrote: DEFINT A-Z

DECLARE FUNCTION IsLeapYear% (Z)
DECLARE FUNCTION Fill$ (V#,ZL)

DIM DAYS.OFFSET AS LONG ' Plus or minus offset days from given date
DIM ZYY AS INTEGER ' Value of the 4 digit year.
DIM ZMM AS INTEGER ' Value of the 2 digit month.
DIM ZDD AS INTEGER ' Value of the 2 digit day.
DIM ZMAX AS INTEGER ' Internal value of max days in month.
DIM ZDWORK AS LONG ' Variable internal to date routines.
Z$ = "" ' Date string as MM-DD-YYYY.

DIM ZMO(1 TO 12) AS INTEGER 'Days in month array
DATA 31,28,31,30,31,30,31,31,30,31,30,31
FOR ZMM=1 TO 12:READ ZMO(ZMM):NEXT ZMM

REM ********************************************************
REM |-------------------------------------|
REM | P R O G R A M M A I N L I N E |
REM |-------------------------------------|
REM ********************************************************


'PURPOSE: Get today's date and compute yesterday's date.

z$=date$ 'Date is mm-dd-yyyy
if left$(time$,2)="00" then z$=date$ 'make sure date didn't just roll over
ZDD=VAL(MID$(Z$,4,2)) 'Set day
ZMM=VAL(MID$(Z$,1,2)) 'Set month.
ZYY=VAL(MID$(Z$,7,4)) 'Set year.

days.offset = -1 'Set for offset days of -1 (yesterday)

zdwork=zdd+days.offset 'Apply offset to today's date
if zdwork < 1 then
do
zmm=zmm-1
if zmm=0 then zmm=12:zyy=zyy-1
gosub GetZMax 'go get max days cur month (zmm)
zdwork=zdwork+zmax
loop while zdwork<1
else
gosub GetZMax
do while zdwork>zmax
zmm=zmm+1
if zmm>12 then zmm=1:zyy=zyy+1
zdwork=zdwork-zmax
gosub GetZMax
loop
end if
zdd=zdwork
'* Pack yesteray's date as MM-DD-YYYY
Z$=FILL$((ZMM),2)+"-"+FILL$((ZDD),2)+"-"+FILL$((ZYY),4)
YDAY$=Z$
MONTH$=LEFT$(YDAY$,2)
DAY$=MID$(YDAY$,4,2)
YEAR$=RIGHT$(YDAY$,2)
OLDLOG$=MONTH$+DAY$+YEAR$+".LOG"
NEWLOG$=MONTH$+DAY$+"1"+YEAR$+".LOG"

ON ERROR GOTO ERRORTRAP
OPEN OLDLOG$ for input as #1

OPEN NEWLOG$ for output as #2
Do
Line INPUT #1, LINE$
PRINT #2, LINE$
Loop Until EOF(1)
close #1
close #2
END

ERRORTRAP:
PRINT OLDLOG$ +" was not found!"
end

REM Get maximum days for month given in ZMM.
GetZMax:
IF ZMM=2 AND ISLEAPYEAR(ZYY) THEN ZMAX=ZMO(ZMM)+1 ELSE ZMAX=ZMO(ZMM)
RETURN

END

' ====================== ISLEAPYEAR ==========================
' Determines if a year is a leap year or not.
' ============================================================
'
FUNCTION IsLeapYear (Z) STATIC

' If the year is evenly divisible by 4 and not divisible
' by 100, or if the year is evenly divisible by 400, then
' it's a leap year.
' NOTE: The logic for years being a multiple of 4000 has not been
' implemented pending official notification.

IsLeapYear = (Z MOD 4 = 0 AND Z MOD 100 <> 0) OR (Z MOD 400 = 0)

END FUNCTION


' ========================= FILL$ ==================================
' Converts a value to string of specified length with leading zeros.
' ==================================================================
FUNCTION Fill$ (V#,ZL) STATIC

FILL$=right$(STRING$(ZL,"0")+MID$(STR$(V#),2),ZL)

END FUNCTION
http://ultimate.4alexanders.com
telnet://ubbs2006.synchro.net
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Nice that you're using my code. I've seen some apparently simpler code, but mine uses routines that have been working for over 15 years in my general date utility program.

I have some comments on the following code:

Code: Select all

zdd=zdwork
'* Pack yesterday's date as MM-DD-YYYY
Z$=FILL$((ZMM),2)+"-"+FILL$((ZDD),2)+"-"+FILL$((ZYY),4)
YDAY$=Z$
MONTH$=LEFT$(YDAY$,2)
DAY$=MID$(YDAY$,4,2)
YEAR$=RIGHT$(YDAY$,2)
OLDLOG$=MONTH$+DAY$+YEAR$+".LOG"
NEWLOG$=MONTH$+DAY$+"1"+YEAR$+".LOG"

ON ERROR GOTO ERRORTRAP
* When you have log files where the filename is a date, the more accepted format for the filename is YYYYMMDD.LOG. If you have many log files, you can then view the filenames in true chronological order.

* NEWLOG$=MONTH$+DAY$+"1"+YEAR$+".LOG"
makes no sense, plus you are creating a 9 character filename wihich is invalid. What are you trying to do?

* Regarding the ON ERROR, I suggest using Mac's logic that I posted for you on the other thread.

Regards..... Moneo
Mike Alexander
Coder
Posts: 19
Joined: Fri Dec 01, 2006 8:27 pm
Location: Temecula, Ca
Contact:

Post by Mike Alexander »

moneo wrote:Nice that you're using my code. I've seen some apparently simpler code, but mine uses routines that have been working for over 15 years in my general date utility program.

I have some comments on the following code:

Code: Select all

zdd=zdwork
'* Pack yesterday's date as MM-DD-YYYY
Z$=FILL$((ZMM),2)+"-"+FILL$((ZDD),2)+"-"+FILL$((ZYY),4)
YDAY$=Z$
MONTH$=LEFT$(YDAY$,2)
DAY$=MID$(YDAY$,4,2)
YEAR$=RIGHT$(YDAY$,2)
OLDLOG$=MONTH$+DAY$+YEAR$+".LOG"
NEWLOG$=MONTH$+DAY$+"1"+YEAR$+".LOG"

ON ERROR GOTO ERRORTRAP
* When you have log files where the filename is a date, the more accepted format for the filename is YYYYMMDD.LOG. If you have many log files, you can then view the filenames in true chronological order.

* NEWLOG$=MONTH$+DAY$+"1"+YEAR$+".LOG"
makes no sense, plus you are creating a 9 character filename wihich is invalid. What are you trying to do?

* Regarding the ON ERROR, I suggest using Mac's logic that I posted for you on the other thread.

Regards..... Moneo
I am actually adding the "1" to overcome some type of Y2K issue on another program. The Log files are generated from Synchronet BBS and the format is fixed. I cannot change it... Bummer eh... This other program reads in the SynchroNet log files (stored in MMDDYY.LOG) and extracts some data... However, the old program (authored in 1993 or so) thinks that after 1999 came 19100 then 19101 etc... so what the program does is read todays date and create the logfile name to find... well there is not a logfile called 1203106.LOG so I had to essentially rename it...

Good thing is, many thanks to you, I got the program running and working as I desired... However, I do like sharing my Hacks with others to help me learn a better approach. Any comments/questions appreciated.

The NEWLOG$ is only 7 character... I trim the first two from the YEAR$.

I will take a look at the ON ERROR logic... It has been a really long time for me doing this program thing and even years ago, I was not as concerned about the ERROR TRAP as I am now... I finally realized that I make a lot of mistakes... :-)
http://ultimate.4alexanders.com
telnet://ubbs2006.synchro.net
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Ok, I didn't realize that YEAR$ was only two digits. Fine, so you have some filenames like 1231100 or 1231101. That's OK, as long it's a valid filename and you know how to interpret the pseudo date within.

I have seen stranger stuff done to dates in an attempt to circumvent the Y2K problem. Most of these quick and dirty solutions will come back and bite you in the near future. Ok well.

Regards..... Moneo
Mike Alexander
Coder
Posts: 19
Joined: Fri Dec 01, 2006 8:27 pm
Location: Temecula, Ca
Contact:

Post by Mike Alexander »

moneo wrote:Ok, I didn't realize that YEAR$ was only two digits. Fine, so you have some filenames like 1231100 or 1231101. That's OK, as long it's a valid filename and you know how to interpret the pseudo date within.

I have seen stranger stuff done to dates in an attempt to circumvent the Y2K problem. Most of these quick and dirty solutions will come back and bite you in the near future. Ok well.

Regards..... Moneo
Yeah, but the code for the other thing is not mine, and not available to me. In fact, I can not even locate the author so my only solution was to bandaid the Y2K problem. I figure the Y2k Problem will not be an issue for 994 more years. At that time, It really won't matter. :lol:
http://ultimate.4alexanders.com
telnet://ubbs2006.synchro.net
Post Reply