1

I have to receive a string representing a date to process through time.strptime in a format similar to %d%m%Y but the thing is that when the day number is less than 10, then the day will be presented as a one digit number, not a two digits one as the %d format requires.

That means that

'8052012'

will be transalted as

day 08, month 05, year 2012

But with the string

'2052012'

strptime gives me

day 20, month 5, year 2012

instead of

day 2, month 5, year 2012

Is there a way around this default behavior? (perhaps to force that the %m should be a 2 digits number so strptime will have no option that to use what's left for the day?)

EDIT: Please note that on the input string, the month ALWAYS has two digits, even if it can be represented with a single one. The problem here is just the day that may be represented with one or two digits, depending on magnitude...

Javier Novoa C.
  • 11,257
  • 13
  • 57
  • 75
  • How can you tell if the date is 20/5/2012 or 2/05/2012? – Joel Cornett May 09 '12 at 18:53
  • through context... I'm receiving this strings on certain real dates, so 20-05-2012 hasn't happend yet, then it is 02-05-2012 – Javier Novoa C. May 09 '12 at 18:54
  • What about 20/04? isn't that ambiguous? – Joel Cornett May 09 '12 at 19:01
  • mmm I don't understand what do you mean by ambiguous... 20042012 is, evidently, 20/04/2012 ... – Javier Novoa C. May 09 '12 at 19:03
  • what if you get 2042012? – Joel Cornett May 09 '12 at 19:06
  • thats 2 april 2012, month ALWAYS has two digits on the input string, so there's no problem with the month, the problem is only the day. The input string is given to me as a day, with one or two digits depending on magnitude, and the rest is a normal %m%Y string ... The problem is, then, that strptime interprets that string 2042012 as 20 april 2012, instead of 2 april 2012... – Javier Novoa C. May 09 '12 at 19:08
  • 1
    Ohhhhh. You should accept Steven Rumbalski's answer then. – Joel Cornett May 09 '12 at 19:12
  • ok, I'll do that... But isn't that some kind of bug or nasty default behaviour? that a %d%m string will always consider the day string (or the first format on a string with many %'s) and that one could not configure that the second format will have 2 digits, and so...? any way, thanks! – Javier Novoa C. May 09 '12 at 19:14

3 Answers3

6

You know more about this data than time.strptime can know from the format strings.

Remove the ambiguity by prepending '0' when the date-string has a length of 7.

Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
1

For me it looks like problem with printing that string. Usually %d return 2 digits (see: http://docs.python.org/library/time.html#time.strftime). Maybe you convert such string to long or int value and print it?? If you really need to parse such value suppose you should verify lenght of such entry.

ddzialak
  • 1,042
  • 7
  • 15
0

You can try counting from the end:

dd='2052012'
print 'Day', dd[:-6], 'Month:',dd[-6:-4], 'Year: ', dd[-4:]
f p
  • 3,165
  • 1
  • 27
  • 35