Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51793 > unrolled thread

outputting time in microseconds or milliseconds

Started bymatt.doolittle33@gmail.com
First post2013-08-02 03:54 -0700
Last post2013-08-08 09:30 -0400
Articles 18 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  outputting time in microseconds or milliseconds matt.doolittle33@gmail.com - 2013-08-02 03:54 -0700
    Re: outputting time in microseconds or milliseconds Dave Angel <davea@davea.name> - 2013-08-02 12:08 +0000
    Re: outputting time in microseconds or milliseconds Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-08-02 13:59 +0200
      Re: outputting time in microseconds or milliseconds matt.doolittle33@gmail.com - 2013-08-02 06:17 -0700
        Re: outputting time in microseconds or milliseconds Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-08-05 08:15 +0200
    Re: outputting time in microseconds or milliseconds Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-02 12:35 +0000
      Re: outputting time in microseconds or milliseconds matt.doolittle33@gmail.com - 2013-08-02 08:09 -0700
    Re: outputting time in microseconds or milliseconds Skip Montanaro <skip@pobox.com> - 2013-08-02 07:37 -0500
      Re: outputting time in microseconds or milliseconds matt.doolittle33@gmail.com - 2013-08-02 07:18 -0700
        Re: outputting time in microseconds or milliseconds Skip Montanaro <skip@pobox.com> - 2013-08-02 10:15 -0500
    Re: outputting time in microseconds or milliseconds Chris Angelico <rosuav@gmail.com> - 2013-08-02 13:50 +0100
    Re: outputting time in microseconds or milliseconds matt.doolittle33@gmail.com - 2013-08-04 04:30 -0700
      Re: outputting time in microseconds or milliseconds Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2013-08-04 13:57 +0200
      Re: outputting time in microseconds or milliseconds Dave Angel <davea@davea.name> - 2013-08-04 13:38 +0000
      Re: outputting time in microseconds or milliseconds Roy Smith <roy@panix.com> - 2013-08-04 10:33 -0400
        Re: outputting time in microseconds or milliseconds matt.doolittle33@gmail.com - 2013-08-07 19:51 -0700
          Re: outputting time in microseconds or milliseconds Skip Montanaro <skip@pobox.com> - 2013-08-08 07:03 -0500
            Re: outputting time in microseconds or milliseconds Roy Smith <roy@panix.com> - 2013-08-08 09:30 -0400

#51793 — outputting time in microseconds or milliseconds

Frommatt.doolittle33@gmail.com
Date2013-08-02 03:54 -0700
Subjectoutputting time in microseconds or milliseconds
Message-ID<b5df0633-c6fe-4489-adda-5b9f270a904d@googlegroups.com>
Hey everybody,

I am using 2.7 on Ubuntu 12.10.  All I need to do is to print time with the microseconds.  I have been looking at the docs and trying things for about half a day now with no success.  Currently my code looks like this:

        #  write date and time and microseocnds
        self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", ))))
        self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", ))))
        self.logfile.write('%s\t'%(str(time())))

the output looks like this:

2013-08-02	06:01:43       00:00:00	
2013-08-02	06:01:43       00:00:00					
2013-08-02	06:01:43       00:00:00				
2013-08-02	06:01:43       00:00:00						
2013-08-02	06:01:43       00:00:00						
2013-08-02	06:01:43       00:00:00				
2013-08-02	06:01:43       00:00:00						
2013-08-02	06:01:43       00:00:00						
2013-08-02	06:01:43       00:00:00						
2013-08-02	06:01:44       00:00:00					
2013-08-02	06:01:44       00:00:00				

as you can see in the 2nd column the observations occur with 'fractional second' frequency, up to 8 or 9 per second.  You can also see my latest attempt to get the microseconds in the third column is not working.  It would really be great if python had a way to just tack the microseconds (actually i think milliseconds would be better) on to the second (in the 2nd column) as a fraction. If this is not possible I need the extra write statement for just the microseconds (millisecond if possible). 

Any help will be much appreciated.
Thanks!

[toc] | [next] | [standalone]


#51797

FromDave Angel <davea@davea.name>
Date2013-08-02 12:08 +0000
Message-ID<mailman.117.1375445333.1251.python-list@python.org>
In reply to#51793
matt.doolittle33@gmail.com wrote:

> Hey everybody,
>
> I am using 2.7 on Ubuntu 12.10.

and what version of Python are you using?  I don't know if it matters,
but it's useful to always supply both Python version and OS version.

>  All I need to do is to print time with the microseconds.  I have been
> looking at the docs and trying things for about half a day now with
> no success. Currently my code looks like this:
>
>         #  write date and time and microseocnds
>         self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", ))))
>         self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", ))))

You've already got a problem:  since this has two (implicit) calls to
localtime().  If this happens near enough to midnight, then your date
could be for one day, but the time for the following day.

You need to make a single call to get the time, and then format it into
one or more fields.  if you didn't need sub-second information, it could
have been done by just using a longer spec for strftime().


>         self.logfile.write('%s\t'%(str(time())))

When I test str(time()) I get an output like this:
 1375444314.484557
I can't imagine how you would get that third column from the line you
quote above.

>
> the output looks like this:
>
> 2013-08-02	06:01:43       00:00:00	
> 2013-08-02	06:01:43       00:00:00					
> 2013-08-02	06:01:43       00:00:00				
> 2013-08-02	06:01:43       00:00:00						
> 2013-08-02	06:01:43       00:00:00						
> 2013-08-02	06:01:43       00:00:00				
> 2013-08-02	06:01:43       00:00:00						
> 2013-08-02	06:01:43       00:00:00						
> 2013-08-02	06:01:43       00:00:00						
> 2013-08-02	06:01:44       00:00:00					
> 2013-08-02	06:01:44       00:00:00				
>
> as you can see in the 2nd column the observations occur with 'fractional second' frequency, up to 8 or 9 per second.  You can also see my latest attempt to get the microseconds in the third column is not working.  It would really be great if python had a way to just tack the microseconds (actually i think milliseconds would be better) on to the second (in the 2nd column) as a fraction. If this is not possible I need the extra write statement for just the microseconds (millisecond if possible). 
>


now = time()   #make a single call to get the time in seconds (float)
localnow = localtime(now)

print(strftime("%Y-%m-%d\t%H:%M:%S\t", llocalnow))
print(now-int(now))  #obviously you should format this to get the
desired layout

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#51799

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2013-08-02 13:59 +0200
Message-ID<2ijsca-ag8.ln1@satorlaser.homedns.org>
In reply to#51793
Am 02.08.2013 12:54, schrieb matt.doolittle33@gmail.com:
> I am using 2.7 on Ubuntu 12.10.  All I need to do is to print time with the microseconds.[...]
>
>          #  write date and time and microseocnds
>          self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", ))))
>          self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", ))))
>          self.logfile.write('%s\t'%(str(time())))
>
> the output looks like this:
>
> 2013-08-02	06:01:43       00:00:00	
> 2013-08-02	06:01:43       00:00:00					
> 2013-08-02	06:01:43       00:00:00				
> 2013-08-02	06:01:43       00:00:00						
> 2013-08-02	06:01:43       00:00:00						

The output here and the code are surely not the same, provided that 
"time()" is the same as Python's "time.time()". That function will give 
you the time as a floating-point number and with as much precision as is 
available. You then only need the rest when dividing by one to get the 
fractional seconds.

BTW:
1. You have a race condition, you are retrieving the time thrice, which 
can and should yield different results for each call. Call it once, then 
format the results in multiple steps if you want.
2. Python's strftime() gives you a string already, calling str() on it 
is redundant. Also, formatting a string in order to then format it into 
another string is unnecessary overhead. Further, '%s' will implicitly 
invoke str() on the argument. Belt and suspenders anyone? :)

Good luck!

Uli

[toc] | [prev] | [next] | [standalone]


#51807

Frommatt.doolittle33@gmail.com
Date2013-08-02 06:17 -0700
Message-ID<8f4958d4-d346-4561-88ba-9a49a67b8792@googlegroups.com>
In reply to#51799
so you are saying that 

   self.logfile.write('%s\t'%(str(time()))) 

should be:

   self.logfile.write('%s\t'%(str(time.time()))) 

???
Thanks

[toc] | [prev] | [next] | [standalone]


#51952

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2013-08-05 08:15 +0200
Message-ID<ggs3da-tts.ln1@satorlaser.homedns.org>
In reply to#51807
Am 02.08.2013 15:17, schrieb matt.doolittle33@gmail.com:
> so you are saying that
>
>     self.logfile.write('%s\t'%(str(time())))
>
> should be:
>
>     self.logfile.write('%s\t'%(str(time.time())))

No, I'm not saying that. What I wanted to make clear is that your code 
is impossible to understand as it stands, because nobody knows what 
"time" refers to. My guess was that you had "from time import time, 
strftime" somewhere in your code so that the "time" in your code refers 
to the "time" function from Python's "time" module.

Uli

[toc] | [prev] | [next] | [standalone]


#51800

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-08-02 12:35 +0000
Message-ID<51fba781$0$30000$c3e8da3$5496439d@news.astraweb.com>
In reply to#51793
On Fri, 02 Aug 2013 03:54:32 -0700, matt.doolittle33 wrote:

> Hey everybody,
> 
> I am using 2.7 on Ubuntu 12.10.  All I need to do is to print time with
> the microseconds.  I have been looking at the docs and trying things for
> about half a day now with no success.  Currently my code looks like
> this:
> 
>         #  write date and time and microseocnds
>         self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", ))))
>         self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", ))))
>         self.logfile.write('%s\t'%(str(time())))

What's this time() function? Where does it come from, and what does it 
do? By the look of it, it merely returns the string "00:00:00". The 
time.time() function returns a number of seconds:

py> "%s" % time.time()
'1375445812.873546'

so I'm not sure what function you are calling there.

Also, you don't need to call str() before using the %s template, since 
that automatically converts any object to a string. Besides, 
time.strftime already returns a string.

You can replace all of those above lines with a single format string:

py> import time
py> print(time.strftime("%Y-%m-%d\t%H:%M:%S"))
2013-08-02      22:31:14


If you google for "strftime millisecond" with the search engine of your 
choice:

https://duckduckgo.com/?q=strftime%20millisecond


you will find this bug report:

http://bugs.python.org/issue1982


Unfortunately, the %S.%f codes do not appear to work for me on Linux 
using Python 2.6, 2.7 or 3.3. Perhaps you will have better luck.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#51817

Frommatt.doolittle33@gmail.com
Date2013-08-02 08:09 -0700
Message-ID<0a347771-a725-4be4-ac68-6afff7ecd2a5@googlegroups.com>
In reply to#51800
On Friday, August 2, 2013 8:35:13 AM UTC-4, Steven D'Aprano wrote:
> On Fri, 02 Aug 2013 03:54:32 -0700, matt.doolittle33 wrote:
> 
> 
> 
> > Hey everybody,
> 
> > 
> 
> > I am using 2.7 on Ubuntu 12.10.  All I need to do is to print time with
> 
> > the microseconds.  I have been looking at the docs and trying things for
> 
> > about half a day now with no success.  Currently my code looks like
> 
> > this:
> 
> > 
> 
> >         #  write date and time and microseocnds
> 
> >         self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", ))))
> 
> >         self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", ))))
> 
> >         self.logfile.write('%s\t'%(str(time()))) 
 
> 
> What's this time() function? Where does it come from, and what does it 
> 
> do? By the look of it, it merely returns the string "00:00:00". The 
> 
> time.time() function returns a number of seconds:
> 
> py> "%s" % time.time()
> 
> '1375445812.873546'
> 
> 
right this is the number that i need in the third column. so should i try?: 

> >         self.logfile.write('%s\t'%(str(time.time()))) 

[toc] | [prev] | [next] | [standalone]


#51801

FromSkip Montanaro <skip@pobox.com>
Date2013-08-02 07:37 -0500
Message-ID<mailman.119.1375447068.1251.python-list@python.org>
In reply to#51793
Perhaps use datetime?

>>> now = datetime.datetime.now()
>>> now.isoformat()
'2013-08-02T07:37:08.430131'
>>> now.strftime("%f")
'430131'

Skip

[toc] | [prev] | [next] | [standalone]


#51815

Frommatt.doolittle33@gmail.com
Date2013-08-02 07:18 -0700
Message-ID<3e3f0032-3b3b-4c50-99b1-8db3676fb993@googlegroups.com>
In reply to#51801
On Friday, August 2, 2013 8:37:45 AM UTC-4, Skip Montanaro wrote:
> Perhaps use datetime?
> 
> 
> 
> >>> now = datetime.datetime.now()
> 
> >>> now.isoformat()
> 
> '2013-08-02T07:37:08.430131'
> 
> >>> now.strftime("%f")
> 
> '430131'
> 
> 
> 
> Skip

Thanks Skip, what i currently i have is: 

   dt = datetime.now()

and 
   self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", ))))
   self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", )))) 
   self.logfile.write('%s\t'%(str(dt.microsecond))) 

what i get is this:

2013-08-02	09:52:20	312961
2013-08-02	09:52:20	313274
2013-08-02	09:52:20	313461
2013-08-02	09:52:20	313580
2013-08-02	09:52:20	498705
2013-08-02	09:52:20	508610
2013-08-02	09:52:20	508963
2013-08-02	09:52:20	509191
2013-08-02	09:52:20	509477
2013-08-02	09:52:20	509703
2013-08-02	09:52:20	509798
2013-08-02	09:52:20	509887
2013-08-02	09:52:20	509975
2013-08-02	09:52:20	511013
2013-08-02	09:52:20	511112
2013-08-02	09:52:20	678554
2013-08-02	09:52:20	687994
2013-08-02	09:52:20	688291
2013-08-02	09:52:20	688519
2013-08-02	09:52:20	688740
2013-08-02	09:52:20	688963

is the third column is only the microsecond?  how could i get this to write with the rest of the time (the hh:mm:ss) ?

[toc] | [prev] | [next] | [standalone]


#51818

FromSkip Montanaro <skip@pobox.com>
Date2013-08-02 10:15 -0500
Message-ID<mailman.128.1375456557.1251.python-list@python.org>
In reply to#51815
> is the third column is only the microsecond?

Yes.

> how could i get this to write with the rest of the time (the hh:mm:ss) ?

It sounds like you really want the time formatted in a particular way,
not just the numeric value of one or more fields.  Look at the
documentation for the strftime method of datetime objects for full
details:

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

Here's a simple example:

>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime("%H:%M:%S.%f")
'10:14:36.526110'

Skip

[toc] | [prev] | [next] | [standalone]


#51802

FromChris Angelico <rosuav@gmail.com>
Date2013-08-02 13:50 +0100
Message-ID<mailman.120.1375447850.1251.python-list@python.org>
In reply to#51793
On Fri, Aug 2, 2013 at 1:08 PM, Dave Angel <davea@davea.name> wrote:
> matt.doolittle33@gmail.com wrote:
>
>> Hey everybody,
>>
>> I am using 2.7 on Ubuntu 12.10.
>
> and what version of Python are you using?  I don't know if it matters,
> but it's useful to always supply both Python version and OS version.
>

Looks to me like Python 2.7.

ChrisA

[toc] | [prev] | [next] | [standalone]


#51916

Frommatt.doolittle33@gmail.com
Date2013-08-04 04:30 -0700
Message-ID<07f6b1c7-069d-458b-a9fe-ff30c09f2f2d@googlegroups.com>
In reply to#51793
ok so now i import the module like this:

   from time import strftime, time

i made the write statement like this:

   self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", ))))
   self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", ))))
   self.logfile.write('%s\t'%(str(time())))

(oh and btw,i know the code is hacker ugly).  
and the output is this:

2013-08-03	23:59:34	1375588774.89
2013-08-03	23:59:35	1375588775.06
2013-08-03	23:59:35	1375588775.25
2013-08-03	23:59:35	1375588775.43
2013-08-03	23:59:35	1375588775.80
2013-08-03	23:59:35	1375588775.99
2013-08-03	23:59:35	1375588775.99
2013-08-03	23:59:35	1375588775.99
2013-08-03	23:59:35	1375588776.00
2013-08-03	23:59:36	1375588776.15
2013-08-03	23:59:36	1375588776.15
2013-08-03	23:59:36	1375588776.16
2013-08-03	23:59:36	1375588776.16
2013-08-03	23:59:36	1375588776.16
2013-08-03	23:59:36	1375588776.16
2013-08-03	23:59:36	1375588776.16
2013-08-03	23:59:36	1375588776.16
2013-08-03	23:59:36	1375588776.16
2013-08-03	23:59:36	1375588776.34
2013-08-03	23:59:36	1375588776.35
2013-08-03	23:59:36	1375588776.35
2013-08-03	23:59:36	1375588776.35

the first two columns are for eyes so if they are a microsecond apart it doesn't matter. the numbers in the third column are for calculating duration which is where i need the precision. 

Why is it only giving me the centisecond precision?  the docs say i should get microsecond precision with the code i put together.

Thanks!

[toc] | [prev] | [next] | [standalone]


#51917

FromAlain Ketterlin <alain@dpt-info.u-strasbg.fr>
Date2013-08-04 13:57 +0200
Message-ID<87eha91xez.fsf@dpt-info.u-strasbg.fr>
In reply to#51916
matt.doolittle33@gmail.com writes:

>    self.logfile.write('%s\t'%(str(time())))
[...]
> 2013-08-03	23:59:34	1375588774.89
[...]
> Why is it only giving me the centisecond precision? the docs say i
> should get microsecond precision with the code i put together.

Because of str()'s default format. Use "%f" % (time()) (and maybe vary
the precision).

-- Alain.

P/S: using str() + string formatting is kind of overkill: either use
string format directives like %f, or use str() with simple string
concatenation -- the latter giving less control.

[toc] | [prev] | [next] | [standalone]


#51919

FromDave Angel <davea@davea.name>
Date2013-08-04 13:38 +0000
Message-ID<mailman.179.1375623543.1251.python-list@python.org>
In reply to#51916
matt.doolittle33@gmail.com wrote:

> ok so now i import the module like this:
>
>    from time import strftime, time
>
> i made the write statement like this:
>
>    self.logfile.write('%s\t'%(str(strftime("%Y-%m-%d", ))))
>    self.logfile.write('%s\t'%(str(strftime("%H:%M:%S", ))))
>    self.logfile.write('%s\t'%(str(time())))
>
> (oh and btw,i know the code is hacker ugly).  
> and the output is this:
>
> 2013-08-03	23:59:34	1375588774.89
> 2013-08-03	23:59:35	1375588775.06
> 2013-08-03	23:59:35	1375588775.25
> 2013-08-03	23:59:35	1375588775.43
> 2013-08-03	23:59:35	1375588775.80
> 2013-08-03	23:59:35	1375588775.99
> 2013-08-03	23:59:35	1375588775.99
> 2013-08-03	23:59:35	1375588775.99
> 2013-08-03	23:59:35	1375588776.00
> 2013-08-03	23:59:36	1375588776.15
> 2013-08-03	23:59:36	1375588776.15
> 2013-08-03	23:59:36	1375588776.16
> 2013-08-03	23:59:36	1375588776.16
> 2013-08-03	23:59:36	1375588776.16
> 2013-08-03	23:59:36	1375588776.16
> 2013-08-03	23:59:36	1375588776.16
> 2013-08-03	23:59:36	1375588776.16
> 2013-08-03	23:59:36	1375588776.16
> 2013-08-03	23:59:36	1375588776.34
> 2013-08-03	23:59:36	1375588776.35
> 2013-08-03	23:59:36	1375588776.35
> 2013-08-03	23:59:36	1375588776.35
>
> the first two columns are for eyes so if they are a microsecond apart it doesn't matter.

But if the time is close enough to midnight, they'll be a *day* apart,
as I (and others) said before. It's also hard to imagine why you
resist the good advice you've been getting about formatting. You can
do both the date and time with one call to strftime().

> the numbers in the third column are for calculating duration which is
> where i need the precision. 
>
> Why is it only giving me the centisecond precision?  the docs say i should get microsecond precision with the code i put together.
>

Have you done any experimenting to decide which logic was losing those
extra digits?  Sit in the debugger, and do a  t = time.time()

Then investigate what t looks like, 
by repr(t), str(t), and "something" %t

(for various values of "something")
....

from time import time, localtime, strftime

now = time()   #make a single call to get the time in seconds (float)
localnow = localtime(now)

out12 = strftime("%Y-%m-%d\t%H:%M:%S\t", localnow)
out3 = "%.6f" % now
self.logfile.write(out12 + out3)

You still have the problem that out12 is in the local time zone, while
out3 is in DCT.  But as you say, these are for different audiences, and
presumably you won't really be putting them in the same file.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#51924

FromRoy Smith <roy@panix.com>
Date2013-08-04 10:33 -0400
Message-ID<roy-7D52A5.10334804082013@news.panix.com>
In reply to#51916
In article <07f6b1c7-069d-458b-a9fe-ff30c09f2f2d@googlegroups.com>,
 matt.doolittle33@gmail.com wrote:

> self.logfile.write('%s\t'%(str(time())))
> [...]
> 1375588774.89
> [...]
> Why is it only giving me the centisecond precision?  the docs say i should 
> get microsecond precision

When citing documentation, it's a good idea to provide a link to the 
docs page, and/or a direct quote of what you read.

I'm looking at http://docs.python.org/2/library/time.html#time.time, 
which says, "not all systems provide time with a better precision than 1 
second".  So, I don't know where you got the impression that you're 
guaranteed microsecond precision.

Earlier in the thread, you did mention that you're on Ubuntu, and there 
you do indeed get pretty good precision.  I'm not 100% sure if it's good 
to the microsecond (it appears to be), but it's certainly better than 
centisecond.

Anyway, your problem appears to be that str(float) gives you two digits 
after the decimal (at least for values in the range we're talking about 
here), but repr() will give you more:

>>> t = time.time()
>>> str(t)
'1375626035.26'
>>> repr(t)
'1375626035.260934'

I don't know anywhere that those behaviors are guaranteed, however.  If 
you want to make sure you print a float with 6 digits after the decimal, 
you should use %f, not %s:

>>> '%.6f' % t
'1375626035.260934'

Of course, if the underlying system call that time.time() invokes 
returns less precision than microseconds, some of those 6 digits may 
always be zero.  Or worse, garbage.

Taking a step back, you're probably better off using datetimes.  You'll 
get all this conversion nonsense for free:

>>> print datetime.datetime.utcnow()
2013-08-04 14:33:09.255096

When I write an operating system, I'm going to have it keep time in 
units of YiTp (Yobi Plank times).

[toc] | [prev] | [next] | [standalone]


#52161

Frommatt.doolittle33@gmail.com
Date2013-08-07 19:51 -0700
Message-ID<32a94a22-107c-4575-8808-d9355d635e83@googlegroups.com>
In reply to#51924
> 
> Taking a step back, you're probably better off using datetimes.  You'll 
> 
> get all this conversion nonsense for free:
> 
i did: 

   from time import strftime, time
   from datetime import datetime

   now = datetime.now()

   self.logfile.write('%s\t'%(strftime("%Y-%m-%d",)))
   self.logfile.write('%s\t'%(now.strftime("%H:%M:%S.%f",)))

this gives me:

2013-08-04	14:01:27.906126
2013-08-04	14:01:28.052273
2013-08-04	14:01:28.058967
2013-08-04	14:01:28.243959
2013-08-04	14:01:28.251107
2013-08-04	14:01:28.251268
2013-08-04	14:01:28.251373
2013-08-04	14:01:28.251475
2013-08-04	14:01:28.424568
2013-08-04	14:01:28.612548
2013-08-04	14:01:28.616569
2013-08-04	14:01:28.616727
2013-08-04	14:01:28.792487
2013-08-04	14:01:28.796226

thats what i need. Thanks!

[toc] | [prev] | [next] | [standalone]


#52193

FromSkip Montanaro <skip@pobox.com>
Date2013-08-08 07:03 -0500
Message-ID<mailman.349.1375963829.1251.python-list@python.org>
In reply to#52161
> i did:
>
>    from time import strftime, time
>    from datetime import datetime
>
>    now = datetime.now()
>
>    self.logfile.write('%s\t'%(strftime("%Y-%m-%d",)))
>    self.logfile.write('%s\t'%(now.strftime("%H:%M:%S.%f",)))

Note that you don't need the time module here.  Datetime objects have
what you need all by themselves:

from datetime import datetime
now = datetime.now()
self.logfile.write('%s\t%s\n' % (now.strftime("%Y-%m-%d"),
                                 now.strftime("%H:%M:%S.%f")))

The time module was historically the way Python did time, but it
wasn't at all object-oriented and provided no direct support for date
arithmetic.  When Tim Peters wrote datetime, the world became a better
place.  Cozy up to the datetime documentation and put the time module
aside.

Skip

Skip

[toc] | [prev] | [next] | [standalone]


#52195

FromRoy Smith <roy@panix.com>
Date2013-08-08 09:30 -0400
Message-ID<roy-95457D.09302908082013@news.panix.com>
In reply to#52193
In article <mailman.349.1375963829.1251.python-list@python.org>,
 Skip Montanaro <skip@pobox.com> wrote:

> When Tim Peters wrote datetime, the world became a better
> place.  

Lots of languages and databases have things called datetimes.  It's easy 
to be lulled into thinking that they're all the same, but they're not.  
They all have slightly different ranges and precisions.

Just something to be aware of.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web