pcp
[Top] [All Lists]

Re: [pcp] Help with Python API

To: David Smith <dsmith@xxxxxxxxxx>, Ken McDonell <kenj@xxxxxxxxxxxxxxxx>, pcp@xxxxxxxxxxx
Subject: Re: [pcp] Help with Python API
From: Rohan Arora <rarora2012@xxxxxxxxx>
Date: Wed, 08 Jul 2015 14:53:11 +0000
Delivered-to: pcp@xxxxxxxxxxx
Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-type; bh=mp3nCx+DA34Eb9xWsqWJ/RLZ7CrOCAYvYeUepIPy/7g=; b=KR4MZbPVMMHQOzSsHt/+XJhJf6CyVr+9D6eGnuPlsOBuAfHdC5txc61SEQZeqFOuMV QmiHfUJx1EVuREHuDpmO6ld2FlVM9O44YJ26COpoL3UlRgIYNVVkscSCRYl5RO4Nl2rp uy7uqnYEr6y8B+afd450z4b8SlFgtJgqXVj10asiCyEl3x+QmR2T2KXq96QsKNZAEt5b PKaO0DSSUX43AiEltXhLSjdnGmDAoMJBxxB2S4XbJeMkgktA2CXekgIktGTr42wpN+WZ d18YRVMDOR1J8BS4kD80r+w8Jr3Z9WndRoTbzM5aQcxPoEgo1O5dxf5w3AyXc/V194Cl zIbQ==
In-reply-to: <559D352E.2050109@xxxxxxxxxx>
References: <CANbxzdXmsRFyF_J_7c93uBuKWJHZikoRs6xmSPQC91rALtZbNA@xxxxxxxxxxxxxx> <CANbxzdXcqg=sRYa0JwETB+RHChw42uAeZE-n+bABUjV-FD9TDg@xxxxxxxxxxxxxx> <587447889.27462536.1435530714712.JavaMail.zimbra@xxxxxxxxxx> <CANbxzdX_4yDiOBqU954wjfbKOFOXnB8SJ_uiRsKEBdUk-hxF1w@xxxxxxxxxxxxxx> <CANbxzdUHygK=0NuO7L3pV+FERXa-LRL59=sumY5TznT0xzonxg@xxxxxxxxxxxxxx> <337532723.28653925.1435618315696.JavaMail.zimbra@xxxxxxxxxx> <CANbxzdVqbstVAue-GKW5moDkkKJKeg5pvtRdg1eHNET_7dHxeQ@xxxxxxxxxxxxxx> <559466FE.8090107@xxxxxxxxxxxxxxxx> <CANbxzdXqUj3i-MR2xF_NrTkYV11MMLBCX0B-+Btt45JQa77NQA@xxxxxxxxxxxxxx> <CANbxzdUn0Q8BkJUm-KAuTGxQAhQTz0CmSLdXEHKLT8UVKuBx4A@xxxxxxxxxxxxxx> <559AF629.5090609@xxxxxxxxxxxxxxxx> <CANbxzdWWozajgdEWpzX-MbndB=4EeLM-8p3QZRytJHhw+rrakQ@xxxxxxxxxxxxxx> <CANbxzdUnXsG3dJrSWnsET3mxAbxuYGP9XpG9+isXtBqH_LwTeQ@xxxxxxxxxxxxxx> <559C4A31.2030106@xxxxxxxxxx> <CANbxzdUYTi9w9s59Y=7CZ-W7OEc-D9XTZgavf9wysLpCvg1dkw@xxxxxxxxxxxxxx> <CANbxzdVcJxXR+6CqYugvGnEa0uPPy0_3hWNqMr5JQmZY4tgRyQ@xxxxxxxxxxxxxx> <559D352E.2050109@xxxxxxxxxx>
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 Wed, Jul 8, 2015 at 10:35 AM David Smith <dsmith@xxxxxxxxxx> wrote:
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)
<Prev in Thread] Current Thread [Next in Thread>