LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2016, 04:34 AM   #1
kikilinux
Member
 
Registered: Sep 2012
Posts: 125

Rep: Reputation: Disabled
how to divide timeval value by a number


I need to divide a timeval value by a number and save the results into tv_sec and tv_usec.
I don’t how to do this in c++.

any suggestions would be greatly appreciated.
 
Old 05-01-2016, 02:09 PM   #2
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 525

Rep: Reputation: 95
Hi,

It will be great if you share with us what you have tried so far (and how it failed).
 
Old 05-01-2016, 02:17 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
If it were me, I would divide by 1000000, because 'usec' means 10^(-6) sec
 
Old 05-01-2016, 09:35 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,676
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Consider pages such as this: http://www.tutorialspoint.com/cplusp..._date_time.htm.

"Time manipulation," of any sort, is often handled through standard libraries and/or objects. The math is easy to get wrong, and you know that somebody else(!) already got it right . . .
 
Old 05-03-2016, 11:22 PM   #5
kikilinux
Member
 
Registered: Sep 2012
Posts: 125

Original Poster
Rep: Reputation: Disabled
I used this solution to multiply time which I get from pcap file packets timestamp:
Code:
if(byte_rate < 1)       // multiply with a value smaller than one
    {
        new_tv.tv_sec = new_tv.tv_sec * (1/byte_rate) + (new_tv.tv_usec * (1/byte_rate)) / ONE_MILION;
        new_tv.tv_usec = (new_tv.tv_usec * (rate / new_rate)) % ONE_MILION;
    }
    else                    // multiply with a value bigger than one
    {

        double m, fracpart, intpart;
        m = (long double)new_tv.tv_sec / byte_rate;
        // separate fractional part from integer part
        fracpart = modf (m , &intpart);
        // Add results of fractional part of SECOND to MICRO SECOND with MICRO SECOND division
        new_tv.tv_usec = (fracpart * ONE_MILION) + (new_tv.tv_usec / (byte_rate));
        // Set integer part of division of SECOND part by byterate to packet tv_sec
        new_tv.tv_sec = intpart;
    }
here byterate is a value bigger or smaller than one. the if portion multiplies timestamp to a value bigger than one and the else portion multiply timestamp to a value smaller than one.
 
Old 05-04-2016, 02:49 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by kikilinux View Post
I need to divide a timeval value by a number and save the results into tv_sec and tv_usec.
I don’t how to do this in c++.

any suggestions would be greatly appreciated.
What language do you know how to do this in? C? Same way in C++ as in C.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
'timeval' undeclared (first use in this function) totesmagotes Programming 3 01-25-2013 03:10 PM
[SOLVED] struct timeval doesn't keep after function returns legendbb Programming 3 06-02-2010 08:59 PM
Divide a big file into small pieces, each has same number of lines Kunsheng Linux - Software 1 10-16-2009 04:08 PM
Convert timeval to timespec ranthal Linux - Kernel 3 07-06-2009 06:31 PM
awk: Using split to divide string to array. How do I find out the number of elements? vxc69 Programming 9 02-09-2008 12:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration