Promo

Preface

Oh, hello.  Here lie a collection of articles, narratives and ponderings of computery things; finely blended with my portfolio bestowing works and experiments in U.I. design, infographics, and software development.  Bon appétit.

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

Categories

posts rss
Cynosura 0 rss
General 0 rss
Graphic Design 4 rss
Programming 6 rss
WWW Tech 2 rss

The TimeSpan Articulator

Numbers to Words

A mongst serveral additions I made to the BlogEngine platform (the ASP.NET blogging application which runs this weblog) was the archive metadata which details the active time of the blog; that is, the span of time from the first and last post publication dates.  This feature is achieved with the TimeSpanArticulator; a simple static class which converts a TimeSpan to simple english.  In the true spirit of openess, I’ve released this utility under the LGPL, and here’s how you use it.

Detail

public static string Articulate(
	TimeSpan span);

public static string Articulate(
	TimeSpan span, 
	TemporalGroupGranularity accuracy);

The TimeSpanArticulator class has two public static methods (above).  The first method should be easy to understand.  You supply a TimeSpan and you recieve a String representation of that TimeSpan.  Here are some example articulations from 1 to 400 days:

pseudocode

n = 0;
WHILE n <= 400 :
	n = n++;
	ts = TimeSpan.FromDays(n);
	DISPLAY: 
		"days = " +
		n +
		" : " +
		TimeSpanArticulator.Articulate(ts);
	END DISPLAY
END WHILE

output

days = 1 : 1 day
days = 2 : 2 days
...
days = 7 : 1 week
days = 8 : 1 week and 1 day
...
days = 31 : 1 month
days = 32 : 1 month and 1 day
...
days = 365 : 1 year
days = 366 : 1 year and 1 day
...

The Second public method of TimeSpanArticulator requires you supply a TemporalGroupGranularity enumeration flag, which is then used by the articulator to allow you to control the granularity and structure of the output.

fig. A:

A breakdown of an example articulation

note:

Without specifying custom temporal granularities, the articulator will default to using years, months, weeks, days, and hours for the articulation.

There are 7 Temporal group granularities — Years, Months, Weeks, Days, Hours, Minutes, and Seconds — which are used as flags to control how the articulation is created. It’s also easy to add others to the enumeration (along with a compulsory TimeSpanAttribute attribute, which as the name suggests, gives the enumeration consants their actual TimeSpan values — see figure A) at design time.  What’s important to note here is that the lengths of each temporal granularity are static aproximations — unlike in reality where, for example, the exact length of months and years can vary — and so the articulation is agnostic with reference to any particular period in history.  As such, months are averaged to 30.5 days (365 days in a year/12 months a year ≈ 30.5 days per month) and years are 365 days (which doesn’t account for leap years or leap seconds).

If a particular temporal group granularity isn’t specified to be used in an articulation (eg. weeks) then the finer temporal granularities (eg. days and hours) are used to ‘fill-in’ the gap left by the absent granulatity.  Here are some examples of a series of articulations without weeks:

pseudocode

n = 0;
accuracy = month | day;
WHILE n <= 40 :
	n = n++;
	ts = TimeSpan.FromDays(n);
	DISPLAY: 
		"days = " +
		n +
		" : " +
		TimeSpanArticulator.Articulate(
			ts, accuracy);
	END DISPLAY
END WHILE

results

Notice that without weeks in the articulation, 8 days equates to ‘8 days’ rather than the typical ‘1 week and 1 day’.

days = 1 : 1 day
days = 2 : 2 days
days = 3 : 3 days
days = 4 : 4 days
days = 5 : 5 days
days = 6 : 6 days
days = 7 : 7 days
days = 8 : 8 days
days = 9 : 9 days
days = 10 : 10 days
...
days = 30 : 30 days
days = 31 : 1 month
days = 32 : 1 month and 1 day

Download

TimeSpanArticulator.txt

Author
Ray
Published
Tue. 27 January, 2009 12:17
1 Comments
comments closed
Tags
,
filed under
Programming
Rating
  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Bookmarks

Commentary

Comments are closed