back to article 'Just give me any old date and I'll make it work' ... said the VB script to the coder

Is it that time of the week again already? You've reached Line Break, our weekly roundup of terrible code you've seen in the wild. Today, we're going to delve into submissions from reader comments rather than the usual postbag. Line Break article comments are such a rich seam of snippets and anecdotes, it would remiss to not …

Re: VBA date handling has taken at least five years off my lifespan

The UK tax year starts on the 5th of April because of our transition to the Gregorian calendar from the Julian, doesn't it? We skipped days in the calendar to make the transition, and merchants rebelled against the idea of being taxed for non-existent days... So the end of year date was moved back to placate them.

Computerphile/Tom Scott made a lovely video on handling time zones, which is relevant to this:

https://www.youtube.com/watch?v=-5wpm-gesOY

I'd tend to agree - if it's at all possible, rely on the libraries that already handle this stuff. As you found, if you have to handle this yourself it rapidly becomes a very deep rabbit hole...

9
0
Pint

Re: VBA date handling has taken at least five years off my lifespan

"The UK tax year starts on the 5th of April because of our transition to the Gregorian calendar from the Julian, doesn't it? We skipped days in the calendar to make the transition, and merchants rebelled against the idea of being taxed for non-existent days... So the end of year date was moved back to placate them."

Correct. The year used to start on 25th March but was moved to 1st January at the same time. Allowing for the 11 days correction for missed leap years we get 5th April.

10
0
Silver badge

Re: VBA date handling has taken at least five years off my lifespan

All this stuff is nothing compared to what you come across in medieval documents which tend to be dated with reference to religious dates such as "Sunday the feast of the decollation of St. John the Baptist". If you're lucky an archivist has already translated the dates (in 1322 that was 29 Aug BTW). Some of them are ambiguous such as "Thursday in Easter week": did they mean before or after Easter?

8
0
Silver badge

Re: VBA date handling has taken at least five years off my lifespan

"merchants rebelled against the idea of being taxed for non-existent days"

I think it was more a case of being able to handle year long contracts. If, for instance, you'd hired a shepherd or borrowed £10 for a year on Lady day 1752 you wanted the arrangement to last the full 365 days.

4
0
Silver badge
Pint

Re: VBA date handling has taken at least five years off my lifespan

PS: "...Tom Scott... ...time zones..." (Great video)

PS: "...rely on the libraries..."

And yet, a recurring theme in these stories is that relying on libraries (or other people's code) often turns up their foolish errors. That matches my experience; that the people that write code for libraries sometimes aren't even as careful a coder as I can be. Oftentimes, they're awfully sloppy in their coding and even worse in their documentation.

.: You can't win.

13
0

Re: Medieval dates

Oh, tell me about it - I did my BA in Medieval Lit., and made some editions of various manuscripts. In addition to the day/month being determined by religious festivals, very few people used AD and instead went off the regnal year - so "St. Stephen's day in the third year of the reign of X", which means you need to know the date and the year the king was crowned. Oh, and every bloody country (and sometimes just cities) in Europe having a different year system (Iberians counting from the Romans, for example) - such fun.

I have to say, though, that I never found medieval dates to be too ambiguous. Confusing as hell, yes, but there's not really a way to confuse 03/04 with 04/03 as we have now. To address your example above, "Easter Week" is the week after Easter - the week before is, as every good Catholic schoolboy knows, Holy Week.

15
0

This post has been deleted by its author

Re: Medieval dates

Ah, but which Easter? Even various branches of the Christian religion don't always agree on that!

4
0

Re: VBA date handling has taken at least five years off my lifespan

Correct. The year used to start on 25th March but was moved to 1st January at the same time. Allowing for the 11 days correction for missed leap years we get 5th April.

But the tax year starts on the 6th April - post transition to the Gregorian calendar it was then advanced another day to make up for the "missing" leap year in 1800. No such amendment was made in 1900 though, and the situation didn't arise in 2000 so it's probably considered fixed to the "new" calendar now.

Must dash, George is about to say how he's going to sting us this coming 6th April...

1
0
Silver badge

Re: VBA date handling has taken at least five years off my lifespan

"Some of them are ambiguous such as "Thursday in Easter week": did they mean before or after Easter?"

Pagan! The week before Easter is called Holy Week, and it contains the days of the Easter Triduum, including Maundy Thursday, commemorating the Maundy and Last Supper, as well as Good Friday, commemorating the crucifixion and death of Jesus. In western Christianity, Eastertide, the Easter Season, begins on Easter Sunday and lasts seven weeks, ending with the coming of the fiftieth day, Pentecost Sunday. In Orthodoxy, the season of Pascha begins on Pascha and ends with the coming of the fortieth day, the Feast of the Ascension.

TL;DR: they did mean "after Easter".

2
0
Anonymous Coward

Re: VBA date handling has taken at least five years off my lifespan

"<day> in <Sunday> week" always means after the feast, as weeks start on Sunday.

Even without that, the week before Easter is "Holy Week", never "Easter Week".

1
0
Silver badge

Re: Medieval dates

"Ah, but which Easter?"

Easy. Context / location.

1
0
Anonymous Coward

Re: Medieval dates

Local context still gets confusing. Ask a Belfast man when he's going on holiday and he'll likely tell you "July, I'm taking the 12th week". "How many weeks are there in July over there?!"

4
0
Silver badge

Re: Medieval dates

'To address your example above, "Easter Week" is the week after Easter - the week before is, as every good Catholic schoolboy knows, Holy Week.'

I'm inclined to agree but what was the usage at the time of the document by the clerk who wrote it? The YAS archivists gave that one a miss.

2
0
Silver badge

Re: Medieval dates

"July, I'm taking the 12th week"

Don't forget the Little Twelfth (July 1st). The Boyne anniversary is another one that got shifted 11 days.

0
0
Anonymous Coward

Re: VBA date handling has taken at least five years off my lifespan

> All this stuff is nothing compared to what you come across in medieval documents

Not to mention that at different times in different places different people had different ides as to what date it was or when the year started.

0
0

Re: VBA date handling has taken at least five years off my lifespan

Um, the tax year starts on 6th April.

0
0

$userId = cookie('userId');

I am guilty of this, but not for a client website. I was learning Classic ASP, and I was sixteen, and this was basically how I wrote my login system for my own blog/CMS thing. When I extended it to other users, it was simple; the cookie for the username was already present.

But at least I had the foresight to realise that after a few weeks that this wasn't the right thing to do (and discovered sessions). And I wasn't being paid to do this. And it wasn't my FT job...

5
0
gv

Visual Basic

"Finding the bad calls is more or less impossible, given the lack of VB code analysis tools."

Judicious use of grep (you can even get it for Windows) is encouraged.

3
0

Re: Visual Basic

Also MZ Tools - I'd estimate a 20-30% productivity boost using that VBA add-in.

0
0
Silver badge

Re: Visual Basic

I was certainly a grep user at the time, though whether I was judicious I leave to others to decide. Unfortunately, grep doesn't solve this sort of problem.

Your mission is to find cases where a function or procedure that expects a Date (probably along with lots of other arguments) is passed a String or Variant. The first problem is that the argument list is frequently stacked over several lines, because developers have been encouraged to use long variable names. All grep can do is output the line with the function name (current greps can output several lines of context, but this was 2003). Even if you manage to output the complete call with all its arguments, how do you tell what type they are? They might be local variables, module-level variables, values returned from other functions, or expressions evaluated when the call is executed. Tracing each of them back to the point where the type is defined is a non-trivial task.

4
0
Silver badge

Re: Visual Basic

If you 'less' a file, I seem to recall you can search within it (which would give you the contextual position of the code).

Or has my mind finally slipped into senility? :)

0
0
Vic
Silver badge

Re: Visual Basic

Your mission is to find cases where a function or procedure that expects a Date (probably along with lots of other arguments) is passed a String or Variant

Ah, so you're an awk man[1].

Vic.

[1] I wrote some code a few years back to expose the symbolic constants from C header files as Forth words. The heavy lifting in that was all done in awk. It's worth the effort...

1
0

Fun VB bug.

We had an ID that was rapidly approaching the max for a 32 bit int in a critical application. VB having no 64 bit long type, the decision was to use the currency type as it was a variant and a 64bit long underneath. We never need to do any operations on this, so it was all fine.

All good up till the point where we pass the ID to a com object. The com was expecting a 64bit long.

For no good reason, every time we pass in the ID, by the time it hit the COM code, it was incremented by 1. Tried a few things, make the COM expect a variant and convert to 64bit long, make it expect a currency type and convert to long, to no avail.

Eventually we ended up passing it as a string and having the COM expect a 64bit long. We still don't know what was happening, probably at some point in the COM interface the currency type was being converted to a float and the conversion to 64bit long was off by one, but we just don't know.

So now we have an integer ID that is a currency type variant that needs to be passed around as a string, just so it can be recognized correctly as a long on the other end.

9
0
Silver badge

One of the reasons that I hate loose-typed languages like VB.

If something's an int, or a string, and you need to make it a long - YOU NEED TO DO IT. Don't just rely on the computer to perform magic guesswork as to what the string intended and interpret it how it sees fit.

Dates in strings are a classic source of such problems, for exactly the reasons described in the article and comments.

Yet, even the oldest of languages (C) won't let you arbitrarily convert from some string to some date. It has a highly-specified date structure, and plenty of functions for getting strings OUT of that but nothing for putting strings back INTO it because of the nightmare of various interpretations.

Sure, when I was younger and toying with VB 1/2/3, it was cool to just throw things at VB and let it convert them to what you intended, But it's the one time that it matters that it won't understand what you intended correctly.

How many people really turn on Strict typing in VB? And even that's not perfect.

1
0
Anonymous Coward

You think that's bad?

All 10Billion lines of spaghetti code was done by a million untrained monkies smashing their fists on a typewriter.

We were hoping for Shakespeare, instead we got Windows 10.

-Signed, Microsoft.

13
13
Anonymous Coward

Re: You think that's bad?

if you think Windows was written by untrained monkeys, you have not seen some of the code I'm working with ... at least Windows works (even if against the user)

3
0
Anonymous Coward

Re: You think that's bad?

If you think the code you're working with was written by untrained monkeys, you haven't seen the code I write. :-(

3
0
Anonymous Coward

Re: You think that's bad?

I was trained on magamps. Now I write embedded software. If there's one thing worse than an untrained monkey it's a self taught monkey in the wrong discipline.

- Anon, because my career depends on my company thinking that I know what I am doing.

1
0
Anonymous Coward

Finding dates and times in text

My hobby is trawling the web pages of a few hundred music groups to produce a schedule of their forthcoming performances anywhere in the world. Many of the announcements are in a narrative posting - so a function is needed to identify and extract the possible future date/time from a mass of text.

It seemed fairly easy on first thoughts - yyyy/mm/dd, dd/mm/yy, mm/dd/yy, dd/mm/yyyy. Then came variants using dots or dashes instead of slashes - and some with spaces too. Some had leading zeroes in their months and days - not to mention regular typos.

Many leave out the year - or even the month - although they are sometimes in a page/section header. Year changes can be signalled by the order of entries - as the month suddenly jumps backwards. However one page had entries without any year reference, in the random order that the performances were booked.

The locale of the group is not a tie breaker for the dd/mm mm/dd variants. A European group page might announce a USA tour with US format dates.

Then there are the month name variants in many languages - and their different ways of expressing that format. 20 January, January 20, 20 de gener. Plus the month abbreviations and different languages' ordinal suffixes like 1st and 1er. "May" and "March" in several languages is the same word as a verb.

To cater for all the encountered variants and typos there are now functions numbered 1 to 7 - with awkward supplements of 2A, 5A, and 7A. Each caters for many variations on a theme - picking its first match that satisfies the validation criteria. The human eye is then presented with a selection of best guesses - together with part of their surrounding text.

Times? A sample of 9am/pm 9a.m 9 uhr 9h 9.00 9:00 09:00 21:00 with varying spaces - preceding or succeeding the date.

No attempt has yet been made to handle relative expressions like "next Saturday", "this Saturday", etc. Too many groups just post an image of the performance poster.

The code is as rambling as this attempt to describe its requirements.

15
0
Silver badge

Re: Finding dates and times in text

Have you tried training it to play Go yet?

10
0
Anonymous Coward

Re: Finding dates and times in text

"Have you tried training it to play Go yet?"

AI has crossed my mind as an area to revisit for things like neural networks.

0
0

Re: Finding dates and times in text

there are now functions numbered 1 to 7 - with awkward supplements of 2A, 5A, and 7A.

Who needs descriptive function names, eh?

0
0
Anonymous Coward

Re: Finding dates and times in text

> 20 de gener

Tracking Antònia Font, aren't we?

0
0
Anonymous Coward

Re: Finding dates and times in text

"Who needs descriptive function names, eh?"

The numbers are only the end of the function name - the text part is very descriptive. "ExtractDateAndTimeFromTextTypenn". They all get called to analyse any piece of text - and the user sees all the apparently valid results. The results always contain the surrounding context and "type" that produced any particular value. That's why there are "nA" versions - for when it was judged that it would impossible in the "n" function to validate for the new variant without invalidating another good variant.

I once worked on a hardware and O/S legacy upgrade where the constraint was to not have to recompile any of the customer's applications. They had lost the source to some of them. We looked at another one that had been coded according to a mandatory departmental standard.

All the routines had a name that was constructed from of a set of letters that were determined by its hierarchy in the design. The lower it was - the more letters it had eg "AAA" and "AAB" were both called from "AA". All the JMP and BRANCH labels within the routines were the same letters followed by a number in strict order of use eg AAE14. I suspect the variables were given similarly constructed names.

2
0
Silver badge

Re: Finding dates and times in text

Many of the announcements are in a narrative posting - so a function is needed to identify and extract the possible future date/time from a mass of text.

Well, the

<time />
element was supposed to help there but seeing as it does not require the browser to render the value in the user's locale, it was basically just another abortive microformat: worked required by users but only for the benefit of computers.

0
0

Pretty sure it'd be <lf> not <cr> as a line break (ooh, title drop), seeing as it was Unix and not classic MacOS!

2
0
FAIL

Sound Problem

Many years ago I was called out to a problem with a pc which had no sound. Checking all the settings everything was in order and I had no idea what was the cause util i figured that I had to select MUTE yes MUTE, well I nearly jumped out of my chair when the sound blasted out a full Tilt.

0
0
Silver badge

Re: Sound Problem

Like the old Start menu, when you wanted to stop the PC.

Or when you get a message like "Cancel the print job? OK. Cancel." - er... what do I press?

Human interface designers never solve these things, they just introduce ten thousand tons of junk instead that make them the least of your worries.

I once spent 20 minutes, with my boss and I trying to work out how to close a full-screen Metro app on a fresh Windows 8 test install, on a touchscreen computer (i.e. designed for Windows 8). We deliberately refused to read the manual (like our user's would have) and honestly never got there (even when we tried all the swipes, because apparently you have to start at the VERY TOP of the screen for it to work).

In the end, we just installed Classic Shell and set the default to go to the desktop and Windows key to open a Start Menu instead of Metro.

8
1
Bronze badge
Facepalm

Re: Sound Problem

Like the old Start menu, when you wanted to stop the PC.

When I was a student we had Flexowriters - Yes folks, Flexowriters, make a typo today and find it tomorrow. - and offline telex machines. One night I was alone, playing with a telex. But how do you stop that noisy thing? Whatever I tried, nothing worked. Finally it turned out that the telex switched itself off when you did not touch it for a couple of minutes.

1
0

People Love to hate VBA

Everyone complains and moans about VBA, but in reality its a very flexible and powerful language, that's far too often abused by people who think they can code, but cant, and then they blame VBA/Microsoft for their own deficiencies.

3
0

Re: People Love to hate VBA

My complaints aren't about VBA, when I write VBA it tends to work fine (barring typos, etc). I like that it's (fairly) logical and easy to read (as opposed to C# which to a beginner is a nightmare to debug. I once had to track down a memory leak in C# which none of the actual developers seemed to care about despite it actually being their jobs on the line, quite literally as it happened). My complaints are that they can't keep VBA code consistent between different versions of Office (such as completely changing the way it treats the folder structure on a mac between versions 2011 and 2016) and how they can introduce new bugs in later versions that require putting in pauses in the code to allow the code to work.

1
0
Silver badge
WTF?

Re: People Love to hate VBA

Do you really mean VBA or VB?

I can think of some nice things to say about VB, or more basically BASIC, as it was the first language I ever came across.

But VBA is an unmitigated disaster.

1
0
Bronze badge

Re: People Love to hate VBA

I love to love VBA. But this feature of VB/VBA/VBS, which MVP Michael (michka) Kaplan memorably dubbed "evil date guessing", is a bug that did nobody any favours.

However, the memory of the original poster is incorrect, because although VB/VBA/VBS had this flaw, it wasn't in the thing called cdate( )

2
0
Bronze badge
Holmes

Arrr -back in me day, we didnae have rocks!

I had to add some extra functions to 4K of hand-assembled, hand-patched, 8085 code in EPROM's.

To do this properly I decided to disassemble the patch-work of code, using ".origin" directives to carefully insert each hand-crafted patch in the right place in the EPROM set until I could eventually re-assemble an exact 1:1 match of the existing EPROM's. Then get rid of the ".origin", rebuild, fix some timing constants because the code-loop was now 30% smaller and faster.

Then .... sometimes the parameters for the new functions didn't work properly.

Hmm. I had an expensive logic analyser, with a hardware debugger for the 8085, so I quickly found out that two memory locations in RAM changed, one of which was a local variable in my subroutines.

But ... the problem was HOW it changed! I could not find it!! No instruction referenced any of the locations in any way (the logic analyser could see all address pins), no DMA, no IRQ .... and it was not just that board - all of them behaved the same way.

After 3 weeks I finally gave up and simply defined two 8 bit variables placed over the offending locations so nothing would be placed there. The code still runs ....

12
0
Thumb Up

Re: Arrr -back in me day, we didnae have rocks!

Binary bandaids, brilliant!

2
0

That C# code

For some reason a previous employer wrote their own scanning software. It was created as a test project to see if it could be done with no intention of actually being used in a live environment. It very quickly became a live application, scanning batches of coupons and saving the scans in their own individual folders and storing a reference in a database so they were easy to find. The reference was a barcode on each coupon, associated with a campaign and a person. All worked fine, most batches could be upwards of 20 coupons and everything was hunky dory. Except for one client.

One client decided instead of a tear-off coupon, or a small complimentary slip sized coupon they'd have full sized A4 coupons. The scanner would fail after 8 or 9 scans. Because of the way the batches were processed they couldn't be split into smaller groups for scanning. They'd been having this issue for several month before it was decided to hand the issue to myself as a "learning exercise" to familiarise myself with C#. So I build in some logging into the code, as it runs it drops a line of text into a text file, the idea being the last line of text should give a clue as to what it's doing when it crashes.

It crashes at the same point each time, at the point it opens the scanned image and tries to save it. The exact same thing it's done over and over again for every other client's coupons. On the PC we run the code and turn on system monitoring and see a spike in memory usage at the same point it crashes.

Turns out it's saving the images into an array before saving to file and can't handle the memory usage. A quick re-write and it now scans and saves individual images. It still crashes. A final re-write and some extra code to garbage handle the now orphaned image memory and it's all fixed (and runs a lot quicker due to no longer hogging the PC's memory). For some reason, despite C# supposedly automatically handling memory allocation it wasn't doing so.

More worryingly for me was why the issue was allowed to continue, then get handed to a complete novice at C#, when they employed a team of 4 experienced developers. That said I could never understand why they were coding entire SQL queries into their code instead of passing parameters to a stored procedure.

4
0

If running on a SPARC then I'd wager that the problem was data alignment issues, aligning the structs by rearranging the order and padding where required, it's helped a lot in the past.

It sucks though when the structs are binary representations such as packets :)

1
0

Anyone else remember?

The userid anecdote reminded me of the good old days of Unix when the mail command would populate the From field of an outgoing email using the value of the LOGNAME shell variable...but the shell did not prevent you from changing LOGNAME, so you could make the email appear to be from whoever you wanted.

3
0

Re: Anyone else remember?

Reminds me of the early days of Demon Internet and their DOS news and email software.

You could update the mail headers to spoof certain things, but the main thing was that it included the computer and drive name within the header. So my C:Drive was renamed Hell and the computer was called Hades

4
0

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon

Forums

Biting the hand that feeds IT © 1998–2017