lists.arthurdejong.org
RSS feed

python-stdnum branch master updated. 1.17-11-g7d81eac

[Date Prev][Date Next] [Thread Prev][Thread Next]

python-stdnum branch master updated. 1.17-11-g7d81eac



This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "python-stdnum".

The branch, master has been updated
       via  7d81eac6f26ba0b4abbc6ddc9593ffec6d2ffd47 (commit)
      from  bda2a9c8ac0646ca4661e2d85020ba6e60dbf0b9 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://arthurdejong.org/git/python-stdnum/commit/?id=7d81eac6f26ba0b4abbc6ddc9593ffec6d2ffd47

commit 7d81eac6f26ba0b4abbc6ddc9593ffec6d2ffd47
Author: Alexis de Lattre <alexis.delattre@akretion.com>
Date:   Fri Apr 8 14:10:09 2022 +0200

    Support parsing dates without a day in GS1-128
    
    Date such as '(17)260400' is now properly interpreted as April 30th
    2026.
    
    Closes https://github.com/arthurdejong/python-stdnum/pull/294

diff --git a/stdnum/gs1_128.py b/stdnum/gs1_128.py
index 4bb5912..0bac88d 100644
--- a/stdnum/gs1_128.py
+++ b/stdnum/gs1_128.py
@@ -148,6 +148,14 @@ def _decode_value(fmt, _type, value):
             return datetime.datetime.strptime(value, '%y%m%d%H%M')
         elif len(value) == 12:
             return (_decode_value(fmt, _type, value[:6]), _decode_value(fmt, 
_type, value[6:]))
+        elif len(value) == 6 and value[4:] == '00':
+            # When day == '00', it must be interpreted as last day of month
+            date = datetime.datetime.strptime(value[:4], '%y%m')
+            if date.month == 12:
+                date = date.replace(day=31)
+            else:
+                date = date.replace(month=date.month + 1, day=1) - 
datetime.timedelta(days=1)
+            return date.date()
         return datetime.datetime.strptime(value, '%y%m%d').date()
     elif _type == 'int':
         return int(value)
diff --git a/tests/test_gs1_128.doctest b/tests/test_gs1_128.doctest
index 8bfa9af..035f17c 100644
--- a/tests/test_gs1_128.doctest
+++ b/tests/test_gs1_128.doctest
@@ -104,6 +104,10 @@ 
pprint.pprint(gs1_128.info('(01)38425876095074(17)181119(37)1 '))
 {'01': '58425876097843', '10': '123456', '17': datetime.date(2018, 11, 19), 
'37': 18}
 >>> pprint.pprint(gs1_128.info('|(01)58425876097843|(10)123456|(17)181119(37)18',
 >>>  separator='|'))
 {'01': '58425876097843', '10': '123456', '17': datetime.date(2018, 11, 19), 
'37': 18}
+>>> pprint.pprint(gs1_128.info('(01)38425876095074(17)260400(10)123456'))
+{'01': '38425876095074', '10': '123456', '17': datetime.date(2026, 4, 30)}
+>>> pprint.pprint(gs1_128.info('(01)38425876095074(17)261200(10)123456'))
+{'01': '38425876095074', '10': '123456', '17': datetime.date(2026, 12, 31)}
 >>> gs1_128.info('(03)38425876095074')  # unknown AI
 Traceback (most recent call last):
     ...

-----------------------------------------------------------------------

Summary of changes:
 stdnum/gs1_128.py          | 8 ++++++++
 tests/test_gs1_128.doctest | 4 ++++
 2 files changed, 12 insertions(+)


hooks/post-receive
-- 
python-stdnum