Program a time-coding application to calculate read-times


by Jason Salas, KUAM.COM
February 02, 2003

 

Past editions:

Building a newsletter management system in ASP.NET with C#

A simple document management system in ASP with XML and XSLT

Password-protect your Web work

Error Handling in VBScript

Creating a banner ad rotation system

The Web-safe color palette for developers

Time-saving Windows shortcuts

Analyzing your site's traffic logfiles

Technologies Used:

Level of Difficulty

See this script in action below

Being in the news game, our lives revolve around time. It’s the one thing that as programmers we can code around, against, or with – but can’t control.

Doing online news everyday at KUAM.COM, we tightly integrate our online content with what runs on our TV news broadcasts that night, as well as what we report on our radio newscasts throughout the day. Everyday is all about 6PM for us – showtime. And when we’re on the air, every single second of our lives for the next 60 minutes is tediously scripted and planned. And because of this, when we write the news that you see every night, knowing how long it will take for one of our anchors to read each script – right down to the precise second – is critical.

For us, it's all about calculating the Total Read Time (TRT) for a story.  It's this functionality that we'll very easily build onto an ASP.NET WebForm this week.

And being that regardless of the amount of speech training, no two reporters are alike, so we need some variability in the amount of time it’s going to take to read each story, so that we can fit the maximum amount of stories within a one-hour show. This is also a sneak-peek inside how we do things inside the newsroom for “Guam’s newsteam”.

There’s nothing really earth-shattering about this script…it just demonstrates how to use the C# programming language creatively with a couple of custom algorithms to time-code a bit of text, translating content entered by a user to it’s equivalent minutes and seconds. It makes use of several key concepts about ASP.NET programming, namely a clear separation of code from content, and having the WebForm post back to itself, rather than pass information onto another script.


Beneath the Hood
Looking at the WebForm itself (all of the elements and contents within the <HTML> tags, we can see that we’re using several ASP.NET Web server controls, namely two Labels, a DropDownList, a TextBox, and a Button.

Looking now at the C# code within the <SCRIPT> blocks, we first define protected three variables that will be used throughout a couple of custom methods we’ll use in the WebForm. The first method, Convert_Time, is wired to the Button server control’s OnClick event, and first instantiates a StringBuilder object and uses the object’s Append method to add-in the text entered in the TextBox. We then pass the total length of the text entered via our StringBuilder object’s Length property as an argument to a custom method, GenerateReadTime.

<% @ Page Language="C#" Debug="true" %>

<script runat="server">

protected string minutes;
protected decimal temp;
protected decimal seconds;

public void Convert_Time(object sender, EventArgs e)
{
const int TIME = 60;

StringBuilder sb = new StringBuilder();
sb.Append(txtMinutes.Text);

decimal convertedTime = GenerateReadTime(sb.Length)/TIME;

if(convertedTime.ToString().IndexOf(".") >= 1)
{
/* MINUTES */
minutes = convertedTime.ToString().Substring(0,convertedTime.ToString().IndexOf("."));

/* SECONDS */
temp = Convert.ToDecimal(convertedTime.ToString().Substring(convertedTime.ToString().IndexOf("."))) * TIME;
seconds = Convert.ToDecimal(temp.ToString().Substring(0));

lblTRT.Text = "The TRT is " + minutes;
if(Convert.ToInt32(minutes) >= 2 || Convert.ToInt32(minutes) <= 0)
{
lblTRT.Text += " minutes, ";
}
else
{
lblTRT.Text += " minute, ";
}
lblTRT.Text += String.Format("{0}",Math.Round(seconds,0));
if(seconds >= 2 || seconds <= 0)
{
lblTRT.Text += " seconds.";
}
else
{
lblTRT.Text += " second(s).";
}
}
else if(convertedTime.ToString().IndexOf(".") == 0 || convertedTime.ToString().IndexOf(".") == -1)
{
minutes = convertedTime.ToString();

lblTRT.Text = "The TRT is " + minutes;
if(Convert.ToInt32(minutes) >= 2 || Convert.ToInt32(minutes) <= 0)
{
lblTRT.Text += " minutes, 0 seconds.";
}
else
{
lblTRT.Text += " minute, 0 seconds.";
}
}

lblRecapInfo.Text = "The length of your story is "+ String.Format("{0:#,###}",sb.Length) + " characters";
}

private decimal GenerateReadTime(int charsInScript)
{
// get the speed for the reader
int readTime;
switch(ddlSpeed.SelectedItem.Text)
{
case "Slow":
readTime = 30;
break;
case "Normal":
readTime = 45;
break;
case "Fast":
readTime = 80;
break;
default:
readTime = 45;
break;
}

decimal pace = Convert.ToDecimal(charsInScript/readTime);
return pace;
}

</script>

<html>
<head>
<title>Total Read Time (TRT) calculator</title>
</head>
<body><form runat="server">
<asp:Label id="lblRecapInfo" runat="server"/>
<br/><br/>
<asp:Label id="lblTRT" runat="server" color="Red" font-bold="True"/>
<br/><br/><br/><br/><br/>
Read-Speed:
<br>
<asp:DropDownList id="ddlSpeed" runat="server">
<asp:ListItem text="Slow"/>
<asp:ListItem text="Normal" selected/>
<asp:ListItem text="Fast"/>
</asp:DropDownList>
<br/><br/><br/>
Write your story here:
<br/>
<asp:Textbox id="txtMinutes" textmode="multiline" columns="85" rows="15" runat="server"/>
<br/>
<asp:Button id="btnSubmit" onclick="Convert_Time" text="Calculate your story's TRT" runat="server"/>
</form></body></html>

This custom method returns data of type decimal, and divides the total number of characters by the variable value selected for the DropDownList, obtained through a switch statement, which is the speed at which a user can read. This quotient is returned to the calling code (to our Convert_Time method).

When control returns to Convert_Time, the remaining code of the script uses various static methods to format an output string that returns the total minutes and seconds needed to read the text to the Web browser, and then assigns this string to one of the Label’s Text property.

The only gotcha that exists for this WebForm (at least that I know of at this point), is that you can only enter a maximum of 79,200 characters in the Textbox server control.

This demo also isn’t too far removed from the time-coding system we use internally at KUAM News to calculate our read times for when we go on the air. We take the same programming logic and subclass it to be inherited and reused by more grandiose applications within and complementary to, our intranet.

You could also use this for timing yourself for speeches, for presentations, etc.

Have fun – and happy programming!

</jason>