Welcome to MSDN Blogs Sign in | Join | Help

Useful DateTime functions - LibraryTime.ps1

I was going to post a blog entries and realized that the example I wanted to give used some of my utility functions so I'll share them first.  I hope you find them interesting.  I put them in a file called LibraryTime.ps1 and dot source that during my login.  BTW  - everyone should adopt that pattern.  Files that contain functions meant to be dot sourced into another script should be called LibraryXXXX.ps1

function Measure-TimeSpan([TimeSpan]$Span, $Units=$Null)
{
   switch ($Units)
   {
   "Weeks"        {[Int]($Span.TotalDays / 7)}
   "Days"         {[Int]$Span.TotalDays}
   "Hours"        {[Int]$Span.TotalHours}
   "Minutes"      {[Int]$Span.TotalMinutes}
   "Seconds"      {[Int]$Span.TotalSeconds}
   "MilliSeconds" {[Int64]$Span.TotalMilliSeconds}
   "Ticks"        {$Span.Ticks}
   default        {$null}
   }
}


function Measure-Since([DateTime]$Time, $Units=$Null)
{
   $Since = Measure-TimeSpan $([DateTime]::Now - $Time) $Units
   if ($Since -eq $null)
   {
      throw 'Usage: Measure-Since -Time <DateTime> -Units ["Weeks","Days","Hours","Minutes","Seconds","Milliseconds","Ticks"]'
   }
   else
   {
      $Since
   }
}


function Measure-Till([DateTime]$Time, $Units=$Null)
{
   $Since = Measure-TimeSpan $($Time - [DateTime]::Now) $Units
   if ($Since -eq $null)
   {
      throw 'Usage: Measure-Till -Time <DateTime> -Units ["Weeks","Days","Hours","Minutes","Seconds","Milliseconds","Ticks"]'
   }
   else
   {
      $Since
   }
}

Set-Alias MSince Measure-Since
Set-Alias MTill  Measure-Till

Now let's try them out:

 

PS> "weeks","Days","Hours","Seconds","Milliseconds","ticks" | 
>>   
%{ "{0,16} $_" -f (Measure-Since "12/25/2005" $_)}
              38 weeks
             267 Days
            6398 Hours
        23034210 Seconds
     23034209798 Milliseconds
 230342098115960 ticks

PS> "weeks","Days","Hours","Seconds","Milliseconds","ticks" |
>>    %{ "{0,16} $_" -f (Measure-Till "12/25/2006" $_)}
              14 weeks
              98 Days
            2362 Hours
         8501789 Seconds
      8501788713 Milliseconds
  85017886963120 ticks

Enjoy

Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

 

PSMDTAG:DOTNET Datetime

Published Sunday, September 17, 2006 8:39 PM by PowerShellTeam
Attachment(s): LibraryTime.ps1

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Useful DateTime functions - LibraryTime.ps1

Hi,

Speaking of login, could you do a post on startup best practices?

Some stuff on profile.ps1 would be nice, as well as an explanation as to why it's in a separate folder, documents\PSConfiguration (rather than documents\PSH), and how we should organize our stuff in the two folders.

Thanks,

Rei
Monday, September 18, 2006 1:20 PM by rei

# re: Useful DateTime functions - LibraryTime.ps1

Rei;

Does this help?

http://www.leeholmes.com/blog/TheStoryBehindTheNamingAndLocationOfPowerShellProfiles.aspx

Re: Organizing -- which two folders are you talking about?

Lee
Monday, September 18, 2006 1:32 PM by PowerShellTeam

# re: Useful DateTime functions - LibraryTime.ps1

I am getting the following error if I create a LibraryTime.ps1 file (in the PsConfiguration folder) and then try to dot source it in my profile. Is the full path required to dot source a file?

'.\LibraryTime.ps1' is not recognized as a cmdlet, function, operable program, or script file.
At C:\Documents and Settings\sourabhm\My Documents\PSConfiguration\Microsoft.PowerShell_profile.ps1:1
+ .  <<<< .\LibraryTime.ps1

Thx
-Sourabh
Monday, September 18, 2006 6:48 PM by Sourabh

# re: Useful DateTime functions - LibraryTime.ps1

> Is the full path required to dot source a file?

Yes - your startup directory is a function of where you started the shell and can change.

Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Tuesday, September 19, 2006 12:33 AM by PowerShellTeam

# re: Useful DateTime functions - LibraryTime.ps1

Thanks Jeffrey. I am now using "$home\My Documents\PSConfiguration\LibraryTime.ps1" and it works great.
Tuesday, September 19, 2006 1:04 PM by Sourabh

# RE: calling functions from other scripts

One way would be to create a library of functions you could load at start up - see this for an example

Monday, July 23, 2007 4:00 PM by Latest Newsgroup Posts

# re: Useful DateTime functions - LibraryTime.ps1

I used following syntax to Dot in a library script in another script.

With $MyProdRoot a system env. variable pointing to root install directory of our product.

. "$env:MyProdRoot\msexch\bin\LibEx07.ps1"

For some reason following didn't work

. "$MyProdRoot\msexch\bin\LibEx07.ps1"

$MyProdRoot won't resolve !

Tuesday, November 20, 2007 12:31 PM by mdexch

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker