Suggestion: YearWeekDay and DaysSince formats for version numbers

Feb 2, 2010 at 9:49 AM
Edited Feb 2, 2010 at 9:51 AM

For a couple of years now, I've been using a modified version of an old AssemblyTaskInfo targets. I never liked the original YearMonthDay format - it always seemed like 50000 was way to big for a build number. And of course it broke come 2007. So I added a couple of alternatives, which I use in slightly different settings. I do ad-hoc, part time work on a number of small projects, and don't have the luxury of a build server - I just use Visual Studio, and these have worked well for me.

I figured these may be of use to someone else, and I presume this is the right forum for this?

YearWeekDay: If you have ever worked in an environment where week numbers matter. Or switched on the 'Show Week Numbers' option in Outlook, this option may be attractive. You would think that DateTime.ToString(format) would provide a week number format option, but it doesn't (well none that I've found - not even in 4.0?). The resulting version number for Feb 2, 2010 would be 10062. March 15 will be 10121 and December 19 10475. So my bit of patch looks like this:

case IncrementMethod.YearWeekDay: {
DateTime now = DateTime.Now; Calendar cal = CultureInfo.InvariantCulture.Calendar; int week = cal.GetWeekOfYear(now, CalendarWeekRule.FirstDay, DayOfWeek.Monday); int newVersionInt = ((now.Year - 2000) * 1000) + (week * 10) + (int) now.DayOfWeek; string newVersionNumber = newVersionInt.ToString(); return newVersionNumber; 

Although if I was to continue in the style of the current release, I suppose it would be more like this (note: I haven't tested this version).

case IncrementMethod.YearWeekDay:
	DateTime now = this.UseUtc ? DateTime.UtcNow : DateTime.Now;
	string newVersionNumber3 = now.ToString("yy", CultureInfo.InvariantCulture);
	newVersionNumber3 += CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
	newVersionNumber3 += ((int)now.DayOfWeek).ToString(); // day of week can only be 1 digit - in all calendars I've ever heard of.
	Log.LogMessage(MessageImportance.Low, logMessage, newVersionNumber3);
	return newVersionNumber3;

My other alternative is a 'days since' option, which simply calculates the number of days since a particular date. I 'overload' the 'Format' field to supply the base date and then just use DateTime maths. In my case, the project had an 'official' first day, and build numbers are a based in the number of days since that date. Here's my code (I won't try and re-style it, and it's missing cultureInfo for the date parse, which probably needs to be added. In fact, a defined format and a TryParseExact would also be a good idea.): 

case IncrementMethod.DaysSinceDate:
        DateTime now = this.UseUtc ? DateTime.UtcNow : DateTime.Now;
        DateTime since;
        if (DateTime.TryParse(format, out since))
            int newVersionNumberInt = (int)new TimeSpan(now.Ticks - since.Ticks).TotalDays;
            string newVersionNumber = newVersionNumberInt.ToString();
            Log.LogMessage(MessageImportance.Low, logMessage, newVersionNumber);
            return newVersionNumber;
            throw new ArgumentException("Format string does not specify a valid date");



note: edited for formatting just after posting. 

Feb 5, 2010 at 12:46 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Feb 5, 2010 at 1:07 PM

Thanks Ryan. Integrated and will ship with the and releases.