Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a Date and Time something useable.

Status
Not open for further replies.

Ati2ude

Programmer
Dec 11, 2001
79
US
I am dealing with a date in the follwoing format "YYYYMMDDHHmmss". Does anyone know of a way that I can get the CLOCK function to recognize that format and convert the date to seconds? Here is what I have tried.

set fmat {%Y%m%d%%H%M%S}
set msgTime [clock format $pv1_45 -format $fmat ]
set msgTime [clock scan $msgTime]
set curTime [clock seconds]
set difference [expr {$curTime - $msgTime}]

It does not like this statement:
set msgTime [clock format $pv1_45 -format $fmat ]

Any pointers would be great, thanks in advance.

Ati2ude
 
how is pv1_45 set? what is the value?

_________________
Bob Rashkin
rrashkin@csc.com
 
I have a pipe delimited string that contains the value in field 45. So the variable pv1_45 is set using this statement:

set pv1_45 [lindex $pv1_fields 45]

the current value is 20050610114002
YYYYMMDDHHMMSS

Thanks in advance for any pointers.

Brian
 
Here is what I have thus far, I am sure it can be cleaned up greatly if I knew some function to handle my string date.

Thanks again BONG for any pointers.


#now that we know the date is not null and the message #qualifies to be here we need to do some work on the
#date we have in the message.
#first we must convert the message date to something tcl #will recognize

set myYear [concat [string index $pv1_45 0][string index $pv1_45 1][string index $pv1_45 2][string index $pv1_45 3]]

set myMonth [concat [string index $pv1_45 4][string index $pv1_45 5]]

set myDay [concat [string index $pv1_45 6][string index $pv1_45 7]]

set msgDate [concat $myMonth/$myDay/$myYear]

#now start the comparison
set msgTime [clock scan $msgDate]
set curTime [clock seconds]
set difference [expr {$curTime - $msgTime}]

if {$debug == 1} {
echo "Start DEBUG DATA****************************************************"
echo "Message Date: $msgTime"
echo "Current Time: $curTime"
set fmt "There are %j days difference in the dates."
puts [clock format $difference -format $fmt]
}
set fmt "%j"
set curDif [clock format $difference -format $fmt]

if {$curDif > 5} {
if {$debug == 1} {
echo "The Message was KILLED because of the difference."
echo "END DEBUG DATA****************************************************"
}
return "{KILL $mh}"
} else {
if {$debug == 1} {
echo "The Message was CONTINUED because of the difference."
echo "END DEBUG DATA****************************************************"
}
return "{CONTINUE $mh}"
}
 
OK then. One problem right away is perhaps a confusion of clock scan and clock format

clock format clockValue ?-format string? ?-gmt boolean?

Converts an integer time value, typically returned by clock seconds, clock scan, or the atime, mtime, or ctime options of the file command, to human-readable form

clock scan dateString ?-base clockVal? ?-gmt boolean?

Convert dateString to an integer clock value (see clock seconds).

So your string, 20050610114002, is already what you would want the result of format to be.

The first thing you want to do, I think, is turn your date string into something that clock scan can read,
like: [red]20050610 11:40:02[/red].
If your string is always 14 characters in the same format, you can do it any number of ways:
set date [string range $pv1_45 0 7]
set hour [string range $pv1_45 8 9]
set min [string range $pv1_45 10 11]
set sec [string range $pv1_45 12 13]
set msgTime "$date $hour:$min:$sec"

for example. Then clock scan $msgTime gives [red]1118425202[/red] as it should.

_________________
Bob Rashkin
rrashkin@csc.com
 
I utilized your suggestion and am now getting the result I am looking for. Thanks again for your assistance here. TCL is not my primary language so I end up stomping my way through the language and often over look built in functions that would make my life easier.

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top