Sorry, I don't know how I missed those links when I read your response. The answer in the first link, and what you just sent gets the correct times as well as dates. ÂI tried reading through the Python datetime documentation, but the code that I came up with from reading the documentation is what I sent in the last e-mail. Thanks for the help!
Best, Rohan
On 07/08/2015 08:59 AM, Rohan Arora wrote:
> From what I can tell, the information in that tv_sec and tv_usec only
> contain the information for the month, day, and year. For the time I
> simply get midnight, or 12:00AM. Is that correct, or am I missing
> something? For example, when converting the timestamps for metrics
> gathered yesterday 07/07/2015, I get the following results for the
> timestamp:
>
>Â Â Â 07/07/2015 00:00:00 for tv_sec
>Â Â Â 01/07/1970 00:00:00 for tv_usec
>
> The code I used to extract that is:
>
>
>Â datetime.date.fromtimestamp(result.contents.timestamp.tv_usec).strftime("%m/%d/%Y
> %H:%M:%S")
The first link I posted explained this. You also could have read the
python documentation. You aren't calling fromtimestamp correctly.
fromtimestamp takes the number of seconds since the epoch. So, you'd do
something like:
from datetime import datetime
dt = datetime.fromtimestamp(result.contents.timestamp.tv_sec +
     Âresult.contents.timestamp.tv_usec // 1000000000)
print dt.strftime("%m/%d/%Y %H:%M:%S")
If that looks suspiciously like the first answer in the first link I
posted, you would be correct.
If you are still only getting the date value, then pcp must be rounding
or sampling. The guys more knowledgeable about pcp would need to chime
in then.
> Thanks,
> Rohan
>
> On Wed, Jul 8, 2015 at 9:43 AM Rohan Arora <rarora2012@xxxxxxxxx
> <mailto:rarora2012@xxxxxxxxx>> wrote:
>
>Â Â ÂWhoops, sorry, I didn't realize that it was just a generic timestamp
>Â Â Âlike that, I don't have much experience with time libraries
>Â Â Âhonestly. Thanks for the help!
>
>Â Â ÂBest,
>Â Â ÂRohan
>
>Â Â ÂOn Tue, Jul 7, 2015 at 5:52 PM David Smith <dsmith@xxxxxxxxxx
>Â Â Â<mailto:dsmith@xxxxxxxxxx>> wrote:
>
>Â Â Â Â ÂOn 07/07/2015 09:47 AM, Rohan Arora wrote:
>Â Â Â Â Â> I am able to get the tv_sec and tv_usec properties from the
>Â Â Â Â Âresult using
>Â Â Â Â Â> "result.contents.timestamp.tv_sec", but I don't know how to
>Â Â Â Â Âconvert that
>Â Â Â Â Â> back into an actual timestamp with format, say, "MM/dd/yyy
>Â Â Â Â ÂHH:mm:ss". Is
>Â Â Â Â Â> there a way to do this?
>
>Â Â Â Â ÂThat's a generic python question, not really pcp related.
>
>Â Â Â Â ÂBasically, use datetime.fromtimestamp() to convert the
>Â Â Â Â Âtv_sec/tv_usec
>Â Â Â Â Âvalues to a python datetime object, then use the strftime()
>Â Â Â Â Âfunction to
>Â Â Â Â Âformat it however you want.
>
>Â Â Â Â Â<https://stackoverflow.com/questions/15649942/python-convert-epoch-time-with-nanoseconds-to-human-readable>
>Â Â Â Â Â<https://stackoverflow.com/questions/10611328/format-nanoseconds-in-python>
--
David Smith
dsmith@xxxxxxxxxx
Red Hat
http://www.redhat.com
256.217.0141 (direct)
256.837.0057 (fax)
|