Skip to content

Instantly share code, notes, and snippets.

@aviatrix
Created January 16, 2012 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aviatrix/1621622 to your computer and use it in GitHub Desktop.
Save aviatrix/1621622 to your computer and use it in GitHub Desktop.
Calculating dansity of posts/items/events etc .. over 24 hr period
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment