Converting Timestamps

The following Tcl procedure converts a 14-place CMS date to a Unix timestamp:

# $Id: nps2unixDate.tcl,v 1.5 2001/12/19 16:09:28 benjamin Exp $
#
# nps2unixDate
#
# Purpose:
#   Converts a CMS timestring (e.g. 20060101123000) to 
#   a Unix timestamp (seconds since 01.01.1970).
#
# Parameters:
#   CMS timestring (format: YYYYMMDDHHmmSS)

proc nps2unixDate {timeStamp} {
    # String must contain 14 digits.
    # regular expressions rule!
    if {[regexp "^(1|2)(0|9)\\d\\d(0|1)\\d(0|1|2|3)\\d(0|1|2)\\d\[0-5\]\\d\[0-5\]\\d\$" $timeStamp] == "0"} {
        error "nps2unixDate: Parameter is not a valid CMS timestamp"
    }
    set year   [string range $timeStamp 0 3]
    set month  [string range $timeStamp 4 5]
    set day    [string range $timeStamp 6 7]
    set hour   [string range $timeStamp 8 9]
    set minute [string range $timeStamp 10 11]
    set second [string range $timeStamp 12 13]
    set unixTime [clock scan "$month/$day/$year $hour:$minute:$second" -gmt true]
    return $unixTime
}

The inverse conversion can be performed using the following procedure:

# $Id: unix2npsDate.tcl,v 1.3 2001/12/19 16:08:28 benjamin Exp $
#
# unix2npsDate
#
# Purpose:
#   Converts a Unix timestamp (seconds since 01.01.1970) to
#   a CMS timestring (e.g. 20060519143000).
#
# Parameters:
#   Unix timestring

proc unix2npsDate {timeStamp} {
    # The string must not contain any non-digits
    if {[regexp "\[^\\d\]" $timeStamp]} {
        error "unix2npsDate: Parameter is not a valid unix timestamp"
    }
    # NOTE
    # The string must not exceed 2.000.000.000 which is 
    # Wed, May 18 05:33:20 MET DST 2033
    # Actually, tcl is able to scan timestamps beyond this date
    if {$timeStamp > "2000000000"} {
        error "unix2npsDate: Parameter is too large"
    }
    return [clock format $timeStamp -format "%Y%m%d%H%M%S"]
}

Please save both scripts to a Tcl file. Open a Tcl shell, connect to the Content Manager, and read the Tcl file using source. You can now use the procedures. Example:

nps2unixDate 20110519130500