Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


April 02, 2008

User-Friendly Time Spans in Windows PowerShell

Working with dissimilar time-span values is a breeze with this Windows PowerShell script
RSS
View this exclusive article with VIP access -- click here to join |
See More Utilities Articles Here | Reprints | Or sign up for our VIP Monthly Pass!
Untitled Document

 Executive Summary:
A short Windows PowerShell script makes it easy to convert time measured in days, hours, minutes, and seconds into more easily compared time-span values using the Microsoft .NET Framework TimeSpan structure.

There's no real standard for how applications should display or log information about the duration of activities. Some applications might show these durations or time spans in a human-understandable form, showing some combination of days, hours, minutes, seconds, and milliseconds. Others stick to a single unit such as seconds or days and use fractional quantities where necessary. Depending on how an application represents a time span, you might have to do some work to make the information usable elsewhere, or sometimes even to get a sense of how long the time span is. The To-TimeSpan.ps1 script and some other techniques I'll demonstrate in this article make universal time-span translation possible using Windows PowerShell. Let's talk about what a time span actually is, then look at how to use the script to simplify translation and some examples of its usage for real-world activities.

Time Spans: Measures of Elapsed Time
A time span is a special quantity because it's a measure of elapsed time between two events, as opposed to a distinct point in time at which something takes place. Time spans are important because most activities we engage in are repetitive, and knowing how often activities occur and how long they take to happen is an intrinsic part of how we think about them. We think about durations so often that we aren't even aware of them. We ask questions such as, How long does it take your backup to run? How long should you microwave the popcorn? How long does it take you to fill out your timesheet each day? What's the Ping response time for server x? How long does it take for Windows to boot up?

We don't usually measure all those things in detail, but when it's important to do so—for example, when you're trying to determine whether your backup system is still backing up as quickly as it used to—our system of representing time spans makes it difficult to compare times easily. Your logs might show that a backup took 8 hours, 26 minutes, and 58 seconds to back up 102.2GB of data on one day. A week later, after some file cleanup and a couple of changes to the backup software settings, a backup takes 8 hours, 19 minutes, and 14 seconds to back up 100.9GB. The backup takes less time, but the amount of data backed up is smaller, so how can you tell whether the changes make the backup run faster? You can calculate the answer by converting numbers into a common measure such as seconds or days, then dividing the backup size by the elapsed time for each backup—but doing so takes a bit of work with a calculator for most people.

The Microsoft .NET Framework runtime has a System.TimeSpan class designed specifically to support time-span calculations. If you give a System.TimeSpan object a time measured in terms of days, minutes, hours, seconds, and milliseconds, it will convert them into a single duration. This duration is measured in terms of ticks (100 nanosecond intervals) by default, but you can also extract it as a total value in whatever measurement you want: days, minutes, seconds, and so on. The To-TimeSpan script uses the System.TimeSpan structure to handle calculation work.

Using To-TimeSpan
You can download the To-TimeSpan script by clicking the Download the Code Here button at the top of the page. To use To-TimeSpan, PowerShell must be able to find the script. You can download To-TimeSpan.ps1 to a directory in your search path to simplify use. If the script isn't located in your search path, you'll need to specify the complete path to the script. If your current PowerShell location is the folder where the script is saved, you can use relative notation to specify this path as .\To-TimeSpan. If you choose to use this technique, you'll use .\To-TimeSpan instead of simply To-TimeSpan in the examples below.

I wrote To-TimeSpan specifically to make it easy to enter mixed-unit time spans. For that reason, To-TimeSpan uses a single argument, the time span in days:hours:minutes:seconds form. You don't have to have all the values, but the script assumes the smallest portion of the time-span data is in seconds, so you do need to provide colons (:) to indicate lower values that are missing. For example, you can convert 8 days and 2 minutes into a time span like this: . . .


Already a VIP member?
Please log on to view the full article

Why become a VIP member?

VIP-only online access
VIP CD delivered twice a year: offline access to the entire Windows IT Pro article library
Monthly issue of your choice of Windows IT Pro or SQL Server Magazine

Subscribe Now
Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
What You Need to Know About Microsoft's x64 Server Product Plans

What do Longhorn Server, Windows Compute Cluster Server, and Windows Vista have in common? The x64 platform. ...

WinInfo Short Takes: 4th of July Special Edition

An often irreverent look at some of the week's other news, including a shortened work week thanks to the 4th of July, expensive Windows 7 pricing, Bing's modest monthly gains, IE 8 heading to work, Steve Jobs back at Apple, and so much more ...

Social War Dialing - The New Identity Theft Menace

A new method of stealing personal financial information uses VOIP to attack bank customer over the phone. ...


Related Articles Working with Shortcuts in Windows PowerShell

New .chm File and Task-Based Guide Can Be Helpful PowerShell Resources

Essential Windows PowerShell Commands

Steps for Getting Started with PowerShell

Related Events PowerShell 201 - eLearning Series with Paul Robichaux

PowerShell 101 - eLearning Series

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format

Test Drive IT Solutions and Get Free Music Downloads
Solve your toughest IT problems with these free downloads and receive 5 free music downloads!


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home asp.netPRO Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement | Reprints and Licensing