Converting dates in CSV programmatically

Status
Not open for further replies.

crombie

Ars Legatus Legionis
20,350
I have run into an extremely frustrating problem. In a new tool that we are using we have reports that are exported in a CSV format. The problem I am running into is the date fields are exporting as:

dd/mm/yy hh:mm AM/PM

I want the date to be saved in that file as:

mm/dd/yyyy HH:mm

For the purposes of this process the only thing I cannot do, as it is very low on the vendors priority list, is have them export the data in the format I want. Some day they might do it, but not any time soon.

The reason for converting the data is that way it can easily be imported into Access, and all the queries I used to run through an ODBC connection with our old vendor will now auto-magically work on reports from our new system.

Right now I have a kludge, but I am worried about memo field truncation:

1) Set the time in Regional settings to 24 hr (I do not mind this being a permanent change)
2) Use a .reg file to set the short date format to dd/MM/yy
3) Open the CSV file in Excel, and save the file (this changes the time format to 24 hr)
4) Import the data into an existing table in Access
5) ???
6) Profit!
6a) Run another .reg file to return the short date to MM/dd/yyyy

Is there any other Windows installable software package that can manage the CSV file for me to:

1) Change the date format to mm/dd/yyyy
2) Change the time format to 24 hour
3) Make certain the memo fields are not truncated (I am not positive this is happening, but it has occurred in the past)

Using the import 'Results Link Specification' tool in Access only results in import errors if my Regional settings are not setup like my kludge. I really tried to use Google on this one, but any results I do find are ancient and not relevant to the task at hand.
 

puffinus

Ars Praefectus
4,381
Subscriptor
Crombie":iq8h0g7x said:
Is there any other Windows installable software package that can manage the CSV file for me to:

1) Change the date format to mm/dd/yyyy
2) Change the time format to 24 hour
3) Make certain the memo fields are not truncated (I am not positive this is happening, but it has occurred in the past)

I would treat it like a text file issue/regular expression problem.

Flex (as in lex/flex/bison) works on Windows, as do perl and other tools.

Without knowing why the memo fields are getting truncated, I can't comment on that (such as improper escaping when commas are in the data). If that's the issue, you might consider translating to tab-delimited before the import: MS tools seem to like that a lot better.
 

crombie

Ars Legatus Legionis
20,350
Actually it is a pretty stupid way that Excel deals with single lines that begin with - I.e.:

-Crombie

becomes

#NAME#? (when you hover over it seems Excel treats it as a formula so it actually becomes =-Crombie)

in Excel, but:

-Crombie
-Is Awesome

is fine. I think I may be remembering saving a CSV as an XLS file, and it truncates the field to 255 characters. I will give flex a try, thanks!
 

puffinus

Ars Praefectus
4,381
Subscriptor
Crombie":hjywkte9 said:
I will give flex a try, thanks!

Ok. If you need any help getting started, holler.

Flex produces C code which pretty much any compiler will work with. That gives you a small command line program. Depending on what you do, you can hard wire it to read and write certain file names or just do something like

myflexprog.exe<input.csv>output.tab
(standard windows command line input/output sourcing).

then set up a shortcut, batch file, or whatever's convenient.

I like it because you get small, standalone programs that you can compile for just about any platform and not have to worry about extensive library dependencies.

If you'd rather install perl or something, that's probably more mainstream these days. Lots of options here, even VB macros to do the conversion within Excel or Access.
 

Faramir

Ars Legatus Legionis
36,356
Subscriptor
I haven't written perl in many years, so no guarantees, but something along these lines should do the trick (albiet inefficiently, I don't remember how to embed conditions in a replace statement).

Code:
open($in "<" "input.csv");
open($out ">" "output.csv");
while($line = <$in>) { 
	($day, $month, $twoyear, $twelvehour, $minute, $AMPM) = ($line =~ m!(\d\d)/(\d\d)/(\d\d) (\d\d):(\d\d) ([AP]M)!);
	if ($AMPM == "AM") {$twentyfourhour = $twelvehour;}
	else {$twentyfourhour = $twelvehour + 12;}
	$newdatetime = $month.'/'+$day.'/'.'20'+$twoyear.' '.$twentyfourhour.":".$minute;
	$line =~ s!(\d\d)/(\d\d)/(\d\d) (\d\d):(\d\d) ([AP]M)!$newdatetime!;
	print $out $line;
}
close $in;
close $out;
 

crombie

Ars Legatus Legionis
20,350
ShuggyCoUk":2x34ae96 said:
Why on earth would you want to use anything but ISO 8601 for a textual representation of date in a data exchange format?
Anything based on locale (which you US style date is) is going to be a PITA sometime down the line.

When you get them to change it go with the ISO format...
Why? I have no idea whether or not the data is already stored in this format. The tool I am working with is external to the database. I have no specifications on how the database stores data, and for all I know I could suggest, "Hey, store the data in ISO 8601 format!" and the vendor will reply, "Crombie, the data is already stored in that format. The 'report' (really just a search with export capabilities) has the date in the format that the completely non-technical implementation team requested."

Plus, down the line is not my problem. The EOL for this new product is well past the time when I will have graduated, and will be happily working outside of this partial IT world as an accountant. My reporting needs are simple: can it be easily imported into Access (Y/N)? The format I provide can be easily imported into Access, and that is all I need.
 

crombie

Ars Legatus Legionis
20,350
ShuggyCoUk":3rqyseus said:
Hey, store the data in ISO 8601 format!

I'm not saying store. I'm saying *transfer*

If you want to just do what someone who has no clue says, then leave it behind you that's fine. Just don't think this was a good thing for the wider world.
Well, go right ahead, and write up a business plan for why dates should be 'transferred' in that format. Can you also explain how to adjust it so non-technical staff who download the data (and only read it on their screen) can understand what the presentation of the dates in the ISO 8601 format mean? I need the plan to be written without any of this ISO jargon-ese gobbledy-gook. kthxbai!

We are talking about temporary data that I use to tell staff basic statistics from our system. We are not talking about a system that exports and backs up data for future use, has a wider audience beyond my desk, and is going to be archived in any manner. Once I am done with the data it get's binned. I fail to see how this affects the 'wider world', and makes it a better place.

In the end I had a porcelain throne thought bubble. Link the field as text, and then have queries do the pattern matching from above. That way I completely avoid the step of actually converting the data. I thank everyone else for their valuable suggestions.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
should be 'transferred'

Why the quotes.
You said earlier that this data was to be added to an access database.

Is it or isn't it? If this output file is to read directly by a human rather than imported into a database and the the data viewed through that it is no longer the same thing as you stated earlier.

If the file is not to be read by a human then what the hell are you rabbiting on about. The date will presented to the user in the database in whatever format they want.

As to a business plan: it reduces the probability of errors as it is unambiguous and is supported by almost everything without any additional effort including access.
Not hard was it, no mumbo jumbo there?
 

crombie

Ars Legatus Legionis
20,350
Why the quotes.
Hey, you started the semantics. I wanted to be clear that wasn't my terminology.


You said earlier that this data was to be added to an access database.
Access is the tool >I< use for reviewing this data. For the record the csv file is now linked in the database. It works wonderful with my simple queries from my old way of doing things. The query also presents the date how I want to sort it, and it is AWESOME.

Is it or isn't it? If this output file is to read directly by a human rather than imported into a database and the the data viewed through that it is no longer the same thing as you stated earlier.

If the file is not to be read by a human then what the hell are you rabbiting on about. The date will presented to the user in the database in whatever format they want.
Why do I have to explain how any of the other hundred plus employees are going to review the data when that is not relevant to the issue I was having? Why am I even explaining myself anymore? You are the one nattering on about how going with 'ISO 8601' is a 'good thing for the wider world'. Anyone can see the date in their browser window, they might download the CSV file, they could download a PDF file, they might open the CSV file in a text editor, some might open it in Microsoft Word, technically I believe someone could open it with vi or hell they might even use it on a MAC! :flail: Do you need me to cover all the scenarios of how someone could open, and view a CSV file?

It is low priority on the vendors part to change this export, and will likely never get done.

As to a business plan: it reduces the probability of errors as it is unambiguous and is supported by almost everything without any additional effort including access.
Not hard was it, no mumbo jumbo there?
You know what, I don't think you really get it. Other people need to look at this date, and read it in the format that is accepted where we live. When I give someone the time as 9:47Z, what do you think the first question will be? I can tell you: "What the HELL is 9:47Z?" Your business plan lacks a description which shows a non-technical user how to convert that time to whatever the hell real time it was (in this case it is Atlantic time).

Oh yes, and 'supported by almost everything without any additional effort including access.' Really? You tried this, right? You know it's supported? Because I am thinking you didn't really try it. Your suggestion does not fix a single thing for this process:

file.php


Oh wait, that was the import to create the table structure... yeah. So the linked table to a CSV file with the ISO 8601 format shows the date as... "#NUM?". That converted PERFECTLY. :rolleyes:

And Excel treats the ISO 8601 standard as General, and does not recognize it as date / time - at all.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Why do I have to explain how any of the other hundred plus employees are going to review the data when that is not relevant to the issue I was having? Why am I even explaining myself anymore? You are the one nattering on about how going with 'ISO 8601' is a 'good thing for the wider world'. Anyone can see the date in their browser window, they might download the CSV file, they could download a PDF file, they might open the CSV file in a text editor, some might open it in Microsoft Word, technically I believe someone could open it with vi or hell they might even use it on a MAC! Do you need me to cover all the scenarios of how someone could open, and view a CSV file?

That's fine then.
This is intended as a directly human readable file, Your OP implied (to me anyway) that this was a file used solely as a means to get data from one system into another.

Excel is a pig in that regard (though simply dropping the T and timezone 'Z') are sufficient for it to parse it fine. I suspect the same holds true to Access linked files but I don't have a copy to hand to try it.
I was indeed wrong about access (Jet is a piece of crap). It will output it fine - I erroneously assumed that if it did one it did the other. my apologies.

All that said I'd still use the YYYY-MM-DD but it's your data knock yourself out.
 

topham

Ars Legatus Legionis
13,756
Subscriptor
As another poor sod who's had to deal with more date issues than he'd like: always, always, always use Year month day.
Its the only non-ambiguous date format.
(it doesn't help the Canadian standard date format is different than the US standard date format.)

Never trust excels date import. It doesn't always do what you expect. No, really, you can't even pretend to trust it.)
 

TheBorg

Ars Tribunus Angusticlavius
8,555
topham":x82f3amf said:
As another poor sod who's had to deal with more date issues than he'd like: always, always, always use Year month day.
++

Agreed - if you can specify it, then going from largest units (years) to smallest units (seconds) is easily recognizable by humans and machines alike. Really smart tools that do really dumb things when encountering ambiguous dates have almost no chance of failure with yyyy-MM-dd-hh:mm:ss, at least as far as I've seen.

(it doesn't help the Canadian standard date format is different than the US standard date format.)
You mean, it doesn't help that the US standard date format is different from... oh never mind :)
 
Status
Not open for further replies.