Showing posts with label System.TImespan. Show all posts
Showing posts with label System.TImespan. Show all posts

Sunday, 5 January 2014

Show-TimeSpanFormatting.ps1

<# 
.SYNOPSIS
This script demonstrates formatting System.TimeSpan objects
Using PowerShell
.DESCRIPTION
This scipt re-writes some MSDN Samples that demostarate
timespan formatting - the original article lacks PowerShell
Examples. And sadly, the MSDN page no longer accepts community
additions.

.NOTES
File Name : Show-TimeSpanFormatting
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 3.0
.LINK
This script posted to:
http://pshscripts.blogspot.co.uk/2014/01/show-timespanformattingps1.html
Reference MSDN Page
http://msdn.microsoft.com/en-us/library/ee372286%28v=vs.110%29.aspx
.EXAMPLE
Psh> .\Show-TimeSpanFormatting.ps1
Example 1
Time of Travel: 1.12:24:02
Time of Travel: 1.12:24:02
Example 2
Converted '1.03:14:56.1667' to 1.03:14:56.1667000
Converted '1.03:14:56.1667' to 1.03:14:56.1667000
Example 3
07:45:16 - 18:12:38 = -10:27:22
07:45:16 + 18:12:38 = 1.01:57:54
00:01:14.3650000 + 00:00:00.2143756 = 00:01:14.5793756
Example 4
7:45:16 - 18:12:38 = -10:27:22
7:45:16 + 18:12:38 = 1:1:57:54
0:01:14.036 + 0:00:00.2143756 = 0:01:14.2503756
Example 5
0:07:45:16.0000000 - 0:18:12:38.0000000 = -0:10:27:22.0000000
0:07:45:16,0000000 + 0:18:12:38,0000000 = 1:01:57:54,0000000
0:00:01:14.0360000 + 0:00:00:00.2143756 = 0:00:01:14.2503756
#>

# Show Time Span Format Strings

# Example 1 - use both tostring() and -f operators
"Example 1"
# Create Timespan object
$duration = New-Object System.TimeSpan 1, 12, 23, 62

# Now output using both tostring() and -f
"Time of Travel: " + $duration.ToString("c")
"Time of Travel: {0:c}" -f $duration

# Example 2
# Demonstrate the use of ParseExact and TryParseExact
"Example 2"

$value = "1.03:14:56.1667"
$interval = New-Object System.TimeSpan

Try {
$interval = [System.TimeSpan]::ParseExact($value, "c", $null)
"Converted '{0}' to {1}" -f $value, $interval
}
Catch [System.FormatException] {"{0}: Bad Format" -f $value}
Catch [System.OverflowException] {"{0}: Out of Range" -f $value }

If ([System.TimeSpan]::TryParseExact($value, "c", $null, [ref] $interval)) {
"Converted '{0}' to {1}" -f $value, $interval
}
Else {
"Unable to convert {0} to a time interval." -f $value
}

# Example 3
# Create two TimeSpan objects, perform arithmetic operations
# on them then displays the result using the 'C' format specifier
"Example 3"

$interval1 = New-Object System.TimeSpan 7, 45, 16
$interval2 = New-Object System.TimeSpan 18, 12, 38

"{0:c} - {1:c} = {2:c}" -f $interval1,$interval2, $($interval1 - $interval2)
"{0:c} + {1:c} = {2:c}" -f $interval1,$interval2, $($interval1 + $interval2)

$interval1 = New-Object System.TimeSpan 0, 0, 1, 14, 365
$interval2 = [System.TimeSpan]::FromTicks(2143756)
"{0:c} + {1:c} = {2:c}" -f $interval1, $interval2, $($interval1 + $interval2)

# Example 4 - The "g" Format Specifier
# This specifier returns the string representation of a TimeSpan value in a compact form
# by including only the elements that are necessary.
"Example 4"

$interval1 = New-Object System.TimeSpan 7, 45, 16
$interval2 = New-Object System.TimeSpan 18, 12, 38

"{0:g} - {1:g} = {2:g}" -f $interval1, $interval2, $($interval1 - $interval2)

# do it in French
$CI = New-object System.Globalization.CultureInfo 'fr-FR'
[System.String]::Format($CI, $("{0:g} + {1:g} = {2:g}") ,
$interval1,$interval2,$($interval1 + $interval2) )
# Another interval
$interval1 = new-object System.TimeSpan 0, 0, 1, 14, 36
$interval2 = [System.TimeSpan]::FromTicks(2143756)
"{0:g} + {1:g} = {2:g}" -f $interval1, $interval2, $($interval1 + $interval2)

# Example 5
# Using the "G' Specifier
#
"Example 5"
$interval1 = new-object System.TimeSpan 7, 45, 16
$interval2 = new-object System.TimeSpan 18, 12, 38
"{0:G} - {1:G} = {2:G}" -f $interval1, $interval2, $($interval1 -$interval2)
[System.String]::Format($(New-Object CultureInfo("fr-FR")),
"{0:G} + {1:G} = {2:G}", $interval1,
$interval2, $($interval1 + $interval2))

$interval1 = new-object System.TimeSpan 0, 0, 1, 14, 36
$interval2 = [System.TimeSpan]::FromTicks(2143756)
"{0:G} + {1:G} = {2:G}" -f $interval1,$interval2, $($interval1 + $interval2)

Tuesday, 8 November 2011

Show-TimeSpan.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script, a re-implementation of an MSDN sample, shows 
  4.     details of a timespan object 
  5. .DESCRIPTION 
  6.     This script re-implements a simple MSDN script that creates a  
  7.     timespan object and displays its properties. 
  8. .NOTES 
  9.     File Name  : Show-TimeSpan.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN sample posted to: 
  16.         http://msdn.microsoft.com/en-us/library/system.timespan.aspx 
  17. .EXAMPLE 
  18.      Psh> .\Show-TimeSpan.ps1 
  19.     8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15 
  20.        Value of Days Component:                             229 
  21.        Total Number of Days:                   229.229340277778 
  22.        Value of Hours Component:                              5 
  23.        Total Number of Hours:                  5501.50416666667 
  24.        Value of Minutes Component:                           30 
  25.        Total Number of Minutes:                       330090.25 
  26.        Value of Seconds Component:                           15 
  27.        Total Number of Seconds:                      19,805,415 
  28.        Value of Milliseconds Component:                       0 
  29.        Total Number of Milliseconds:             19,805,415,000 
  30.        Ticks:                               198,054,150,000,000 
  31. #> 
  32.  
  33. # Define two dates 
  34. $date1 = new-object system.datetime 2010, 1, 1, 8, 0, 15 
  35. $date2 = new-object system.datetime 2010, 8, 18, 13, 30, 30 
  36.  
  37. # Create a time Interval 
  38. $interval = New-Timespan -start $date1 -end $date2 
  39.  
  40. #Display the interval 
  41. "{0} - {1} = {2}" -f $date2, $date1, $interval.ToString() 
  42.  
  43. # Display individual properties of the resulting TimeSpan object. 
  44. "   {0,-35} {1,20}" -f "Value of Days Component:", $interval.Days 
  45. "   {0,-35} {1,20}" -f "Total Number of Days:", $interval.TotalDays 
  46. "   {0,-35} {1,20}" -f "Value of Hours Component:", $interval.Hours 
  47. "   {0,-35} {1,20}" -f "Total Number of Hours:", $interval.TotalHours 
  48. "   {0,-35} {1,20}" -f "Value of Minutes Component:", $interval.Minutes 
  49. "   {0,-35} {1,20}" -f "Total Number of Minutes:", $interval.TotalMinutes 
  50. "   {0,-35} {1,20:N0}" -f "Value of Seconds Component:", $interval.Seconds 
  51. "   {0,-35} {1,20:N0}" -f "Total Number of Seconds:", $interval.TotalSeconds 
  52. "   {0,-35} {1,20:N0}" -f "Value of Milliseconds Component:", $interval.Milliseconds 
  53. "   {0,-35} {1,20:N0}" -f "Total Number of Milliseconds:", $interval.TotalMilliseconds 
  54. "   {0,-35} {1,20:N0}" -f "Ticks:", $interval.Ticks 

Monday, 15 June 2009

Get-TimeTick.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets and displays the static Tick values from TimeSpan Object 
  4. .DESCRIPTION 
  5.     This script 
  6. .NOTES 
  7.     File Name  : Get-TimeTick.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell V2 CTP3 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/system.timespan.tickspersecond(VS.85).aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-TimeTick.ps1 
  17.     Ticks Per: 
  18.     Day         : 864,000,000,000 
  19.     Hour        :  36,000,000,000 
  20.     Minute      :     600,000,000 
  21.     Second      :      10,000,000 
  22.     Millisecond :          10,000 
  23. #> 
  24.  
  25. ## 
  26. # Start of script 
  27. ## 
  28.   
  29. # Create the values 
  30. $tsticksperday         = [system.timespan]::ticksperday 
  31. $tsticksperhour        = [system.timespan]::ticksperhour 
  32. $tstickspermillisecond = [system.timespan]::TicksPerMillisecond 
  33. $tsticksperminute      = [system.timespan]::ticksperminute 
  34. $tstickspersecond =      [system.timespan]::tickspersecond 
  35.  
  36. # Display results nicely formatted 
  37. "Ticks Per:"  
  38. "Day         : {0, 15}" -f $tsticksperday.tostring("###,###"
  39. "Hour        : {0, 15}" -f $tsticksperhour.tostring("###,###"
  40. "Minute      : {0, 15}" -f $tsticksperminute.tostring("###,###"
  41. "Second      : {0, 15}" -f $tstickspersecond.tostring("###,###"
  42. "Millisecond : {0, 15}" -f $tstickspermillisecond.tostring("###,###"
  43. #End of Script 

Thursday, 20 November 2008

Display-TimeSpan.ps1

<#
.SYNOPSIS
Creates timespan objects and displays properties for each
.DESCRIPTION
This script creates time span objects then displays
the properties of each one.
.NOTES
File Name : Display-TimeSpan.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2
.LINK
http://pshscripts.blogspot.co.uk/2008/11/display-timespanps1.html
.EXAMPLE
PSH [C:\foo]: .\Display-Timespan.ps1
This example of the TimeSpan class properties
generates the following output. It creates
several TimeSpan objects and displays the values
of the TimeSpan properties for each

TimeSpan( 1 )
Interval = 00:00:00.0000001
Days 0 TotalDays 1.15740740740741E-12
Hours 0 TotalHours 2.77777777777778E-11
Minutes 0 TotalMinutes 1.66666666666667E-09
Seconds 0 TotalSeconds 1E-07
Milliseconds 0 TotalMilliseconds 0.0001
Ticks 1
TimeSpan( 111222333444555 )
Interval = 128.17:30:33.3444555
Days 128 TotalDays 128.729552597865
Hours 17 TotalHours 3089.50926234875
Minutes 30 TotalMinutes 185370.555740925
Seconds 33 TotalSeconds 11122233.3444555
Milliseconds 344 TotalMilliseconds 11122233344.4555
Ticks 111222333444555

TimeSpan( 10, 20, 30, 40, 50 )
Interval = 10.20:30:40.0500000
Days 10 TotalDays 10.8546302083333
Hours 20 TotalHours 260.511125
Minutes 30 TotalMinutes 15630.6675
Seconds 40 TotalSeconds 937840.05
Milliseconds 50 TotalMilliseconds 937840050
Ticks 9378400500000
TimeSpan( 1111, 2222, 3333, 4444, 5555 )
Interval = 1205.22:47:09.5550000
Days 1205 TotalDays 1205.94941614583
Hours 22 TotalHours 28942.7859875
Minutes 47 TotalMinutes 1736567.15925
Seconds 9 TotalSeconds 104194029.555
Milliseconds 555 TotalMilliseconds 104194029555
Ticks 1041940295550000
FromDays( 20.84745602 )
Interval = 20.20:20:20.1980000
Days 20 TotalDays 20.8474559953704
Hours 20 TotalHours 500.338943888889
Minutes 20 TotalMinutes 30020.3366333333
Seconds 20 TotalSeconds 1801220.198
Milliseconds 198 TotalMilliseconds 1801220198
Ticks 18012201980000

#>

##
# Start of Script
##

# Constant
$HeaderFmt = "`n{0, -45}"
$DataFmt = "{0,-12}{1,8} {2,-18}{3,21}"

# Helper function
function ShowTimeSpanProperties {
param ([System.TimeSpan] $Interval = 1)

# Display the properties of the TimeSpan parameter.

"Interval = {0,21}" -f $interval
"$DataFmt" -f "Days", $interval.Days, "TotalDays", $interval.TotalDays
"$DataFmt" -f "Hours", $interval.Hours, "TotalHours", $interval.TotalHours
"$dataFmt" -f "Minutes", $interval.Minutes, "TotalMinutes", $interval.TotalMinutes
"$DataFmt" -f "Seconds", $interval.Seconds, "TotalSeconds", $interval.TotalSeconds
"$DataFmt" -f "Milliseconds", $interval.Milliseconds, "TotalMilliseconds", $interval.TotalMilliseconds
"$DataFmt" -f $null, $null, "Ticks", $interval.Ticks
}

# Start of main script
# Create and display a comment
$comment = @"
This example of the TimeSpan class properties
generates the following output. It creates
several TimeSpan objects and displays the values
of the TimeSpan properties for each
"@
$comment

# Create and display a TimeSpan value of 1 tick.
$ts = [system.TimeSpan] 1
"$HeaderFmt" -f "TimeSpan( 1 )"
ShowTimeSpanProperties($ts)

# Create a TimeSpan value with a large number of ticks.
$ts = [System.TimeSpan] 111222333444555
"$HeaderFmt" -f "TimeSpan( 111222333444555 )"
ShowTimeSpanProperties($ts)

# This TimeSpan has all fields specified.
$ts = New-Object System.TimeSpan 10, 20, 30, 40, 50
"$HeaderFmt" -f "TimeSpan( 10, 20, 30, 40, 50 )"
ShowTimeSpanProperties($ts)

# This TimeSpan has all fields overflowing.
$ts = New-Object System.Timespan 1111, 2222, 3333, 4444, 5555
"$HeaderFmt" -f "TimeSpan( 1111, 2222, 3333, 4444, 5555 )"
ShowTimeSpanProperties($ts)

# This TimeSpan is based on a number of days.
$ts = [system.TimeSpan] ([system.TimeSpan]::FromDays(20.8474560))
"$headerFmt" -f "FromDays( 20.84745602 )"

ShowTimeSpanProperties($ts)

# End of script