• Aucun résultat trouvé

Using Static Methods

Dans le document Copyright Copyright (Page 186-191)

By now, you know that PowerShell stores information in objects, and objects always have a type.

You know that simple text is stored in objects of type System.String and that a date, for example, is stored in an object of type System.DateTime. You also know by now that each .NET object has a GetType() method with a FullName property which tells you the name of the type this object was derived from:

$date = Get-Date

$date.GetType().FullName System.DateTime

Every type can have its own set of private members called "static" members. Simply specify a type in square brackets, then pipe it to Get-Member and use the -static parameter to see the static members of a type.

[System.DateTime] | Get-Member -static -memberType *method TypeName: System.DateTime

Name MemberType Definition ----

---Compare Method static System.Int32 ---Compare(

DateTime t1, DateTime t2) DaysInMonth Method static System.Int32 DaysInMonth(

Int32 year, Int32 month) Equals Method static System.Boolean Equals(

DateTime t1, DateTime t2), static Sys...

FromBinary Method static System.DateTime FromBinary(

Int64 dateData)

FromFileTime Method static System.DateTime

FromFileTime(Int64 fileTime) FromFileTimeUtc Method static System.DateTime

FromOADate Method static System.DateTime FromOADate(

op_GreaterThanOrEqual Method static System.Boolean

op_GreaterThanOrEqual(DateTime t1,

op_LessThanOrEqual Method static System.Boolean

op_LessThanOrEqual(DateTime t1,

There are a lot of method names starting with "op_," with "op"

standing for "operator." These are methods called internally whenever you use this data type with an operator.

op_GreaterThanOrEqual is the method that does the internal work when you use the PowerShell comparison operator "-ge" with date values.

The System.DateTime class supplies you with a bunch of important date and time methods. For example, to convert a date string into a real DateTime object and use the current locale, use Parse():

[System.DateTime]::Parse("March 12, 1999") Friday, March 12, 1999 00:00:00

You could easily find out whether a certain year is a leap year:

[System.DateTime]::isLeapYear(2010) False

for ($x=2000; $x -lt 2010; $x++) {

if( [System.DateTime]::isLeapYear($x) ) { "$x is a leap year!" } }

2000 is a leap year!

2004 is a leap year!

2008 is a leap year!

Or you'd like to tell your children with absolute precision how much time will elapse before they get their Christmas gifts:

[DateTime]"12/24/2007 18:00" - [DateTime]::now

Days : 74

Hours : 6

Minutes : 28

Seconds : 49

Milliseconds : 215

Ticks : 64169292156000 TotalDays : 74.2700140694444 TotalHours : 1782,48033766667 TotalMinutes : 106948,82026 TotalSeconds : 6416929,2156 TotalMilliseconds : 6416929215,6

Two dates are being subtracted from each other here so you now know what happened during this operation:

• The first time indication is actually text. For it to become a DateTime object, you must specify the desired object type in square brackets. Important: Converting a String to a DateTime this way always uses the US locale. To convert a String to a DateTime using your current locale, use the Parse() method as shown a couple of moments ago!

• The second time comes from the Now static property, which returns the current time as DateTime object. This is the same as calling the Get-Date cmdlet (which you'd then need to put in parenthesis because you wouldn't want to subtract the Get-Date cmdlet but rather the result of the Get-Date cmdlet).

• The two timestamps are subtracted from each other using the subtraction operator ("-"). This was possible because the DateTime class defined the op_Subtraction() static method, which is needed for this operator.

Of course, you could have called the static method yourself and received the same result:

[DateTime]::op_Subtraction("12/24/2007 18:00", [DateTime]::Now)

Now it's your turn. In the System.Math class, you'll find a lot of useful mathematical methods. Try to put some of these methods to work.

Function Description Example

Abs Returns the absolute value of a

specified number (without signs). [Math]::Abs(-5)

Acos Returns the angle whose cosine is the

specified number. [Math]::Acos(0.6)

Asin Returns the angle whose sine is the

specified number. [Math]::Asin(0.6)

Atan Returns the angle whose tangent is the

specified number. [Math]::Atan(90)

Atan2 Returns the angle whose tangent is the

quotient of two specified numbers. [Math]::Atan2(90, 15)

BigMul Calculates the complete product of two 32-bit numbers.

[Math]::BigMul(1gb, 6)

Ceiling Returns the smallest integer greater

than or equal to the specified number. [Math]::Ceiling(5.7)

Cos Returns the cosine of the specified

angle. [Math]::Cos(90)

Cosh Returns the hyperbolic cosine of the

specified angle. [Math]::Cosh(90)

DivRem

Calculates the quotient of two numbers and returns the remainder in an output parameter.

$a = 0

[Math]::DivRem(10,3 ,[ref]$a)

$a

Exp Returns the specified power of e

(2.7182818). [Math]::Exp(12)

Floor Returns the largest integer less than or

equal to the specified number. [Math]::Floor(5.7)

IEEERemainde

r Returns the remainder of division of

two specified numbers. [Math]::IEEERemaind er(5,2)

Log Returns the natural logarithm of the

specified number. [Math]::Log(1)

Log10 Returns the base 10 logarithm of the

specified number. [Math]::Log10(6)

Max Returns the larger of two specified

numbers. [Math]::Max(-5, 12)

Min Returns the smaller of two specified

numbers. [Math]::Min(-5, 12)

Pow Returns a specified number raised to

the specified power. [Math]::Pow(6,2)

Round

Rounds a value to the nearest integer or to the specified number of decimal

places. [Math]::Round(5.51)

Sign Returns a value indicating the sign of a

number. [Math]::Sign(-12)

Sin Returns the sine of the specified angle. [Math]::Sin(90)

Sinh Returns the hyperbolic sine of the [Math]::Sinh(90)

specified angle.

Sqrt Returns the square root of a specified

number. [Math]::Sqrt(64)

Tan Returns the tangent of the specified

angle. [Math]::Tan(45)

Tanh Returns the hyperbolic tangent of the

specified angle. [Math]::Tanh(45)

Truncate Calculates the integral part of a

number. [Math]::Truncate(5.6

7)

Table 6.5: Mathematical functions from the [Math] library

Dans le document Copyright Copyright (Page 186-191)