From: dave on
Quick question. I have to time stamps (now and now2).

now = datetime.datetime.now();
now2 = datetime.datetime.now();

now2-now1 yields me a result in 0:00:00.11221 (H:MM:SS.ssss)

I wanted to know if there is a standard python method or a quick hack
to add an extra zero in the beginning.

So the result I am looking for would be 00:00:00.11221
From: Gabriel Genellina on
On 3 jun, 17:24, dave <davidrey...(a)gmail.com> wrote:
> Quick question. I have to time stamps (now and now2).
>
> now = datetime.datetime.now();
> now2 = datetime.datetime.now();
>
> now2-now1 yields me a result in 0:00:00.11221 (H:MM:SS.ssss)
>
> I wanted to know if there is a standard python method or a quick hack
> to add an extra zero in the beginning.
>
> So the result I am looking for would be 00:00:00.11221

Try the strptime method with a suitable format, like this (untested):
delta = now2-now1
delta.strftime('%H:%M:%S.%f')

http://docs.python.org/library/datetime.html#strftime-strptime-behavior

--
Gabriel Genellina
From: Günther Dietrich on
Gabriel Genellina <gagsl-py2(a)yahoo.com.ar> wrote:

>Try the strptime method with a suitable format, like this (untested):
>delta = now2-now1
>delta.strftime('%H:%M:%S.%f')

Throws an exception:

|Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
|AttributeError: 'datetime.timedelta' object has no attribute 'strftime'

What seems logical, since the documentation doesn't mention an strftime
method for timedelta.



Regards,

Günther
From: Gabriel Genellina on
On 4 jun, 06:14, "Günther Dietrich" <gd.use...(a)spamfence.net> wrote:
> GabrielGenellina<gagsl-...(a)yahoo.com.ar> wrote:

> >Try the strptime method with a suitable format, like this (untested):
> >delta = now2-now1
> >delta.strftime('%H:%M:%S.%f')
>
> Throws an exception:
>
> |Traceback (most recent call last):
> |  File "<stdin>", line 1, in <module>
> |AttributeError: 'datetime.timedelta' object has no attribute 'strftime'
>
> What seems logical, since the documentation doesn't mention an strftime
> method for timedelta.

You're right. Second try (still untested):

def nice_timedelta_str(d):
result = str(d)
if result[1] == ':':
result = '0' + result
return result

delta = now2-now1
print nice_timedelta_str(delta)


--
Gabriel Genellina