Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  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
Subscribe to Windows IT Pro | See More Utilities Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
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: . . .

Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
More fun TechEd 2005 Resources

Kevin points out some more TechEd resources ...

Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

Ballmer: Xbox 360 'an Unqualified Success'

It's a product line that has consumed tens of billions of dollars of R&D, money that can never be recouped. The most recent version of the product is so endemically buggy that it has suffered from an historic product recall whose value exceeds $1 billion ...


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 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 Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Microsoft Exchange & Windows Connections event returns to Las Vegas Nov 10 - 13
Connections returns to Las Vegas for this exciting event where each attendee will receive SQL Server 2008 standard with 1 CAL. Co-located with Microsoft ASP.NET, SQL Server, and SharePoint Connections with over 250 in-depth sessions.

Free Online Event! Virtualization:Get the Facts!
Register now and attend this free, live in-depth online conference on November 13 and 20, 2008, produced by Windows IT Pro. All registrants are eligible to receive a complimentary one-year digital subscription to Windows IT Pro (a $49.95 value)!

Check Out Hyper-V Video on ITTV
Watch Karen Forster's interview on Hyper-V's performance on ITTV.net.

Ease Your Scripting Pains with the Flexibility of PowerShell!
Join MVP Paul Robichaux on December 11, 2008 at 11:00 AM EDT as he equips you with PowerShell basics in 3 introductory lessons, each followed by a live Q&A session—all on your own computer!

PASS Community Summit 2008 in Seattle on Nov 18-21
The don’t-miss event for Microsoft SQL Server Professionals. Register now and you’ll enjoy top-notch Microsoft and Community speakers and more.



Speed Up Your PC!
Try Diskeeper 2008 with InvisiTasking Free Now!

Get Protected -- Data Protection Manager 2007
Protect your virtualized environment with Data Protection Manager

Agent-less Remote Backup Service, Free 30 Day Trial
Award winning remote backup service at a competitive price with no min GB/month. Sign up Now!

ScriptLogic Cartoon Caption Contest
Submit your caption and you will be entered to win $198.42

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

List Your Products in Our Technology Resource Directory
Don't miss the chance to post your free listing in this comprehensive directory for IT and developer professionals, powered by Windows IT Pro. But hurry! Deadline ends Oct. 9.
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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing