pcp
[Top] [All Lists]

Re: [pcp] Help with Python API

To: Rohan Arora <rarora2012@xxxxxxxxx>, Ken McDonell <kenj@xxxxxxxxxxxxxxxx>, pcp@xxxxxxxxxxx
Subject: Re: [pcp] Help with Python API
From: David Smith <dsmith@xxxxxxxxxx>
Date: Wed, 8 Jul 2015 09:35:26 -0500
Delivered-to: pcp@xxxxxxxxxxx
In-reply-to: <CANbxzdVcJxXR+6CqYugvGnEa0uPPy0_3hWNqMr5JQmZY4tgRyQ@xxxxxxxxxxxxxx>
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>
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.0.1
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>