zhikrullah / pyshp

Automatically exported from code.google.com/p/pyshp
MIT License
0 stars 0 forks source link

Date Field Type Improvement #28

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Handling date fields (type 'D') is currently done awkwardly and incorrectly in 
pyshp.

Try writing a shapefile with a 'D' type field, re-opening it for with an 
`Editor`, and saving the file: the date field becomes corrupted because pyshp 
has written it's value as a string version of [2012,02,18], which is "[2012,02".

Issue 6 <http://code.google.com/p/pyshp/issues/detail?id=6&can=1> suggested 
regex parsing of date fields, falling back to parsing out positions, but the 
fact is that this is unnecessary. The DBF format 'D' field must be exactly 8 
characters, with date stored as YYYYMMDD; no delimiters, no ambiguity with size.

I've attached a patch (unified diff format) against current trunk (r72) which 
does the following:
* overrides column size when 'D' type is specified so that it is always exactly 
8 characters
* marshalls to and from the actual Python datetime.date type; no string 
formatting or arrays-of-integers for dealing with dates

Original issue reported on code.google.com by david.ri...@myotisoft.com on 25 Feb 2012 at 10:29

Attachments:

GoogleCodeExporter commented 8 years ago
Attached is a quick test script to show that current 'D' handling is broken for 
read/edit/write. This script writes a date value, opens for edit and saves to a 
new file, then re-opens to print the values (which should be identical).

{{{$ python testcase.py
testing
records: [['first record', [2012, 2, 18]]]

testing_edited
records: [['first record', '[2012, 2']]
}}}

Original comment by david.ri...@myotisoft.com on 25 Feb 2012 at 10:50

Attachments:

GoogleCodeExporter commented 8 years ago
The attached, improved patch handles null values properly, by coercing between 
Python None and Shapefile '0' for null date. It also accepts the literal string 
YYYYMMDD format.

Attached test case shows it working with datetime.datetime, None, and string 
literal.

{{{
$ python testcase.py
testing
fields: [('DeletionFlag', 'C', 1, 0), ['DESC', 'C', 20, 0], ['DATE', 'D', 8, 0]]
record count: 2
records: [['first record', datetime.date(2012, 2, 25)], ['record null date', 
None]]

testing_edited
fields: [('DeletionFlag', 'C', 1, 0), ['DESC', 'C', 20, 0], ['DATE', 'D', 8, 0]]
record count: 3
records: [['first record', datetime.date(2012, 2, 25)], ['record null date', 
None], ['record string literal', datetime.date(2012, 12, 31)]]
}}}

Original comment by david.ri...@myotisoft.com on 25 Feb 2012 at 11:15

Attachments:

GoogleCodeExporter commented 8 years ago

Original comment by jlawh...@gmail.com on 28 Feb 2012 at 5:24

GoogleCodeExporter commented 8 years ago
Hi guys, I am trying to write shapefile adding a Date field, but when I use a 
python datetime object the field get recorded as (i.e. 0000/20/14) (note that 
is 2014-the year) not the right date I chose, but strangely if I add the first 
record as a string, let's say '20140328' the others records become the correct 
datetime value I expected. 

pyshp version is 1.2.0

Any clue? 

Thanks in advance

Original comment by andrefel...@gmail.com on 27 Mar 2014 at 10:55