Page 1 of 1 1
Topic Options
#50305 - 2000-06-21 02:39 PM Get yer date maths routines here.
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
I'd like to present a couple of date manipulation routines and accept your praise for the wonderful algorithms.

I'd like to, but I can't as they're not mine - I transliterated them from an article that I found on the web. Credit goes to Peter Baum whos document it is. The document can be found at:
http://www.capecod.net/~pbaum/date/date0.htm

If you use the scripts please keep the reference to Peters document as per his request. You should also refer to this page to see the limitations of the algorithms - suffice to say that they should be fine for business use, but if you want to use them to calculate to first Tuesday of the Jurassic forget it.

Now, the code. I keep these type of scripts in a "library" directory - you can of course copy them straight into a script and use them as subroutines if you prefer. There are two scripts, one to convert a calendar date into an internal integer representation (intdate.kix) and one to convert the internal representation back to the calendar format.

code:
;
; intdate.kix
; Library routine to convert a date from Calendar to internal format.
;
; The algorithm used here is taken directly from the following document:
; http://www.capecod.net/~pbaum/date/date0.htm
;
; Variables passed:
; rd_day IN Day of month (0-31)
; rd_month IN Month of year (1-12)
; rd_year IN Full year
; rd_internal OUT Internal representation
;
; Amendment history:
; Version 1.0 21 June 2000 Richard Howarth
;

GLOBAL $rd_day,$rd_month,$rd_year,$rd_internal
DIM $MyYear,$MyMonth

$MyYear=$rd_year
$MyMonth=$rd_month

if $MyMonth < 3
$MyMonth = $MyMonth + 12
$MyYear = $MyYear - 1
endif

$rd_internal = $rd_day + ( 153 * $MyMonth - 457 ) / 5 + 365 * $MyYear + $MyYear / 4 - $MyYear / 100 + $MyYear / 400 - 306

RETURN



code:
;
; extdate.kix
; Library routine to convert a date from internal to Calendar format.
;
; The algorithm used here is taken directly from the following document:
; http://www.capecod.net/~pbaum/date/date0.htm
;
; Variables passed:
; rd_day OUT Day of month (0-31)
; rd_month OUT Month of year (1-12)
; rd_year OUT Full year
; rd_internal IN Internal representation
;
; Amendment history:
; Version 1.0 21 June 2000 Richard Howarth
;

GLOBAL $rd_day,$rd_month,$rd_year,$rd_internal
DIM $MyZ,$MyH,$MyA,$MyB,$MyC

$MyZ=$rd_internal + 306
$MyH=100*$MyZ-25
$MyA=$MyH/3652425
$MyB=$MyA-$MyA/4
$rd_year=(100*$MyB+$MyH)/36525
$MyC=$MyB+$MyZ-365*$rd_year-$rd_year/4
$rd_month=(5*$MyC+456)/153
$rd_day=$MyC-(153*$rd_month-457)/5

if $rd_month > 12
$rd_year=$rd_year + 1
$rd_month = $rd_month - 12
endif

RETURN



As you can see the code is very small, and it executes very quickly.

Just to show I'm not completely clueless, here are a couple of scripts which test the routines - you don't need any of these to use the routines, they here as examples.

test_1.kix iterates the dates from 1 Jan 1900 'till 1 Jan 2001. It is a simple check that the results of the two routines complement each other.

test_2.kix is more of a real world application. It'll accept a date, parse it and validate it. You can then apply a modifier. A positive number will advance the date, a negative one will reverse it.

code:
;
; test_1.kix
; Test date conversion routines.
; Iterate the dates between 1 Jan 1900 and 1 Jan 2001
;
; Amendment history:
; Version 1.0 21 June 2000 Richard Howarth

BREAK ON

GLOBAL $rd_day,$rd_month,$rd_year,$rd_internal
DIM $Months[13]

$Months[1]="Jan"
$Months[2]="Feb"
$Months[3]="Mar"
$Months[4]="Apr"
$Months[5]="May"
$Months[6]="Jun"
$Months[7]="Jul"
$Months[8]="Aug"
$Months[9]="Sep"
$Months[10]="Oct"
$Months[11]="Nov"
$Months[12]="Dec"

; Start at 01 Jan 1900
$IntDate=693596

WHILE $rd_year < 2001

$rd_internal=$IntDate
CALL extdate.kix ; Convert to Calendar
$rd_internal=0 ; No Cheating!!
CALL intdate.kix ; Convert to internal

"Day $IntDate is $rd_day " $Months[$rd_month] " $rd_year " chr(13)
if $IntDate <> $rd_internal
? "Sanity check mismatch: IS $rd_internal, S/B $IntDate" ?
RETURN
endif

$IntDate = $IntDate + 1
LOOP


If you want to use the test_2.kix script you will also need the strtok.kix script which I use to parse the date.

code:
;
; test_2.kix
; Test date conversion routines.
; Input a date in DD/MM/YYYY format, validate is then perform math on it.
;
; Amendment History:
; Version 1.0 21 June 2000 Richard Howarth

BREAK ON

; Globals for date routines.
GLOBAL $rd_day,$rd_month,$rd_year,$rd_internal

; Globals for token routines.
GLOBAL $TokenFS,$TokenIN,$Token,$TokenERR

DIM $Date,$Modifier,$Months[13],$InternalDate

$Months[1]="Jan"
$Months[2]="Feb"
$Months[3]="Mar"
$Months[4]="Apr"
$Months[5]="May"
$Months[6]="Jun"
$Months[7]="Jul"
$Months[8]="Aug"
$Months[9]="Sep"
$Months[10]="Oct"
$Months[11]="Nov"
$Months[12]="Dec"

color w/n "Enter date as DD/MM/YYYY, or return to exit: " gets $Date
WHILE $Date <> ""
GOSUB "ValidateDate"
if $Date<> ""
$InternalDate=$rd_internal
? color y+/n "Enter modifier, or return to exit: " gets $Modifier
WHILE $Modifier <> ""
$InternalDate=$InternalDate + val($Modifier)
$rd_internal=$InternalDate
CALL extdate.kix
" Date is now $rd_day " $Months[$rd_month] " $rd_year" ?
color y+/n "Enter modifier, or return to exit: " gets $Modifier
LOOP
endif
? color w/n "Enter date as DD/MM/YYYY, or return to exit: " gets $Date
LOOP

RETURN

; ************************
; Validate Date subroutine
; ************************
:ValidateDate

$TokenFS="/"
$TokenIN=$Date
$Date=""

$TokenERR=1

CALL strtok.kix
if $TokenERR color r+/n " Error parsing day of month or invalid date." ? RETURN endif
$MyDay=val($Token)
$rd_day = $MyDay

CALL strtok.kix
if $TokenERR color r+/n " Error parsing month or invalid date." ? RETURN endif
$MyMonth=val($Token)
$rd_month = $MyMonth

CALL strtok.kix
if $TokenERR color r+/n " Error parsing year or invalid date." ? RETURN endif
$MyYear=val($Token)
$rd_year = $MyYear

; Convert to internal then back again. If the values match then the date is
; probably valid.
CALL intdate.kix
CALL extdate.kix
if "$MyDay/$MyMonth/$MyYear" = "$rd_day/$rd_month/$rd_year"
$Date="OK"
" Ok, date validates as $rd_day/$rd_month/$rd_year" ?
else
color r+/n " You entered $MyDay/$MyMonth/$MyYear, but my check returned $rd_day/$rd_month/$rd_year" ?
color r+/n " I interperet that as an invalid date." ?
endif
RETURN


code:
;
; strtok.kix
; Kix library function - tokenize string.
; Each time this routine is called it will set $Token to the next field in
; $TokenIN. The field seperator is defined in $TokenFS. $TokenIN is reset
; for subsequent calls.
;
; $TokenFS IN Field Seperator(s) (default is comma)
; $TokenIN IN String to be tokenized.
; $Token OUT Token found.
; $TokenERR OUT 0=valid token, 1=Out of tokens
;
; Amendment History:
; Version 1.0 16 June 2000 Richard Howarth

$_MyTokenReset="___TokenIN reset by tokenizer___"

$Token=""
$TokenERR=1

if $TokenFS = "" $TokenFS="," endif

if $TokenIN <> $_MyTokenReset
$_MyTokenIN=$TokenIN
$_MyTokenLen=len($TokenIN)
$_MyIndex=0
$TokenIN=$_MyTokenReset
if $_MyTokenIN = ""
$TokenERR=0
RETURN
endif
endif

if $_MyTokenIN = "" RETURN endif
if $_MyIndex > $_MyTokenLen RETURN endif

$TokenERR=0
while 1
$_MyIndex=$_MyIndex + 1
if $_MyIndex > $_MyTokenLen RETURN endif
$_T = substr($_MyTokenIN,$_MyIndex,1)
if instr($TokenFS,$_T) RETURN endif
$Token=$Token+$_T
loop


Richard Howarth.

[This message has been edited by rhowarth (edited 21 June 2000).]

[This message has been edited by rhowarth (edited 21 June 2000).]

Top
#50306 - 2000-06-21 03:55 PM Re: Get yer date maths routines here.
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Richard:

This stuff is awesome !

I just tried it with some date subtraction (days between) type stuff, and it works like a champ. These are important algorithms for any date hound !

In fact, there must be some voo-doo or black magic embedded in these formulas (457 ? 153 ? ). My brain hurts just looking at them !

Thanks a cuzillion !

Jochen:

I hate to say it, but I think you can throw in the towel now drat !

Shawn.



Top
#50307 - 2000-06-21 04:49 PM Re: Get yer date maths routines here.
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
no way brother,

i will built in richards stuff in my frame
that is working but hasn't got yet performance....

Gang, has anybody heard something from LD?!!?
He raised this *brrrrrrainsucking* handling date Issue!!!!

Jochen

_________________________



Top
#50308 - 2000-06-21 05:41 PM Re: Get yer date maths routines here.
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Richard,

WOW

Jochen

_________________________



Top
#50309 - 2001-04-02 08:36 AM Re: Get yer date maths routines here.
Anonymous
Unregistered


Combine these routines with the UDF capabilities of KIX2001 and what do you get?

DateMath() and DateSerial() User-Defined Functions.

Simply cut-and-paste them from: www.scriptlogic.com/support/kixtart/functionlib.asp

Top
#50310 - 2001-06-07 01:53 AM Re: Get yer date maths routines here.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Hello Richard,

These are some very nice routines. However, could you possibly help me and perhaps others by posting a few samples to the board that are a little more clear for usage where you want to get a date difference of a file. Shawn, Bryce, JPOLS and some others already seem to know how to alter this code to their liking... I would like to learn.

i.e. I want to find out if a file is older then a specific period of time. Meaning 10, 30, 60, etc.. days old.

The date format returned by the files are similar to: 2001/05/28 18:00:00 but code that could decipher for YYYY/DD/MM, MM/DD/YYYY etc... would be great.

Bryce and others have come up with these http://kixtart.org/board/Forum2/HTML/000334.html

adamharris posted this (which works), but perhaps yours would be more bullet proof using the Math routines here. http://kixtart.org/board/Forum2/HTML/000133.html

Sample of what I'm looking for an answer for

code:

$common = ReadValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders","Common AppData")
$defdate = GetFileTime("$Common\Symantec\Norton AntiVirus Corporate Edition\7.5\I2_LDVP.VDB\VD0D7A02.VDB\naveng32.dll")
$navhome = ReadValue("HKEY_LOCAL_MACHINE\SOFTWARE\INTEL\LANDesk\VirusProtect6\CurrentVersion", "Home Directory")
$filename = "$navhome\vptray.exe"
$datedif = 0
IF (Exist($filename) = 1)
$version = GetFileVersion($filename,"Fileversion")
$size = GetFileSize($filename)
$defdate = GetFileTime($filename)
; ? "$filename version is: "+$version
; ? "$filename size is: "+$size+" Bytes"
; ? "Virus definition date is: $defdate"

$datedif = (todays date - $defdate)

IF $datedif > 30
; take some action
ELSE
; no action required
ENDIF


TIA for any and all assistance and training from those more learned in the art...


------------------
NTDOC...
The dark side is quicker, easier, more seductive.
If you choose the quick and easy path, you will become an agent of evil.

[This message has been edited by NTDOC (edited 07 June 2001).]

Top
#50311 - 2003-04-04 11:21 PM Re: Get yer date maths routines here.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Richard,

Any update yet? Still waiting [Razz]

I've been sitting here at my desktop pressing the F5 key now for 2 YEARS, still don't see an update.

[ 04. April 2003, 23:25: Message edited by: NTDOC ]

Top
#50312 - 2003-04-05 05:17 AM Re: Get yer date maths routines here.
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
  • SerialDate()
  • SerialTime()
  • DateMath()
  • TimeDiff()
  • DeltaTime()
  • DateTimeMath()
_________________________
There are two types of vessels, submarines and targets.

Top
#50313 - 2003-04-05 09:35 PM Re: Get yer date maths routines here.
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
and finally DateCalc() (No dependencies)
_________________________



Top
#50314 - 2003-04-06 11:50 AM Re: Get yer date maths routines here.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Jens and Jochen, thanks for the links. Not exactly what I meant, but I guess it will do.

Was meaning maybe something a little more easier for beginners to use/learn i.e. laymans terms.

Hey guys... seems we lost one of the UDFs. Did the author delete it or what?

SerialDate() - Convert dates to numbers (and back) for the purpose of performing date
http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=12;t=000089;p=

SERIALTIME() - Convert time to numbers (and back) for the purpose of performing time
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000039

DateTimeMath() - Calculates the difference between two date-time strings
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000326

DateTimeDiff() - Calculates the time difference between two date-times-groups
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000374

TimeDiff() - Calculates the time difference between two times
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000040

DeltaTime() - Calculates time differences up to 23:59:59:999
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000011

DateCalc() - Calculates Days between or returns calculated Date (Days as modifier)
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000076

DateMath()
FYI You have requested a topic that does not exist!
http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000102#000000

Top
#50315 - 2003-04-06 05:20 PM Re: Get yer date maths routines here.
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
Actually, DateMath() is available here: http://www.scriptlogic.com/kixtart/FunctionLibrary_ViewFunction.aspx?ID=DateMath
_________________________
There are two types of vessels, submarines and targets.

Top
#50316 - 2003-04-07 09:55 AM Re: Get yer date maths routines here.
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
While I was pondering Ent-like over an appropriate response these durned whipper-snappers came up with more user friendly UDFs than I could shake a wizened stick-like at.

Spookily all those litle jobs that need doing around the house get delayed in a similar fashion.

Top
#50317 - 2003-04-07 10:16 AM Re: Get yer date maths routines here.
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
We Hobbits are way too fast for ya , eh ?
_________________________



Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 557 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.06 seconds in which 0.023 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org