Tuesday, June 10, 2008

XML Schema dateTime Primer for C# Developers

The XML Schema dateTime primitive data type represents the current date and time (June 10th, 2008 at 9:30am Eastern Daylight Savings at the time of this writing) in the following format:

2008-06-10T13:30:00Z

The above format represents the UTC date/time.  UTC, or coordinated universal time, is just Greenwich Mean Time (or Zulu time, hence the "Z").  UTC is the SI name for this zero time offset.  To get the UTC date/time for your local time, simply add your time zone offset.  In Indiana, where I'm from, our GMT offset is currently -04:00 (colloquially, "four hours behind").

Here's a C# one-liner to format the current time in the XML Schema dateTime format.

string xsDateTime = System.DateTime.Now.ToUniversalTime().ToString("o");

Thursday, June 5, 2008

PowerShell: Convert Unix line-ending to Windows CrLf

function GetFiles
{
  foreach ($i in $input) {
   ? -inputObject $i -filterScript { $_.GetType().Name -eq "FileInfo" }
  }
}

function ConvertToCrLf
{
  param([string] $path)
  gci $path |
  GetFiles |
  % {
    $c = gc $_.FullName
    $c | % {
             if (([regex] "`r`n$").IsMatch($_) -eq $false) {
               $_ -replace "`n", "`r`n"
             } else {
               $_
             }
           } |
        set-content $path
  }
}