-1

I came across code from this article, displayed below. Why is it using the Unix time (epochStart)? Can't it just use the UTCNow time instead of subtracting from the epochStart later?

DateTime epochStart = new DateTime(1970, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc);
TimeSpan currentTs = DateTime.UtcNow – epochStart;

var serverTotalSeconds = Convert.ToUInt64(currentTs.TotalSeconds);
var requestTotalSeconds = Convert.ToUInt64(requestTimeStamp);

if ((serverTotalSeconds – requestTotalSeconds) > requestMaxAgeInSeconds)
{
    return true;
}
Jared Lovin
  • 543
  • 9
  • 24
bp581
  • 859
  • 1
  • 16
  • 47
  • 1
    I would imagine that `requestTimeStamp` is "seconds since epoch" so would need to calculate the same offset from "Now" to allow a correct comparison against each other. – Scott Perham Jan 30 '18 at 20:53
  • Just guessing: This code assume that `requestTimeStamp` (is it a string? what else?) is expressed as the number of seconds elapsed from beginning of Unix time – Gian Paolo Jan 30 '18 at 20:54
  • requestTimeStamp is string – bp581 Jan 30 '18 at 20:56
  • What is the point of this question? You're asking us to explain the intent of the author (not you) of some arbitrary piece of code. I don't see an answerable question here. – theMayer Jan 31 '18 at 19:40

4 Answers4

0

Basically, UNIX time is just an approach that allows represent time point as a number. It represents number of time periods elapsed since start of an epoch. By an unwritten convention, the start of epoch is 1 Jan 1970. Number of seconds since 1 Jan 1970 isn't a huge number (relatively).

DateTime is also a 64-bit integer with much higher resolution, and its value is in a much higher range. Probably this was the rationale behind opting for UNIX time in this case.

felix-b
  • 8,178
  • 1
  • 26
  • 36
  • its not unwritten. http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_16 – pm100 Jan 30 '18 at 22:41
0

The page you reference explains that referenceTimeStamp is Unix Time and why:

We’ve calculated the time stamp for the request using UNIX timing (number of seconds since Jan. 1st 1970). This will help us to avoid any issues might happen if the client and the server resides in two different time zones.

NetMage
  • 26,163
  • 3
  • 34
  • 55
  • Here is my confusion. Since the UTC time in east coast and west cost differ....whats the point of subtracting from Unix time ?. WestCostTime - UniXTime is always greater than EastCost -UnixTime right ? – bp581 Jan 31 '18 at 15:16
  • UTC time on the west coast is the same as UTC time on the east coast... So if every time is represented as UTC, then there is no issue. It is up to the client to convert between local time representation and UTC. This is pretty standard stuff nowadays. – theMayer Jan 31 '18 at 17:28
  • @bp581 The conversion of 1/1/1970 to `DateTime` is done on the client and server independently, so it is done in the local timezone of each, so that erases the timezone from the results. I don't know why Unix Time was chosen instead of UTC time in the article. – NetMage Jan 31 '18 at 19:11
  • SO I can just ignore the UNix time. This article made me to think UTC time differs from time zone..which is not. – bp581 Feb 02 '18 at 12:39
0

Basically, type incompatibility.

DateTime.UtcNow is a DateTime type. requestTimeStamp is an int (or long int). If you tried to compare them directly in the if-statement, at best you would get nonsense, and at worst, your code won't compile.

So, you need to know the conversion between DateTime.UtcNow and the int value, which is provided by the definition of the Unix Epoch.

theMayer
  • 15,456
  • 7
  • 58
  • 90
  • `requestTimeStamp` is a string. – NetMage Jan 31 '18 at 17:03
  • OK, a string in what format? That's the whole issue here. Please show an example of what this value looks like when you get it. – theMayer Jan 31 '18 at 17:16
  • Based on the code you have, `Convert.ToUInt64(requestTimeStamp);` will throw an exception if it is anything other than a number. – theMayer Jan 31 '18 at 17:29
  • I didn't ask the question. You should read the article linked and then you would know the answers. – NetMage Jan 31 '18 at 19:09
  • Your answer says "`requestTimeStamp` is an `int` (or `long int`)." That isn't correct, it is a string, as explained in the comments to the question and the article linked. – NetMage Jan 31 '18 at 20:27
0

Unix time in seconds and .net time in seconds use a different 0.

  • Windows 0 is 0/0/0001
  • Unix 0 is 1/1/1970

So if you want to comare them you have to adjust by the differentce between these 2. Which is what this code is doing

DateTime epochStart = new DateTime(1970, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc);
TimeSpan currentTs = DateTime.UtcNow – epochStart;
pm100
  • 48,078
  • 23
  • 82
  • 145