News for hackers. Stuff that matters.
Re:Please put units in names by HideousKojima on Monday March 21, 2022 @12:31AM (#30750290)
>Having everything work this way makes it so much easier to review code for errors.

Had one making me pull my hair put the other day in C#. C# datetimed are measured in increments of 100 nanoseconds elapsed since January 1st 1 AD or something like that. Was trying to convert Unix time in milliseconds to a C# datetime and didn't realize they were using different units. My fault for not reading the docs but having it in the name would have saved me a lot of trouble.

Re:Please put units in names by eyelidlessness on Monday March 21, 2022 @12:52AM (#30750433)
What the heck is with MS and weird datetime precision? I figured out some bug with a third party API was due to SQL Server using 1/300th seconds. Who would even think to check for that if you’re not using their products?
Re:Please put units in names by osigurdson on Monday March 21, 2022 @01:16PM (#30756862)
1/300 seconds is an odd one for sure. In the case of DateTime however, I'd say it is designed to hit as many use cases as possible with a 64 bit data structure. Using a 1970 epoch as is (logically) used for Unix system times naturally misses even basic use cases like capturing a birthdate.

It is quite hard actually to disagree with the 100ns tick size that they did use. 1 microsecond may have also been reasonable as it would have provided a larger range but there are not many use cases for microsecond accurate times very far in the past or in the future. Similarly using 1 nanosecond may have increased the applicability to higher precision use cases but would have reduced the range to 100 years. Alternately, they could have used a 128 bit structure providing picosecond precision precision from big bang to solar system flame out with the resultant size/performance implications.

Re:Please put units in names by spyspy on Monday March 21, 2022 @07:28AM (#30752757)
God speed trying to parse dates from excel. They have bugs _intentionally built in_
Re:Please put units in names by justsomehnguy on Monday March 21, 2022 @03:15AM (#30751150)
Who would even think to check if the system is counting from 1970/01/01 you’re not using their products?
Re:Please put units in names by eyelidlessness on Monday March 21, 2022 @01:19PM (#30756914)
If they’re using ISO format I don’t really care what they’re counting from. But I care if some ISO dates are preserved exactly and some are rounded to another value… especially when that rounding is non-obvious. It took me months to even identify the pattern clearly enough to find an explanation. Up to that point we just had an arbitrary allowance that the value might vary by some unknown amount, and looked for the closest one.
Re:Please put units in names by onlyrealcuzzo on Monday March 21, 2022 @09:13AM (#30753722)
This is an established standard.

It's really not any stranger than starting dates at 1 CE, or array indexes starting at 0.

Re:Please put units in names by justsomehnguy on Monday March 21, 2022 @09:33AM (#30753911)
> This is an established standard.

Established doesn't mean it is understandable without documentation. Anyone who is not familiar with it doesn't know why it starts in 1970 and counts seconds. You need to actually open the documentation to know about that, and it's name (be it unix time, epoch, epoch time or whatever) doesn't help in understanding what it is and what unit it is using.

Re:Please put units in names by hannasanarion on Monday March 21, 2022 @10:25AM (#30754489)
The metric system is also not understandable without documentation, byt you don't need to explain it every time because every living person should have gotten that documentation drilled into them at age ten.

UNIX time is an easy international standard, everybody with computer experience knows what it is and how to work with it.

Re:Please put units in names by justsomehnguy on Monday March 21, 2022 @11:28AM (#30755184)
> everybody with computer experience knows what it is and how to work with it.

Thanks for the laugh.

The only ones who needs to know about unixtime is:

developers when they do something which takes it/produces it

*nix sysadmins

Everyone else with "computer experience" could live all their life without the need to know what unixtime is.

Re:Please put units in names by hannasanarion on Monday March 21, 2022 @11:53AM (#30755512)
Yeah, that's what I meant. People who program or do sysadmin, ie anybody who will ever need to call a sleep function, should know what a unixtime is.
Re:Please put units in names by justsomehnguy on Tuesday March 22, 2022 @03:57AM (#30763646)
> anybody who will ever need to call a sleep function

Bullshit.

I never needed to know what unixtime is when I wrote anything with sleep().

All I needed to know is how long in seconds I want the execution to pause for, never ever I needed to manually calculate something from/to unixtime, even when working with datetimes types.

Re:Please put units in names by hannasanarion on Wednesday March 23, 2022 @12:30AM (#30774968)
No, but as a person who has had a need for sleep() before, you are also the type of person who could be expected to know what unix time is.

Nobody is saying that the two things need to be connected, the point is that it can be name dropped in a type definition, and you would know what it means.

Re:Please put units in names by jodrellblank on Monday March 21, 2022 @03:17PM (#30758476)
https://devblogs.microsoft.com/oldnewthing/20090306-00/?p=18...

Windows uses the Gregorian Calendar as its epoch.

Re:Please put units in names by kqr on Monday March 21, 2022 @12:35AM (#30750323)
I'm not sure I agree. When I convert fields to DateTime, I remove the unit suffix from the name. The DateTime is supposed to be an implementation-agnostic point in time. It shouldn't come with any units, and nor should they be exposed by the internal implementation.

The factory method used to convert e.g. Unix timestamps to DateTimes, now that should indicate whether we're talking seconds or milliseconds since epoch, for example, and when the epoch really was.

Re:Please put units in names by gregmac on Monday March 21, 2022 @12:47AM (#30750405)
They do:

    .ToUnixTimeSeconds()
    .ToUnixTimeMilliseconds()
    DateTimeOffset.FromUnixTimeSeconds(Int64)
 
   DateTimeOffset.FromUnixTimeMilliseconds(Int64)

https://docs.microsoft.com/en-us/dotnet/api/system.datetimeo...

https://docs.microsoft.com/en-us/dotnet/api/system.datetimeo...

Re:Please put units in names by stevesimmons on Monday March 21, 2022 @02:44AM (#30750989)
How does the C# DateTime type distinguish between dates and a point in time whose time just happens to be midnight?

Much of the C# code I've seen uses names like start_date with no indication of whether it really is a date (with no timezone), a date (in one particular timezone), or a datetime where the time is significant.

I'm certainly not a C# developer, though my quick reading of the docs suggests that the DateOnly type was only introduced recently in .NET6.

Re:Please put units in names by Sharparam on Monday March 21, 2022 @09:23AM (#30753826)
Yeah, before the new DateOnly (and TimeOnly) types, there was no built-in way in C# to specify a plain date. NodaTime[1] (a popular third-party library for datetime operations) did have such types though.

[1]: https://nodatime.org/

Re:Please put units in names by pharmakom on Monday March 21, 2022 @02:25AM (#30750882)
F# has unit support in the type system :)
Re:Please put units in names by S04dKHzrKT on Monday March 21, 2022 @02:43AM (#30750982)
An example for unfamiliar folks.

Many Languages

  var lengthInFeet = 2;
  var weightInKg = 2;
  
  var sum = lengthInFeet + weightInKg; // Runs without issue but is an error
F#

  [<Measure>] type ft
  [<Measure>] type kg
  
  let lengthInFeet = 2<ft>
  let weightInKg = 2<kg>

  let sum = lengthInFeet + weightInKg // Compile time error
More info at https://fsharpforfunandprofit.com/posts/units-of-measure/
Re:Please put units in names by osigurdson on Monday March 21, 2022 @02:49AM (#30751008)
What precisely could have been changed to make you realize that C# DateTime is not the same as Unix time? Perhaps Ticks could be renamed to Ticks100ns but I'm not sure how to encode the epoch date such that it is not necessary to read any documentation. I suppose the class could have been named something like DateTimeStartingAtJan1_0001 but obviously would have been ridiculous.

Naming is an optimization problem: minimize identifier length while maximizing comprehension.

Re:Please put units in names by justsomehnguy on Monday March 21, 2022 @03:00AM (#30751059)
> how to encode the epoch date such that it is not necessary to read any documentation

And you need to read the docs to know why some systems use 1970 as the reference point. Should we rename it to Unix1970datetimeepoch everywhere?