ASPN ActiveState Programmer Network
  ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups | Web Services
SEARCH

Reference
ActiveTcl 8.5
ActiveTcl 8.5.4.0 User Guide
ActiveTcl 8.5.4.0 Documentation Index
Tutorial
Adding & Deleting members of a list
Adding new commands to Tcl - proc
Assigning values to variables
Associative Arrays
Building reusable libraries - packages and namespaces
Changing Working Directory - cd, pwd
Channel I/O: socket, fileevent, vwait
Child interpreters
Command line arguments and environment strings
Creating Commands - eval
Debugging & Errors - errorInfo errorCode catch error return
Evaluation & Substitutions 1: Grouping arguments with ""
Evaluation & Substitutions 2: Grouping arguments with {}
Evaluation & Substitutions 3: Grouping arguments with []
File Access 101
Information about Files - file, glob
Information about procs - info
Introduction
Invoking Subprocesses from Tcl - exec, open
Learning the existence of commands and variables ? - info
Leftovers - time, unset
Looping 101 - While loop
Looping 102 - For and incr
Modifying Strings - tolower, toupper, trim, format
Modularization - source
More channel I/O - fblocked & fconfigure
More command construction - format, list
More Debugging - trace
More Examples Of Regular Expressions
More list commands - lsearch, lsort, lrange
More On Arrays - Iterating and use in procedures
More Quoting Hell - Regular Expressions 102
Numeric Comparisons 101 - if
Regular Expressions 101
Results of a command - Math 101
Simple pattern matching - "globbing"
Simple Text Output
State of the interpreter - info
String comparisons - compare match first last wordend
String Subcommands - length index range
Substitution without evaluation - format, subst
Tcl Data Structures 101 - The list
Textual Comparison - switch
Time and Date - clock
Variable scope - global and upvar
Variations in proc arguments and return values

MyASPN >> Reference >> ActiveTcl 8.5 >> ActiveTcl 8.5.4.0 User Guide >> ActiveTcl 8.5.4.0 Documentation Index >> Tutorial
ActiveTcl 8.5 documentation

Time and Date - clock

The clock command provides access to the time and date functions in Tcl. Depending on the subcommands invoked, it can acquire the current time, or convert between different representations of time and date.

The clock command is a platform independent method of getting the display functionality of the unix date command, and provides access to the values returned by a unix gettime() call.

clock seconds
The clock seconds command returns the time in seconds since the epoch. The date of the epoch varies for different operating systems, thus this value is useful for comparison purposes, or as an input to the clock format command.

clock format clockValue ?-gmt boolean? ?-format string?
The format subcommand formats a clockvalue (as returned by clock clicks into a human readable string.

The -gmt switch takes a boolean as the second argument. If the boolean is 1 or True, then the time will be formatted as Greenwich Mean Time, otherwise, it will be formatted as local time.

The -format option controls what format the return will be in. The contents of the string argument to format has similar contents as the format statement (as discussed in lesson 19, 33 and 34). In addition, there are several more %* descriptors that can be used to describe the output.

These include:

  • %a . . . . Abbreviated weekday name (Mon, Tue, etc.)
  • %A . . . . Full weekday name (Monday, Tuesday, etc.)
  • %b . . . . Abbreviated month name (Jan, Feb, etc.)
  • %B . . . . Full month name (January, February, etc.)
  • %d. . . . . Day of month
  • %j . . . . . Julian day of year
  • %m . . . . Month number (01-12)
  • %y. . . . . Year in century
  • %Y . . . . Year with 4 digits

  • %H . . . . Hour (00-23)
  • %I . . . . . Hour (00-12)
  • %M . . . . Minutes (00-59)
  • %S . . . . . Seconds(00-59)
  • %p . . . . . PM or AM

  • %D . . . . Date as %m/%d/%y
  • %r. . . . . Time as %I:%M:%S %p
  • %R . . . . Time as %I:%M
  • %T . . . . Time as %I:%M:%S
  • %Z . . . . Time Zone Name
clock scan dateString
The scan subcommand converts a human readable string to a system clock value, as would be returned by clock seconds

The dateString argument contains strings in these forms:

time
A time of day in one of the formats shown below. Meridian may be AM, or PM, or a capitalization variant. If it is not specified, then the hour (hh) is interpreted as a 24 hour clock. Zone may be a three letter description of a time zone, EST, PDT, etc.
  • hh:mm:ss ?meridian? ?zone?
  • hhmm ?meridian? ?zone?
date
A date in one of the formats shown below.
  • mm/dd/yy
  • mm/dd
  • monthname dd, yy
  • monthname dd
  • dd monthname yy
  • dd monthname
  • day, dd monthname yy

Example

set systemTime [clock seconds]

puts "The time is: [clock format $systemTime -format %H:%M:%S]"
puts "The date is: [clock format $systemTime -format %D]"
puts [clock format $systemTime -format {Today is: %A, the %d of %B, %Y}]
puts "\n the default format for the time is: [clock format $systemTime]\n"

set halBirthBook "Jan 12, 1997"
set halBirthMovie "Jan 12, 1992"
set bookSeconds [clock scan $halBirthBook]
set movieSeconds [clock scan $halBirthMovie]

puts "The book and movie versions of '2001, A Space Oddysey' had a"
puts "discrepancy of [expr {$bookSeconds - $movieSeconds}] seconds in how"
puts "soon we would have sentient computers like the HAL 9000"

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState 2004 All rights reserved