Wolfram Research, Inc.

2.13.3 Date and Time Functions

Finding the date and time.

This gives the current date and time.

In[1]:= Date[ ]

Out[1]=

The Mathematica Date function returns whatever your computer system gives as the current date and time. It assumes that any corrections for daylight saving time and so on have already been done by your computer system. In addition, it assumes that your computer system has been set for the appropriate time zone.

The function TimeZone[ ] returns the current time zone assumed by your computer system. The time zone is given as the number of hours which must be added to Greenwich mean time (GMT) to obtain the correct local time. Thus, for example, U.S. eastern standard time (EST) corresponds to time zone . Note that daylight saving time corrections must be included in the time zone, so U.S. eastern daylight time (EDT) corresponds to time zone .

This gives the current time zone assumed by your computer system.

In[2]:= TimeZone[ ]

Out[2]=

This gives the current date and time in time zone +9, the time zone for Japan.

In[3]:= Date[9]

Out[3]=

Time functions.

You should realize that on any computer system, there is a certain "granularity" in the times that can be measured. This granularity is given as the value of the global variable $TimeUnit. Typically it is about of a second.

Pausing during a calculation.

This gives various time functions.

In[4]:= {AbsoluteTime[ ], SessionTime[ ], TimeUsed[ ]}

Out[4]=

This pauses for 10 seconds, then re-evaluates the time functions. Note that TimeUsed[ ] is not affected by the pause.

In[5]:= Pause[10]; {AbsoluteTime[ ], SessionTime[ ],

TimeUsed[ ]}

Out[5]=

Converting between dates and absolute times.

This sets d to be the current date.

In[6]:= d = Date[ ]

Out[6]=

This adds one month to the current date.

In[7]:= Date[ ] + {0, 1, 0, 0, 0, 0}

Out[7]=

This gives the number of seconds in the additional month.

In[8]:= FromDate[%] - FromDate[d]

Out[8]=

Timing Mathematica operations.

Timing allows you to measure the CPU time, corresponding to the increase in TimeUsed, associated with the evaluation of a single Mathematica expression. Note that only CPU time associated with the actual evaluation of the expression within the Mathematica kernel is included. The time needed to format the expression for output, and any time associated with external programs, is not included.

You should realize that the time reported by Timing for a particular calculation depends on many factors.

First, the time depends in detail on the computer system you are using. It depends not only on instruction times, but also on memory caching, as well as on the details of the optimization done in compiling the parts of the internal code of Mathematica used in the calculation.

The time also depends on the precise state of your Mathematica session when the calculation was done. Many of the internal optimizations used by Mathematica depend on details of preceding calculations. For example, Mathematica often uses previous results it has obtained, and avoids unnecessarily re-evaluating expressions. In addition, some Mathematica functions build internal tables when they are first called in a particular way, so that if they are called in that way again, they run much faster. For all of these kinds of reasons, it is often the case that a particular calculation may not take the same amount of time if you run it at different points in the same Mathematica session.

This gives the CPU time needed for the calculation. The semicolon causes the result of the calculation to be given as Null.

In[9]:= Timing[1000!;]

Out[9]=

Now Mathematica has built internal tables for factorial functions, and the calculation takes much less time.

In[10]:= Timing[1000!;]

Out[10]=

Note that the results you get from Timing are only accurate to the timing granularity $TimeUnit of your computer system. Thus, for example, a timing reported as 0 could in fact be as much as $TimeUnit.

Time-constrained calculation.

When you use Mathematica interactively, it is quite common to try doing a calculation, but to abort the calculation if it seems to be taking too long. You can emulate this behavior inside a program by using TimeConstrained. TimeConstrained tries to evaluate a particular expression for a specified amount of time. If it does not succeed, then it aborts the evaluation, and returns either $Aborted, or an expression you specify.

You can use TimeConstrained, for example, to have Mathematica try a particular approach to a problem for a certain amount of time, and then to switch to another approach if the first one has not yet succeeded. You should realize however that TimeConstrained may overrun the time you specify if Mathematica cannot be interrupted during a particular part of a calculation. In addition, you should realize that because different computer systems run at different speeds, programs that use TimeConstrained will often give different results on different systems.