Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

strptime format string nasty default

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


jstitch at invernalia

May 9, 2012, 12:38 PM

Post #1 of 5 (331 views)
Permalink
strptime format string nasty default

Hi,

I am using time.strptime method as follows:

I receive an input string, representing some date in the following
format:

%d%m%Y

However, the day part may be a single digit or two, depending on
magnitude.

For example:

'10052012' will be parsed as day 10, month 5, year 2012

Again:

'8052012' will be parsed as day 8, month 5, year 2012

What happens when day is 1 or 2?

'1052012' will be parsed as day 10, month 5, year 2012 !!!!

That's not the expected behaviour! Not for me at least. I mean, in my
case, month will always be a 2 digit string, so there's no ambiguity
problem by pretending that... say '1052012' is correctly parsed.

Is there a way out of here? I know I can pre-parse the string and
append a '0' to it when lenght == 7, but I think that a better way
would be if strptime allowed me to define my format in a better
way... To say that the month is the optional one-two digit part is
just a default, isn't it? Why can't I specify that the day part is the
one with one-or-two digits on the input string...?

Or is there a way out that I don't know yet?

--
Javier Novoa C.

--- Posted via news://freenews.netfront.net/ - Complaints to news [at] netfront ---
--
http://mail.python.org/mailman/listinfo/python-list


yanegomi at gmail

May 9, 2012, 12:54 PM

Post #2 of 5 (315 views)
Permalink
Re: strptime format string nasty default [In reply to]

On May 9, 2012, at 12:38 PM, "Javier Novoa C." <jstitch [at] invernalia> wrote:

> Hi,
>
> I am using time.strptime method as follows:
>
> I receive an input string, representing some date in the following
> format:
>
> %d%m%Y
>
> However, the day part may be a single digit or two, depending on
> magnitude.
>
> For example:
>
> '10052012' will be parsed as day 10, month 5, year 2012
>
> Again:
>
> '8052012' will be parsed as day 8, month 5, year 2012
>
> What happens when day is 1 or 2?
>
> '1052012' will be parsed as day 10, month 5, year 2012 !!!!
>
> That's not the expected behaviour! Not for me at least. I mean, in my
> case, month will always be a 2 digit string, so there's no ambiguity
> problem by pretending that... say '1052012' is correctly parsed.
>
> Is there a way out of here? I know I can pre-parse the string and
> append a '0' to it when lenght == 7, but I think that a better way
> would be if strptime allowed me to define my format in a better
> way... To say that the month is the optional one-two digit part is
> just a default, isn't it? Why can't I specify that the day part is the
> one with one-or-two digits on the input string...?
>
> Or is there a way out that I don't know yet?

Delimiters, e.g. dashes, slashes, etc, can remove ambiguity in the date string. Leading 0s in elements in the date would help as well.
HTH!
-Garrett
--
http://mail.python.org/mailman/listinfo/python-list


grahn+nntp at snipabacken

May 9, 2012, 1:11 PM

Post #3 of 5 (314 views)
Permalink
Re: strptime format string nasty default [In reply to]

On Wed, 2012-05-09, Javier Novoa C. wrote:
> Hi,
>
> I am using time.strptime method as follows:
>
> I receive an input string, representing some date in the following
> format:
>
> %d%m%Y
>
> However, the day part may be a single digit or two, depending on
> magnitude.
>
> For example:
>
> '10052012' will be parsed as day 10, month 5, year 2012
>
> Again:
>
> '8052012' will be parsed as day 8, month 5, year 2012
>
> What happens when day is 1 or 2?
>
> '1052012' will be parsed as day 10, month 5, year 2012 !!!!
>
> That's not the expected behaviour! Not for me at least. I mean, in my
> case, month will always be a 2 digit string, so there's no ambiguity
> problem by pretending that... say '1052012' is correctly parsed.
>
> Is there a way out of here? I know I can pre-parse the string and
> append a '0' to it when lenght == 7, but I think that a better way
> would be if strptime allowed me to define my format in a better
> way... To say that the month is the optional one-two digit part is
> just a default, isn't it? Why can't I specify that the day part is the
> one with one-or-two digits on the input string...?
>
> Or is there a way out that I don't know yet?

You'd have to read the strptime(3) manual page (it's a Unix function,
imported straight into Python, I'm sure). Judging from a quick read
it's not intended to support things like these. I'm surprised it
doesn't parse your last example to (10, 52, 12) and then fail it due
to month>12.

Can't you use a standard date format, like ISO? Apart from not being
possible to parse with standard functions, this one looks quite odd
and isn't very human-readable.

If you have to use this format, I strongly recommend parsing it
"manually" as text first. Then you can create an ISO date and feed
that to strptime, or perhaps use your parsed (day, month, year) tuple
directly.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
--
http://mail.python.org/mailman/listinfo/python-list


jstitch at invernalia

May 9, 2012, 1:25 PM

Post #4 of 5 (315 views)
Permalink
Re: strptime format string nasty default [In reply to]

On 2012-05-09, Jorgen Grahn <grahn+nntp [at] snipabacken> wrote:
>
> You'd have to read the strptime(3) manual page (it's a Unix function,
> imported straight into Python, I'm sure). Judging from a quick read
> it's not intended to support things like these. I'm surprised it
> doesn't parse your last example to (10, 52, 12) and then fail it due
> to month>12.

Well, it doesn't, at least on my Python. I'm using 2.7.3 version

>
> Can't you use a standard date format, like ISO? Apart from not being
> possible to parse with standard functions, this one looks quite odd
> and isn't very human-readable.

No, sadly the input doesn't depends on me :-(

>
> If you have to use this format, I strongly recommend parsing it
> "manually" as text first. Then you can create an ISO date and feed
> that to strptime, or perhaps use your parsed (day, month, year) tuple
> directly.

Ok, I'll do that.

>
> /Jorgen
>

Thanks!


--
Javier Novoa C.

--- Posted via news://freenews.netfront.net/ - Complaints to news [at] netfront ---
--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

May 9, 2012, 1:39 PM

Post #5 of 5 (319 views)
Permalink
Re: strptime format string nasty default [In reply to]

On 09/05/2012 20:38, Javier Novoa C. wrote:
> Hi,
>
> I am using time.strptime method as follows:
>
> I receive an input string, representing some date in the following
> format:
>
> %d%m%Y
>
> However, the day part may be a single digit or two, depending on
> magnitude.
>
> For example:
>
> '10052012' will be parsed as day 10, month 5, year 2012
>
> Again:
>
> '8052012' will be parsed as day 8, month 5, year 2012
>
> What happens when day is 1 or 2?
>
> '1052012' will be parsed as day 10, month 5, year 2012 !!!!
>
> That's not the expected behaviour! Not for me at least. I mean, in my
> case, month will always be a 2 digit string, so there's no ambiguity
> problem by pretending that... say '1052012' is correctly parsed.
>
> Is there a way out of here? I know I can pre-parse the string and
> append a '0' to it when lenght == 7, but I think that a better way
> would be if strptime allowed me to define my format in a better
> way... To say that the month is the optional one-two digit part is
> just a default, isn't it? Why can't I specify that the day part is the
> one with one-or-two digits on the input string...?
>
> Or is there a way out that I don't know yet?
>
You could just right-justify the string to 8 characters, padding with
'0':

>>> '1052012'.rjust(8, '0')
'01052012'
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.