Quartus® II Tcl Example: Date Time Stamp

author-image

By

This example shows how to create date time stamps with Tcl. You can use a date time stamp in a scripted design flow to record exactly when the script ran. Writing the date time stamp into your design files provides on-chip storage of when the design was compiled.

You can use Tcl commands to generate a custom-formatted date time string with just the date and time elements you want. Use the Tcl command clock seconds to return the current time and clock format to generate a custom-formatted date time string. Refer to the Date and Time Formatting web page for more information about formatting date time strings. It includes examples and a table of formatting keywords.

Here are two examples of different formatting options for a date time string.

Example 1

The following command generates a formatted date time string with the following elements, in this order:

  1. Four digit year
  2. Two digit month (01-12)
  3. Two digit day (01-31)
  4. Two digit hour in 24-hour format (00-23)
  5. Two digit minute (00-59)
  6. Two digit seconds (00-59)
clock format [clock seconds] -format {%Y %m %d %H %M %S}

That command generates a string like this:

2005 01 10 15 16 55

Example 2

The second example generates a formatted date time string with the following elements, in this order:

  1. Abbreviated month name followed by a period
  2. Two digit day of the month followed by a comma
  3. Four digit year
  4. The time in HH:MM:SS format
  5. An AM/PM indicator
clock format [clock seconds] -format {%b. %d, %Y %I:%M:%S %p}

That command generates a string like this:

Jan. 10, 2005 03:31:20 PM

Converting the Date Time String

You usually have to convert the ASCII date time string to another format (such as hexadecimal or binary) to store it in a register bank or memory. Here are two examples of format conversion.

Decimal to Hexadecimal

If you use date time elements that generate only numeric values, you can treat them as decimal numbers for the purpose of conversion. The following command generates a date stamp of the day number in the year (001 - 366), followed by the hour in 24-hour format, then the minute.

set str [clock format [clock seconds] -format {%j%H%M}]
set out [format "%X" $str]

That command generates a string like this in the variable out:

17D40F

ASCII to Hexadecimal

The following command generates a date time string and converts it to a packed hexadecimal string stored in the variable out. Each pair of hexadecimal digits is the hexadecimal code for the ASCII character.

set str [clock format [clock seconds] -format {%b. %d, %Y %I:%M:%S %p}]
binary scan $str "H*" out

That command generates a string like this in the variable out:

4a616e2e2031302c20323030352030333a33363a303520504d