# Date and Time Functions

DateList[] | give the current local date and time in the form |

DateList[TimeZone->z] | give the current date and time in time zone z |

$TimeZone | give the time zone assumed by your computer system |

In[1]:= |

Out[1]= |

The *Mathematica* DateList 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 variable $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 .

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

AbsoluteTime[] | total number of seconds since the beginning of January 1, 1900 |

SessionTime[] | total number of seconds elapsed since the beginning of your current Mathematica session |

TimeUsed[] | total number of seconds of CPU time used in your current Mathematica session |

$TimeUnit | the minimum time interval recorded on your computer system |

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 either about or of a second.

Pause[n] | pause for at least n seconds |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

AbsoluteTime[date] | convert from date to absolute time |

DateList[time] | convert from absolute time to date |

Converting between dates and absolute times.

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]= |

DateList["string"] | convert a date string to a date list |

DateList[{"string",{"e_{1}","e_{2}",...}}] | give the date list obtained by extracting elements from |

Converting from different date formats.

You can use DateList["string"] to convert a date string into a date list, as long as the date format is sufficiently unambiguous.

In[9]:= |

Out[9]= |

For more control of the conversion, you can specify the order and type of date elements appearing in the string. The elements can be strings like , , , , , , , , , or .

In[10]:= |

Out[10]= |

If the date element delimiters contain letters or digits, these must also be specified as part of the date elements.

In[11]:= |

Out[11]= |

DateString[] | give a string representing current local date and time |

DateString[datespec,elems] | give elements elems of date and time given by datespec |

Converting to different date formats.

DateString is used to give a nice string representation of a date and time. The exact output format can be specified from a long list of date elements, such as , , , etc.

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

DatePattern[elems] | string pattern matching a date with the given elements |

Extracting dates from a string.

You can use DatePattern[elems] as a string pattern in string matching functions. The date elements are the same as used in DateList, although the default date element delimiters are restricted to the , , , or characters. Other delimiters can be given explicitly in the list of date elements.

In[14]:= |

Out[14]= |

In[15]:= |

Out[15]= |

DateListPlot[list] | generate a plot from a list of data with date coordinates |

DateListPlot[list,datespec] | generate a plot from a list of data with dates specified by datespec |

DateListLogPlot[list] | generate a linear-log plot from a list of data with date coordinates |

DateListLogPlot[list,datespec] | generate a linear-log plot from a list of data with dates specified by datespec |

Plotting data with date coordinates.

DateListPlot can be used to plot data with date or time horizontal coordinates. Dates can be lists, strings, or absolute times as with DateList, DateString, and AbsoluteTime. A date specification datespec can be given to associate dates with data given as . DateListLogPlot allows you to plot the data with a logarithmic vertical scale.

In[16]:= |

In[17]:= |

Out[17]= |

In[18]:= |

Out[18]= |

In[19]:= |

Out[19]= |

Timing[expr] | evaluate expr, and return a list of the CPU time needed, together with the result obtained |

AbsoluteTiming[expr] | evaluate expr, giving the absolute time taken |

Timing *Mathematica* operations.

Timing allows you to measure the CPU time associated with the evaluation of a single *Mathematica* expression. Timing corresponds to the increase in TimeUsed. 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.

AbsoluteTiming allows you to measure absolute total elapsed time. You should realize, however, that the time reported for a particular calculation by both AbsoluteTiming and Timing 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 reevaluating 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.

In[20]:= |

Out[20]= |

*Mathematica*has built internal tables for factorial functions, and the calculation takes no measurable CPU time.

In[21]:= |

Out[21]= |

In[22]:= |

Out[22]= |

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 could in fact be as much as $TimeUnit.

TimeConstrained[expr,t] | try to evaluate expr, aborting the calculation after t seconds |

TimeConstrained[expr,t,failexpr] | return failexpr if the time constraint is not met |

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.