Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
137
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

HANDLING DATES 567

Console.WriteLine(TS.Days)

Console.WriteLine(TS.TotalDays)

The last three statements printed the following values in the Output window:

3.08:09:12.9970000 -3 -3.33973376157407

The Duration of the span is 3 days, 8 hours, 9 minutes, 12 seconds, and 997 milliseconds. The Days method returns the number of days in the TimeSpan as a whole number. The TotalDays method returns the same difference as a number of days with a fractional part. If you multiply the fractional part (0.33973376157407) by 24, you’ll get 8.153610277. If you multiply the fractional part of this number by 60, you’ll get 9.2166166. Finally, you can multiply the new fractional part by 60 to get the number of seconds: 12.996999 (a rounding error was introduced in the calculations, but this was expected). You will get the same results by calling the TimeSpan object’s Hour, Minute, and Second properties.

CompareTo

This method compares the current instance of the TimeSpan with another TimeSpan object; its syntax is

TS.CompareTo(TS1)

where TS is a properly initialized TimeSpan object. The CompareTo method returns 0 if they’re equal, –1 if the current instance is longer, and 1 if the TimeSpan object passed as argument is longer.

Equals

This method returns a True/False value that indicates whether two TimeSpan objects represent the same interval. The syntax of the Equals method is

TS.Equals(TS1)

Negate

This method negates the current TimeSpan instance. A positive TimeSpan (which will yield a future date when added to the current date) becomes negative (which will yield a past date when added to the current date).

VB.NET at Work: Timing Operations

The TimeSpan class has a fine granularity, which makes it ideal for timing operations. To time an operation—a sequence of statements—you measure the time right before they start executing and right after they have executed. Then you take the difference, which is the time it took for the operation to complete. If the operation takes a very short time (a time comparable to the time it takes to measure the time), you must execute the statements repeatedly and divide the total duration by the number of iterations. In general, the time spent executing the statements that keep track of the time should be negligible compared to the time spent executing the actual statements.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com