› Clovertech Forums › Read Only Archives › Cloverleaf › Cloverleaf › Age Calculation in tcl
Any help appreciated.
Dave
set birthYear [lindex [split $birthYear /] 2]
set yrsOld [expr ($currentYear – $birthYear)]
######################################################################
# Name: XltVPBulkAge
# Purpose: Get age in years from bulk data file
# UPoC type: xltp
# Args: none
# Notes: All data is presented through special variables. The initial
# upvar in this proc provides access to the required variables.
#
# This proc style only works when called from a code fragment
# within an XLT.
#
proc XltVPBulkAge {} {
upvar xlateId xlateId
xlateInList xlateInList
xlateInTypes xlateInTypes
xlateInVals xlateInVals
xlateOutList xlateOutList
xlateOutTypes xlateOutTypes
xlateOutVals xlateOutVals
set byr [lindex [split $xlateInVals /] 2]
set byr [lindex $byr 0]
set nyr expr[clock format [clock seconds] -format “%Y”]
set age [expr “$nyr – $byr”]
set xlateOutVals $age
}
errorInfo:
can’t use non-numeric string as operand of “-“
while executing
“expr {$nyr – $byr}”
(procedure “XltVPBulkAge” line 6)
invoked from within
“XltVPBulkAge”
Try this
set inp [lindex $xlateInVals 0]
# If not a valid date/time the clock scan command fails
if {[catch {clock format [clock scan $inp] -format %Y} birthYear]} {
# Issue an error message and leave age blank
echo nnReceived invalid birthdatenn
set age {}
} else {
# Valid birthdate
set nowYear [clock format [clock seconds] -format %Y]
set age [expr $nowYear – $birthYear]
}
set xlateOutVals
set nyr expr[clock format [clock seconds] -format “%Y”]
Should be:
set nyr [clock format [clock seconds] -format “%Y”]
set inp [lindex $xlateInVals 0]
# If not a valid date/time the clock scan command fails
if {[catch {clock format [clock scan $inp] -format %Y} birthYear]} {
# Issue an error message and leave age blank
echo nnReceived invalid birthdatenn
set age {}
} else {
# Valid birthdate
set nowYear [clock format [clock seconds] -format %Y]
set age [expr $nowYear – $birthYear]
}
#set xlateOutVals
set xlateOutVals
1 3]]
echo pt age = $age
pt age = -188313
Does this calculate the days? Why can’t I get the $age to stink down to position 1-3 only?
Hello, David
Please note that your code snippet as shown is missing a step. You cannot just subtract the birth year from the current year to obtain the age in years. You also have to account for whether the person has had a birthday yet this year.
With the example you have given (3/4/1953), the code would return an age of 65, which is wrong because the person has not reached a birthday yet this year and is still age 64.
Therefore you must also compare the day of birth (3/4) to the current day and subtract 1 from the age if the day of birth is greater (birthday not celebrated yet this year).
Isn’t the proc ‘gc_calculateAge’ part of the standard Cloverleaf installation?
I’m never sure whether we got stuff from our Dutch supplier, or whether it’s part of standard Cloverleaf.
Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands
Robert,
To my knowledge, this proc is not part of the standard Cloverleaf installation. You must have a supplemental library.
Rather than trying to figure out if the current date is before or after the birthday, why not take the current time in seconds since the epoch ([clock seconds]), subtract the birthdate in seconds since the epoch ([clock scan $birthdate], which may be negative if the patient was born before the epoch), and divide the result by the number of seconds in a year?
Actually that wouldn’t work because some years are longer than others. Maybe subtracting the years and figure out if the patient has reached the birthdate this year is best.
gc_calculateAge is indeed part of a package supplied only to the Dutch market. Can’t share them with the world.
Googled it and found this proc, which should give the age in years as well:
# yearsBetween —
#
# Returns the years elapsed between two given timestamps
#
# Arguments:
# oldertime
# newertime
proc yearsBetween {oldertime newertime} {
set i 1
while {$oldertime < [clock scan "$i year ago" -base $newertime]} {
incr i
}
return [expr {$i – 1}]
}
Example:
set date1 [clock scan 1987-01-18]
set date2 [clock seconds]
puts [yearsBetween $date1 $date2]
Zuyderland Medisch Centrum; Heerlen/Sittard; The Netherlands