Second Life of a Hungarian SharePoint Geek

January 29, 2011

How to get the assembly version number from the build date and vice versa?

Filed under: Utility, Visual Studio, WPF — Tags: , , — Peter Holpar @ 00:36

Recently I worked on a project, where automatic assembly versioning of Visual Studio 2010 was applied using the AssemblyVersion attribute:

[assembly: AssemblyVersion("1.0.*")]

Testers worked with multiple application versions simultaneously, and version number (that was displayed on the application UI) was assigned to the bug report in TFS work items. To be able to determine the latest changeset for a version number, I needed the exact date and time of the build.

I found several none-official description (with minor differences) of the build date – version number conversion algorithm, like this one:

Determining the Build Date of an Assembly

I decided to create a simple utility to help the conversion in both directions.

The main part of the utility is an IValueConverter class that contains the logic for the conversion:

  1. using System;
  2. using System.Linq;
  3. using System.Windows.Data;
  4. using System.Globalization;
  5.  
  6. namespace Version2Date
  7. {
  8.     class VersionToDateConverter : IValueConverter
  9.     {
  10.         // based on the algorithm found:
  11.         // Kevin Gearing's Blog – Determining the Build Date of an Assembly
  12.         // http://dotnetfreak.co.uk/blog/archive/2004/07/08/determining-the-build-date-of-an-assembly.aspx
  13.  
  14.         #region IValueConverter Members
  15.  
  16.         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  17.         {
  18.             String result = String.Empty;
  19.  
  20.             DateTime versionDate = DateTime.MinValue;
  21.  
  22.             // check the value type and try to parse it as a date
  23.             if ((value is String) && (DateTime.TryParseExact((String)value, Global.DateFormat,
  24.                 CultureInfo.GetCultureInfo("en"), DateTimeStyles.None, out versionDate)))
  25.             {
  26.                 if (TimeZone.IsDaylightSavingTime(versionDate, TimeZone.CurrentTimeZone.GetDaylightChanges(versionDate.Year)))
  27.                 {
  28.                     versionDate.AddHours(-1);
  29.                 }
  30.  
  31.                 TimeSpan dateDiff = versionDate – Global.StartDate;
  32.                 int dayCount = (int)dateDiff.TotalDays;
  33.                 TimeSpan dateDiffSec = dateDiff.Subtract(new TimeSpan(dayCount, 0, 0, 0));
  34.                 int secCount = (int)(dateDiffSec.TotalSeconds / 2);
  35.                 result = String.Format("1.0.{0}.{1}", dayCount, secCount);
  36.             }
  37.  
  38.             return result;
  39.         }
  40.  
  41.         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  42.         {
  43.             String result = String.Empty;
  44.  
  45.             if (value is String)
  46.             {
  47.                 String[] verNums = ((String)value).Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
  48.  
  49.                 int dummyInt;
  50.                 if ((verNums.Length == 4) && (verNums.ToList().TrueForAll(str => int.TryParse(str, out dummyInt))))
  51.                 {
  52.                     int dayCount = int.Parse(verNums[2]);
  53.                     int secCount = int.Parse(verNums[3]) * 2;
  54.  
  55.                     DateTime versionDate = Global.StartDate.AddDays(dayCount).AddSeconds(secCount);
  56.                     if (TimeZone.IsDaylightSavingTime(versionDate, TimeZone.CurrentTimeZone.GetDaylightChanges(versionDate.Year)))
  57.                     {
  58.                         versionDate.AddHours(1);
  59.                     }
  60.  
  61.                     result = versionDate.ToString(Global.DateFormat);
  62.                 }
  63.             }
  64.  
  65.             return result;
  66.         }
  67.  
  68.         #endregion
  69.     }
  70.  
  71. }

The MainWindow.xaml of the application looks like this:

  1. <Window x:Class="Version2Date.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
  4.         xmlns:local="clr-namespace:Version2Date"
  5.         Title="Version – Date Converter"
  6.         Height="45" Width="400" ResizeMode="NoResize">
  7.     <Window.Resources>
  8.         <local:VersionToDateConverter x:Key="VersionToDate"/>
  9.     </Window.Resources>
  10.     <Grid>
  11.         <Grid.ColumnDefinitions>
  12.             <ColumnDefinition Width="Auto"></ColumnDefinition>
  13.             <ColumnDefinition Width="*"></ColumnDefinition>
  14.             <ColumnDefinition Width="*"></ColumnDefinition>
  15.         </Grid.ColumnDefinitions>
  16.         <TextBox Grid.Column="0" Name="VersionNumber" Text="{Binding ElementName=Date, Mode=TwoWay,
  17.             UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True,
  18.             Path=Text, Converter={StaticResource VersionToDate}}" MinWidth="100" MaxWidth="200"/>
  19.         <TextBox Grid.Column="1" Name="Date" MinWidth="100" MaxWidth="200" />
  20.         <Button Grid.Column="2" Click="GetCurrentDate" MinWidth="100" MaxWidth="200">Now</Button>
  21.         
  22.     </Grid>
  23. </Window>

The code behind of the XAML contains the simple event handler for the button click:

  1. private void GetCurrentDate(object sender, RoutedEventArgs e)
  2. {
  3.     Date.Text = DateTime.Now.ToString(Global.DateFormat);
  4. }

The next image displays the utility in action:

image

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: