Formula One fans left cringing by Martin Brundle's 'most awkward grid interview ever' with Conor Benn
Heroic Essex dad jumped into freezing ocean to save sailor who fell overboard near Mersea Island
I was too fat to wear shoes. Even Ozempic didn't work and made my hair fall out. But a radical slimming solution helped me shed two thirds of my body weight - and keep it off
Chelsea fans call out star who dressed 'as a janitor' at the club's glitzy end-of-season awards party while team-mates donned their best suits
M&S cyber attack could take 'months' to fully recover from as 'paranoid' staff resort to sleeping in the office amid 'pure chaos'
Revealed: The reason behind Fred & Rose West kids' bitter family rift as siblings have 'nothing to do with each other'
Mushroom chef Erin Patterson bragged about 'hiding mushrooms in everything' to Facebook chums before asking them for advice on how to cook a beef Wellington
Class Action Accuses Toyota of Illegally Sharing Drivers' Data
Read more of this story at Slashdot.
The Essex street named after people 'who helped reshape' postwar Britain
Two Brits are killed and two more injured when their car overturns in horror crash at Moroccan tourist hotspot
Uber announces major shake up to the way customers can pay for rides
Trump promises protection for TikTok, for which he has a ‘warm spot in my heart’
US President Donald Trump has said TikTok will be “very strongly protected” as the made-in-China social network has “a warm spot in my heart”.…
Horror as $4.5M influencer-laden yacht SINKS off Miami... after glam women made a rookie maritime error
Top Tory brands Prince Harry 'entitled' as Palace source 'says deluded Duke has lost touch with reality' amid fallout from scorched-earth BBC interview
I visited Apple's secret testing labs - here's what REALLY happens behind-the-scenes at the Cork campus
Unlikely royal with a very similar timeline to Prince Harry - including the loss of a parent, finding solace in the army then being forced to abandon his duties writes TESSA DUNLOP
Major terror attack 'was just HOURS away' before it was foiled by the special forces and police: Seven Iranians arrested in raids after 'biggest counter-state threat in years'
CodeSOD: A Double Date
Alice picked up a ticket about a broken date calculation in a React application, and dropped into the code to take a look. There, she found this:
export function calcYears(date) { return date && Math.floor((new Date() - new Date(date).getTime()) / 3.15576e10) }She stared at it for awhile, trying to understand what the hell this was doing, and why it was dividing by three billion. Also, why there was a && in there. But after staring at it for a few minutes, the sick logic of the code makes sense. getTime returns a timestamp in milliseconds. 3.15576e10 is the number of milliseconds in a year. So the Math.floor() expression just gets the difference between two dates as a number of years. The && is just a coalescing operator- the last truthy value gets returned, so if for some reason we can't calculate the number of years (because of bad input, perhaps?), we just return the original input date, because that's a brillant way to handle errors.
As bizarre as this code is, this isn't the code that was causing problems. It works just fine. So why did Alice get a ticket? She spent some more time puzzling over that, while reading through the code, only to discover that this calcYears function was used almost everywhere in the code- but in one spot, someone decided to write their own.
if (birthday) { let year = birthday?.split('-', 1) if (year[0] != '') { let years = new Date().getFullYear() - year[0] return years } }So, this function also works, and is maybe a bit more clear about what it's doing than the calcYears. But note the use of split- this assumes a lot about the input format of the date, and that assumption isn't always reliable. While calcYears still does unexpected things if you fail to give it good input, its accepted range of inputs is broader. Here, if we're not in a date format which starts with "YYYY-", this blows up.
After spending hours puzzling over this, Alice writes:
I HATE HOW NO ONE KNOWS HOW TO CODE