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


Groups > comp.lang.python > #51797

Re: outputting time in microseconds or milliseconds

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'column': 0.07; 'everybody,': 0.07; 'problem:': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'second.': 0.09; 'spec': 0.09; 'python': 0.11; '2.7': 0.14; '(actually': 0.16; '00:00:00': 0.16; 'matters,': 0.16; 'observations': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'hey': 0.18; 'trying': 0.19; 'version.': 0.19; 'working.': 0.19; '(in': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'this:': 0.26; 'second': 0.26; 'header:X-Complaints-To:1': 0.27; 'skip:p 30': 0.29; 'statement': 0.30; 'code': 0.31; 'subject:time': 0.33; 'third': 0.33; 'could': 0.34; "can't": 0.35; 'test': 0.35; 'but': 0.35; 'version': 0.36; 'really': 0.36; 'ubuntu': 0.36; 'done': 0.36; "didn't": 0.36; 'useful': 0.36; 'charset:us-ascii': 0.36; 'possible': 0.36; 'should': 0.36; 'half': 0.37; 'seconds': 0.37; 'two': 0.37; 'to:addr:python-list': 0.38; 'quote': 0.39; 'success.': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'how': 0.40; '2nd': 0.60; 'information,': 0.61; "you've": 0.63; 'email addr:gmail.com': 0.63; 'day.': 0.63; 'more': 0.64; 'great': 0.65; 'occur': 0.65; 'latest': 0.67; 'day': 0.76; 'milliseconds': 0.84; 'imagine': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re: outputting time in microseconds or milliseconds
Date Fri, 2 Aug 2013 12:08:33 +0000 (UTC)
References <b5df0633-c6fe-4489-adda-5b9f270a904d@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 174.32.174.32
User-Agent XPN/1.2.6 (Street Spirit ; Linux)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.117.1375445333.1251.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375445333 news.xs4all.nl 15927 [2001:888:2000:d::a6]:44254
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51797

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web