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


Groups > comp.lang.python > #67769

Re: Proper conversion of timestamp

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'applicable,': 0.07; 'error:': 0.07; 'string': 0.09; 'formatting': 0.09; 'happen,': 0.09; 'lawrence': 0.09; 'method,': 0.09; 'python': 0.11; 'question.': 0.14; 'digits.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'igor': 0.16; 'integer.': 0.16; 'mark,': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'nearest': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'seconds,': 0.16; 'timestamp': 0.16; 'truncate': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; 'properly': 0.19; 'value.': 0.19; '(the': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'format,': 0.24; 'looks': 0.24; 'question': 0.24; 'this:': 0.26; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'record': 0.27; 'point': 0.28; 'function': 0.29; 'leave': 0.29; 'related': 0.29; "i'm": 0.30; 'gives': 0.31; 'code': 0.31; 'getting': 0.31; 'decimal': 0.31; 'object.': 0.31; 'produces': 0.31; 'could': 0.34; 'convert': 0.35; 'received:84': 0.35; 'but': 0.35; 'google': 0.35; 'there': 0.35; 'dates': 0.36; 'described': 0.36; "i'll": 0.36; 'possible': 0.36; 'hi,': 0.36; 'should': 0.36; 'too': 0.37; 'expected': 0.38; 'thank': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'does': 0.39; 'environment.': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'remove': 0.60; 'read': 0.60; 'easy': 0.60; 'range': 0.61; 'first': 0.61; 'you.': 0.62; 'provide': 0.64; 'different': 0.65; 'mar': 0.68; 'cut': 0.74; 'milliseconds': 0.84; 'stamp': 0.91
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=JLW1sq6b c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=sASEtNAQL0YA:10 a=1K8QQjYBxE4A:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=BrDiTsk0AAAA:8 a=uPZiAMpXAAAA:8 a=xEn4Lq4iCbdhW0u3pMoA:9 a=sqDRCHGnrOgECMCF:21 a=hIx362mEvWhaUS9-:21 a=QEXdDO2ut3YA:10 a=-hJg1tCh9CgA:10
X-AUTH mrabarnett:2500
Date Wed, 05 Mar 2014 00:42:51 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Proper conversion of timestamp
References <CA+FnnTy2zoJYzx+ExkHf8SsENppBLaQzp2zOC9WFYsLBYwK9Qg@mail.gmail.com> <lf5hes$ve3$1@ger.gmane.org> <CA+FnnTxuiNTeaTu7LEdPWvsSnbbirgXqaCS67p5HOrruQPcZ9A@mail.gmail.com>
In-Reply-To <CA+FnnTxuiNTeaTu7LEdPWvsSnbbirgXqaCS67p5HOrruQPcZ9A@mail.gmail.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.7778.1393980173.18130.python-list@python.org> (permalink)
Lines 79
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1393980173 news.xs4all.nl 2859 [2001:888:2000:d::a6]:56582
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:67769

Show key headers only | View raw


On 2014-03-04 21:55, Igor Korot wrote:
> Hi, Mark,
>
>
> On Tue, Mar 4, 2014 at 1:44 PM, Mark Lawrence <breamoreboy@yahoo.co.uk
> <mailto:breamoreboy@yahoo.co.uk>> wrote:
>
>     On 04/03/2014 20:57, Igor Korot wrote:
>
>         Hi, ALL,
>         I'm getting this:
>
>         timestamp out of range for platform localtime()/gmtime() function
>
>         trying to convert the timestamp with milliseconds into the
>         datetime object.
>
>         The first hit of Google gives me this:
>
>         http://stackoverflow.com/__questions/12458595/convert-__epoch-timestamp-in-python
>         <http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python>
>
>         but the solution described is not good for me since it does not
>         gives
>         me the milliseconds value.
>
>         How do I get the proper datetime value including milliseconds
>         from the
>         timestamp?
>
>         Thank you.
>
>
>     You have a long record of asking timestamp related questions so you
>     should know where the docs are that provide the answer to this
>     question.  I'll leave you to go off and read them.  If you don't
>     understand them, please cut and paste your code here, state what you
>     expected to happen, what actually happened, including any traceback
>     if applicable, and then we'll be happy to point you the error of
>     your ways.
>
>
> Working with the dates is not that easy and not just in Python.
> There are too many different formatting involved with many different
> representation.
> And on top of it it is possible to use one system in completely
> different environment.
>
> But this particular question is easy.
>
> What I have is a timestamp which reads: 1289410678L.
>
That's an integer. It looks like the timestamp is a whole number of
seconds, so the number of milliseconds is 0. (I make it '2010-11-10
17:37:58'.)

> Trying to convert this into the datetime object in Python using:
>
> import datetime
> datetime.datetime.fromtimestamp( stamp )
>
> produces the error: timestamp out of range for platform
> localtime()/gmtime() function.
>
> This is because this timestamp is not in seconds, but rather in
> milliseconds.
>
> Now the question I have is: how do I properly convert this timestamp
> into the datetime object with the milliseconds?
>
Using the datetime's .strftime method, you can include the number of
microseconds in the format with '%f' (it'll write the microseconds as 6
digits).

If you want it to the nearest millisecond (the timestamp would be a
float), you could round the timestamp to 3 decimal places, use the '%f'
in the format, and then truncate the string result to remove the last 3
digits.

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


Thread

Re: Proper conversion of timestamp MRAB <python@mrabarnett.plus.com> - 2014-03-05 00:42 +0000

csiph-web