This is a small function i wrote in C# to calculate the density of say twitter posts or events … it takes as a parameter a list of UNIX timestamps. It can be easily moddified to get a list of DateTime items.
[csharp]private static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
}

public Dictionary<int, int> CalculateDensity(List<int> timestamps)
{
// the Key represents the hour ,
// and the value represents the number of timestamps sent during a given hour
var converted = new Dictionary<int, int>();

foreach (var stamp in timestamps)
{
var hour = ConvertFromUnixTimestamp(stamp);

if (converted.ContainsKey(hour.Hour))
{
converted[hour.Hour]++;
}
else
{
converted.Add(hour.Hour, 1);
}
}
return converted;
}[/csharp]

Tagged with:
 

2 Responses to Calculating density of posts/items/events etc .. over 24 hr period

  1. avioli says:

    density, not dAnsity

  2. Никола says:

    thanks , i will update

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>