195
77
Jun 01 '21
How is this bad code? Am I missing something?
80
Jun 01 '21
It's not UTC. And it it's just the same as saying timestamp = datetime.now
3
48
Jun 01 '21
[removed] — view removed comment
19
0
Jun 01 '21
[deleted]
5
Jun 01 '21
[removed] — view removed comment
1
u/DogmaSychroniser Jun 02 '21
The firm I'm at just has a class called clock which has a getter and setter property that let's you capture the date time and fix it if you need to for a test, elsewise it just calls DateTime.Now()
2
Jun 02 '21
[removed] — view removed comment
1
u/DogmaSychroniser Jun 02 '21
I'd probably build it differently if I had a say in it but I came waaaay late to the party.
13
u/szescio Jun 01 '21
Not sure what they are trying to achieve, but a) for sure theres an easier way b) if its so complicated, put that shit in a helper or provider
1
-4
27
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 01 '21 edited Jun 01 '21
Image Transcription: Code
[The code is in an overview of a Git commit; the first line in the method is highlighted red, and the second line is highlighted green, meaning that the first line was replaced with the second line.]
public ApiResult()
{
TimeStamp = DateTime.UtcNow;
TimeStamp = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
8
u/britishben Jun 01 '21
It might be a bit more readable in diff format - something like this:
public ApiResult() {+ TimeStamp = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); }
- TimeStamp = DateTime.UtcNow;
7
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 01 '21
I prefer not to add characters to the code block that aren't in the code, if possible to avoid. I also think that wouldn't work well with screen-readers (I know visually-impaired programmers are rare, but they do exist), and although it conveys the same information, it doesn't actually describe how that information is conveyed in the image, which I prefer to do as it's a more "direct" transcription that way.
3
u/britishben Jun 01 '21
That's a fair point - I wasn't aware that screen-readers would struggle with it. I guess mine is more an "interpretation" than a transcription, too.
19
Jun 01 '21
[removed] — view removed comment
16
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 01 '21
Oh my god, I feel so stupid... These are things I know
I've fixed it, thanks for pointing it out!
9
13
u/Ooze3d Jun 01 '21
I’m currently working with lots of dates and between formats and offsets, my code looks a little like this. I ended up storing it as a string that auto converts to date as it’s being entered in the database. Angular and Node kept going back and forth with the offset and I couldn’t find a stable way to do it.
11
u/Willinton06 Jun 01 '21
Yeah, he could have said DateTime.Now, the reason the new implementation works is because he’s not using UtcNow
3
Jun 01 '21
Maybe they wanted to remove (floor) milliseconds?
3
u/Willinton06 Jun 01 '21
You can do AddMilliseconds(-DateTime.Now.Millisecond), 10 times more performant
3
u/dabombnl Jun 02 '21
No, you can't. The first call to
DateTime.Nowmay have a different number of milliseconds than the second call for the number to subtract.4
u/Willinton06 Jun 02 '21
My bad, just put the DateTime.Now in a variable like:
var now = DateTime.Now; now = now.AddMilliSeconds(-now.MilliSeconds);1
Jun 01 '21
I know, just saying, that’s also something that this code is doing along with not explicitly being utc
5
u/lookaze Jun 01 '21
I hope they will come up with some better way to handle time was soon as there are a couple of people on Mars. I could deal with some interplanetary standard time before I start converting to local timezones which are only approximations based on where the local star is currently positioned
4
7
u/Cmgeodude Jun 01 '21
I see and understand that this is repetitive, bad code.
DateTimes really are the worst, though.
3
Jun 01 '21 edited Jun 01 '21
I once had to spend an hour arguing with someone that the logs do UTC by default so the timestamp is in-fact current and not old by 7hrs like they thought it was.
Also who can tell if 2021-10-07 is actually the Seventh of October or the 10th of July ? 🤷♂️
3
u/Return-foo Jun 01 '21
My boss told me that he wanted all possible date time formats that we were scrapping from the web converted to our standard. Boy oh boy that was not fun times.
1
3
u/KaiserMazoku Jun 02 '21
DateTimes are always annoying, especially in Javascript when you forget to offset the month by 1.
2
u/Giocri Jun 01 '21
I still have nightmares from when I had to make a parser for an api that didn't respect the datetime format said on the documentation.
8 different date time format from the same api 7 of which undocumented and we had to find out from reading the raw json coming in.
1
2
u/ign1fy Jun 01 '21
The C# DateTime lib is the best I've worked with (C, Java and JS suck at it). However, I did have to write my own function to truncate a timestamp to the nearest second. It was much cleaner than this example.
2
Jun 02 '21
I don’t know why it’s written this way, but I can probably come up with a few valid guesses.
DateTimes are the absolute worst in so many ways and have absolutely bizarre behavior.
1
1
u/DogmaSychroniser Jun 02 '21
The worst part is the string format there will interpolate months and minutes.
0
u/ghillisuit95 Jun 01 '21
I feel like everyone is missing the fact that this is a function called ApiResult() and for some reason it is assigning to what appears to be a global variable and that is it. That is not at all what I would expect a function called ApiResult() to do.
8
u/aaaantoine Jun 01 '21
It's a constructor for a class named
ApiResult. The "global variable" is potentially a read-only property (that can be set only during construction).3
0
u/PFthroaway Jun 01 '21
DateTimeOffset is much better to use. It may not give you the specific time zone, but you know when it was, which is really all that matters.
0
0
0
u/_hoyet Jun 02 '21
I'm assuming the Convert class is able to be mocked in tests. This is probably solid code, with context.
303
u/Yserbius Jun 01 '21
To be fair, DateTimes are hard. Like ridiculously unintuitively hard. There was a time when computer scientists figured everything can be represented as the number of seconds from 1970. We know now that that idea is woefully wrong. You need to know time zones. You need to factor in Daylight Savings. You need to have a different API depending on whether you're talking about an instant in time, or the amount of time during which something is occurring.
So you have bloated messes that look like this because it's honestly the only way to pass that unit test which attempts to roll back a transaction triggered from Venezuela to a server across the International Date Line in the second when Daylight Savings Time switches over.