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.

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;
        }
Tagged with:
 
  • avioli

    density, not dAnsity

  • http://nsivkov.com Никола

    thanks , i will update