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
Infected blood scandal victims are 'scared' they won't live to see £11.8billion compensation after raft of delays
When the BBC were first denied access to televising Queen Elizabeth's Coronation but the Earl Marshall changed his mind due to it possibly backfiring on the monarch
The more you look at it, the more you think three years back in Paradise will be enough for Rodgers, writes Gary Keown
Commercial pilot who narrowly escaped UFO mothership had his story buried by the CIA for 30 years
Disturbing new study reveals how everyday number all adults use can be early indicator of CANCER
Sopranos actor Charley Scalies dead at age 84: The Wire vet dies following battle with Alzheimer's
Stansted Airport: extra flights to seven sunny destinations planned for winter 2025
Well-known DJ to join legendary band at summer festival in Essex
After Reddit Thread on 'ChatGPT-Induced Psychosis', OpenAI Rolls Back GPT4o Update
Read more of this story at Slashdot.
JoJo Siwa's heartbroken ex Kath Ebbs seeks comfort in the arms of a friend as they share a kiss after returning to Australia following their brutal split at Celebrity Big Brother afterparty
Plastic surgeon claims to have turned his mom into his sister using surgery... are YOU impressed by results?
The towns being ruined by day-tripper invasions. Selfie-loving tourists cause traffic hell and the High Streets are dying - while the council does nothing
Three dead and 14 missing after two sightseeing boats carrying tourists capsize in China
Co-op cyber attack leaves 20MILLION customers at risk of scams for years to come, experts say
Dunelm's 'colourful' suitcase collection that shoppers 'absolutely love'
Primark's 'gamechanger' fold-away backpack that 'combines style with smart functionality'
'Star Wars Day' Celebrations Hit Fortnite, Disney+, X.com - and Retailers Everywhere
Read more of this story at Slashdot.