[Bots Open Source EDI Translator] Date Format

245 views
Skip to first unread message

Akhilesh Kumar

unread,
Dec 14, 2011, 1:35:36 PM12/14/11
to bots...@googlegroups.com
Hello Henk-Jan,

I'm trying to convert the date format from DD/MM/YYYY to DD/MM/YY (8 chars) but not able to find any function to do so.
All I got from one of the Plugin is this function time.strftime('%Y%m%d%H%M') but this does not gives any output. It only gives the current Date/Time. 
Not able to proceed with the translation.

If you can please help me find related functions to play with Date/Time conversions (to different formats) and String conversions (like String Concatenation, Extracting Substring from a String, Lower, Upper, etc).

Regards,
Akhilesh

Jean-François

unread,
Dec 14, 2011, 2:59:46 PM12/14/11
to Bots Open Source EDI Translator
Hi Akhilesh,

I've been working a lot with date/time format, especially when facing
the problem of daylight time change...

Here is an example of my code, using time.strftime and time.strptime

#format is EDI 203, meaning CCYYMMDDHHMM > XML format is CCYY-MM-
DDTHH:MMZ
# converting a string ('201112010500') into a date/time format (use
time.strptime(date value, date format)
creationDT=time.strptime(inn.get({'BOTSID':'UNH'},
{'BOTSID':'DTM','C507.2005':'137','C507.2380':None}),'%Y%m%d%H%M')
# putting the date time value into my output file (use time.strftime
to convert the date/time value (creationDT) into a string, according
to the specified format ('%Y-%m-%dT%H:%MZ'))
out.put({'BOTSID':'ConsumptionMeasurement','CreationDateTime':'','CreationDateTime__v':time.strftime('%Y-
%m-%dT%H:%MZ',creationDT)})

Hope this helps.

JF


On 14 déc, 19:35, Akhilesh Kumar <k.akhil...@gmail.com> wrote:
> Hello Henk-Jan,
>
> I'm trying to convert the date format from DD/MM/YYYY to DD/MM/YY (8 chars)
> but not able to find any function to do so.

> All I got from one of the Plugin is this function *
> time.strftime('%Y%m%d%H%M')* but this does not gives any output. It only

Akhilesh Kumar

unread,
Dec 14, 2011, 3:26:23 PM12/14/11
to bots...@googlegroups.com
Hi Jean,

Thank you so much for the detailed and quick reply. Let me work on it and let you know in case I see any issue.

Regards,
Akhilesh
--

Regards,
Akhilesh

henk-jan ebbers

unread,
Dec 14, 2011, 3:32:35 PM12/14/11
to bots...@googlegroups.com
Akhilesh,

in bots 2.1.0 there is a function called 'datemask'

use it like this:
converted_date = transform.datemask(old_date,' DD/MM/CCYY','DD/MM/YY')

is easy to backport (mind the import)

henk-jan

On 12/14/2011 07:35 PM, Akhilesh Kumar wrote:
> Hello Henk-Jan,
>
> I'm trying to convert the date format from DD/MM/YYYY to DD/MM/YY (8 chars) but not able to find any function to do so.

> All I got from one of the Plugin is this function *time.strftime('%Y%m%d%H%M')* but this does not gives any output. It only gives the current Date/Time.

Akhilesh Kumar

unread,
Dec 14, 2011, 4:13:45 PM12/14/11
to bots...@googlegroups.com
Hello Henk,

Your method seems to be easy and short. However, I'm getting the following error:

AttributeError: 'module' object has no attribute 'datemask'

My code has the following (pl see if I'm missing something or what is incorrect)

import bots.transform as transform

    BHT04DT = inn.get({'BOTSID':'Claim','CurrentConditionDate':None})          ==> this shows the value in log file.
    BHT04DT1 = transform.datemask(BHT04DT,'DD/MM/CCYY','DD/MM/YY')  ==> ERROR in masking



Akhilesh
--

Regards,
Akhilesh

BikeMike

unread,
Dec 14, 2011, 6:45:27 PM12/14/11
to Bots Open Source EDI Translator
Hi Akhilesh,

This function is new in version 2.1.0

You can easily add it to earlier versions, just edit transform.py and
add the following code at the bottom. I have included "import
collections" in the function, you can put it at top with other imports
if you wish.

Kind Regards,
Mike

---- code ----
def datemask(value,frommask,tomask):
''' value is formatted according as in frommask;
returned is the value formatted according to tomask.
'''
import collections
if not value:
return value
convdict = collections.defaultdict(list)
for key,value in zip(frommask,value):
convdict[key].append(value)
terug = ''
for c in tomask:
terug += convdict.get(c,[c]).pop(0)
return terug

Akhilesh Kumar

unread,
Dec 15, 2011, 3:27:20 PM12/15/11
to bots...@googlegroups.com
Hello Mike,

Thanks for the code. I added to the transform.py as directed, however I'm getting the following error:

    BHT04DT3 = transform.datemask(BHT04DT,'DD/MM/CCYY','YYYYMMDD')
  File "D:\Python27\lib\site-packages\bots\transform.py", line 460, in datemask
    terug += convdict.get(c,[c]).pop(0)
  IndexError: pop from empty list

Just a note that BHT04DT is a string and not a date in my code, could that be a problem. 

BHT04DT = inn.get({'BOTSID':'Claim','CurrentConditionDate':None})

CurrentConditionDate  is a AN data type in the grammar.

If you can let me know some string manipulation functions too, I can try using that to get the date extracted in the format required by BHT04 element.

Regards,
Akhilesh

BikeMike

unread,
Dec 15, 2011, 5:52:06 PM12/15/11
to Bots Open Source EDI Translator
Hi Akhilesh,
I think this is because you have different masks for the "from" and
"to". The "to" mask cannot contain any extra characters not in the
"from" mask.

BHT04DT3 = transform.datemask(BHT04DT,'DD/MM/CCYY','YYYYMMDD')

should be
BHT04DT3 = transform.datemask(BHT04DT,'DD/MM/YYYY','YYYYMMDD')
or
BHT04DT3 = transform.datemask(BHT04DT,'DD/MM/CCYY','CCYYMMDD')

The content of the string does not matter, the datemask function will
just rearrange the characters.

There are a lot of useful ways to manipulate strings in Python. The
links below are good reference points.
http://docs.python.org/tutorial/introduction.html#strings
http://docs.python.org/library/stdtypes.html#string-methods

Kind Regards,
Mike

Akhilesh Kumar

unread,
Dec 16, 2011, 3:11:52 AM12/16/11
to bots...@googlegroups.com
Hi Mike,

Thank you so so much... Got it now.

Appreciate your quick responses to everyone in the group.

And thanks for the string manipulation links too..

Regards,
Akhilesh
Reply all
Reply to author
Forward
0 new messages