0

I have a list of Unix Time Stamps for October 20, 2018 which looks something like this

Unix Time Stamp Time Converted

  • 1539993951 - 10/20/2018 @ 12:05am (UTC)
  • 1539997544 - 10/20/2018 @ 1:05am (UTC)
  • 1540001143 - 10/20/2018 @ 2:05am (UTC)
  • 1540004743 - 10/20/2018 @ 3:05am (UTC)
  • 1540008353 - 10/20/2018 @ 4:05am (UTC)
  • 1540011941 - 10/20/2018 @ 5:05am (UTC)
  • 1540015547 - 10/20/2018 @ 6:05am (UTC)
  • 1540019148 - 10/20/2018 @ 7:05am (UTC)
  • 1540022753 - 10/20/2018 @ 8:05am (UTC)
  • 1540026346 - 10/20/2018 @ 9:05am (UTC)
  • 1540029951 - 10/20/2018 @ 10:05am (UTC)
  • 1540033550 - 10/20/2018 @ 11:05am (UTC)
  • 1540037150 - 10/20/2018 @ 12:05pm (UTC)
  • 1540040746 - 10/20/2018 @ 1:05pm (UTC)
  • 1540044347 - 10/20/2018 @ 2:05pm (UTC)
  • 1540047951 - 10/20/2018 @ 3:05pm (UTC)
  • 1540051549 - 10/20/2018 @ 4:05pm (UTC)
  • 1540055144 - 10/20/2018 @ 5:05pm (UTC)
  • 1540058750 - 10/20/2018 @ 6:05pm (UTC)
  • 1540062349 - 10/20/2018 @ 7:05pm (UTC)
  • 1540065938 - 10/20/2018 @ 8:05pm (UTC)
  • 1540069549 - 10/20/2018 @ 9:05pm (UTC)
  • 1540073134 - 10/20/2018 @ 10:05pm (UTC)
  • 1540076744 - 10/20/2018 @ 11:05pm (UTC)

I have a method to pull the Unix Time Stamp for the current time

        public static Int32 unixTimeStampNow()
        {
            Int32 unixTimestamp = (Int32)(DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            Console.WriteLine("The Unix Timestamp is ", unixTimestamp);
            return unixTimestamp;
        }

Current time stamp: 1596893776 08/08/2020 @ 1:45pm (UTC)

If I wanted to find the closest time stamp to the time of day from the current time stamp (1:45pm) which would be (1540044347 - 10/20/2018 @ 2:05pm (UTC)) in the above list. How could I go about that? Appreciate any help in advance!

Ryan Brown
  • 43
  • 2
  • Why are you doing that at all instead of using DateTime? You gain nothing by it, but you *do* lose the local time indicator -`DateTime.Now` returns local time. You lose data too - second fractions – Panagiotis Kanavos Aug 08 '20 at 17:50
  • If you wanted to find the 'closes' point to some arbitrary DateTime, you can order all DateTime instances and find the 'Min` value larger than it , the value right before it and check which is closest. You can do that with a LINQ query. Or you can subtract the target DateTime from all stored values and select the smallest absolute difference. You can do the same with just timestamps. but that would incur the extra conversion cost. – Panagiotis Kanavos Aug 08 '20 at 17:54

1 Answers1

1

You could put the converted unix time stamp in an array, then use something like Array.Exists public static bool Exists<T> (T[] array, Predicate<T> match);I don’t know all the code off the top of my head, but here’s an example of how I would do something like that where ‘planets’ would essentially be your timestamps:

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] planets = { "Mercury", "Venus",
                "Earth", "Mars", "Jupiter",
                "Saturn", "Uranus", "Neptune" };

            Console.WriteLine("One or more planets contain 'M': {0}",
                Array.Exists(planets, element => element.Contains("M")));

            Console.WriteLine("One or more planets contain 'T': {0}",
                Array.Exists(planets, element => element.Contains("T")));

            Console.WriteLine("Is Pluto one of the planets? {0}",
                Array.Contains(planets, element => element == "Pluto"));
        }
    }
}
SkiSharp
  • 171
  • 9