I have a program that uses this popular library, however I am struggling to use it to convert from seconds to minutes
The following code...
#include <iostream>
#include "units.h"
int main(int argc, const char * argv[])
{
{
long double one = 1.0;
units::time::second_t seconds;
units::time::minute_t minutes(one);
seconds = minutes;
std::cout << "1 minute is " << seconds << std::endl;
}
{
long double one = 1.0;
units::time::second_t seconds(one);
units::time::minute_t minutes;
minutes = seconds;
std::cout << "1 second is " << minutes << std::endl;
}
return 0;
}
produces...
1 minute is 60 s
1 second is 1 s
however, I would have expected it to produce...
1 minute is 60 s
1 second is .016666667 m
The library offers a units::convert method, check the doc here.
Here's a working snippet:
long double one = 1.0;
units::time::second_t seconds(one);
units::time::minute_t minutes;
minutes = seconds;
std::cout << "1 second is " << minutes << std::endl;
std::cout << "1 second is "
<< units::convert<units::time::seconds, units::time::minutes>(seconds)
<< std::endl;
For more, I suggest searching in the doc.
I don't know the library you are using, but C++11 added the std::chrono::duration class that seems to be able to do what you want:
#include <chrono>
#include <iostream>
int main()
{
{
std::chrono::minutes minutes(1);
std::chrono::seconds seconds;
seconds = minutes;
std::cout << "1 minute is " << seconds.count() << std::endl;
}
{
std::chrono::seconds seconds(1);
using fMinutes = std::chrono::duration<float, std::chrono::minutes::period>;
fMinutes minutes = seconds;
std::cout << "1 second is " << minutes.count() << std::endl;
}
return 0;
}
Note that the default std::chrono::minutes uses an integer counter, and thus reports that 1 second is 0 minutes. That is why I define my own float-minutes.
In any case, the above program produces the following output:
1 minute is 60
1 second is 0.0166667
Related
I need to get the current date and time in my emscripten code. I know there is emscripten_date_now(). But how would I get the day, the month, the year, and the time (hrs, minutes, and seconds) from that? (Or should I use the C/C++ time functions?).
Some operations that I need to perform is to be able to add a week. To change the month and year.
I am really stuck on this, so help will be greatly appreciated!
How do you get the current date and time in emscripten c++
Use std::chrono::system_clock::now() to get the current point in time.
how would I get the day, the month, the year, and the time (hrs, minutes, and seconds)
With the date library date library
#include <date/date.h>
#include <iostream>
int
main ()
{
using namespace date;
auto const currentTimePoint = std::chrono::system_clock::now ();
auto const currentDate = year_month_day{ floor<days> (currentTimePoint) };
std::cout << "year: " << currentDate.year () << std::endl;
std::cout << "month: " << currentDate.month () << std::endl;
std::cout << "day: " << currentDate.day () << std::endl;
auto const timeWithOutDate = make_time (currentTimePoint - date::floor<days> (currentTimePoint));
std::cout << "hour: " << timeWithOutDate.hours () << std::endl;
std::cout << "minute: " << timeWithOutDate.minutes () << std::endl;
std::cout << "second: " << timeWithOutDate.seconds () << std::endl;
}
Example output:
year: 2022
month: Jan
day: 24
hour: 11h
minute: 34min
second: 18s
Some operations that I need to perform is to be able to add a week
#include <date/date.h>
#include <iostream>
int
main ()
{
using namespace date;
auto now = floor<days> (std::chrono::system_clock::now ());
std::cout << "now in 1 week: " << now + weeks{ 1 } << std::endl;
}
Example output:
now in 1 week: 2022-01-31
I'm trying to learn how classes and their constructors work by creating a Time class that includes an hour, minute, and second. I wanted to print one time by using the default constructor and one through user input. While my program compiles, it does not ask for user input, most likely because of the way I call the class function getHour (if I were to only print the input hour). I am also unsure of how to print the time (0,0,0) through the default constructor.
Any help would be appreciated!
Main:
#include <iostream>
#include "Time.h"
int main(){
std::cout << "Enter the hour, minute, and second: " << std::endl;
int hour, minute, second;
std::cin >> hour >> minute >> second;
Time time1(hour, minute, second);
std::cout << time1.getHour() << std::endl;
return 0;
}
Class implementation:
#include <iostream>
#include "Time.h"
//default constructor
Time::Time() {
hour = 0;
minute = 0;
second = 0;
}
//construct from hour, minute, second
Time::Time(int theHour, int theMinute, int theSecond) {
hour = theHour;
minute = theMinute;
second = theSecond;
}
int Time::getHour() const {
return hour;
}
int Time::getMinute() const {
return minute;
}
int Time::getSecond() const {
return second;
}
Works fine for me, this was my output:
Enter the hour, minute, and second:
2
45
32
2
Press any key to continue . . .
Make sure you are rebuilding the code and running the new executable.
In the constructor you can just do:
Time::Time() {
hour = 0;
minute = 0;
second = 0;
std::cout << hour << " " << minute << " " << second << std::endl;
}
this will be called anytime you call Time with the default constructor:
std::cout << "Enter the hour, minute, and second: " << std::endl;
int hour, minute, second;
std::cin >> hour >> minute >> second;
Time t; //<--- this prints 0,0,0
Time time1(hour, minute, second);
std::cout << time1.getHour() << std::endl;
system("pause");
return 0;
will result in:
Enter the hour, minute, and second:
11
19
59
0 0 0
11
Press any key to continue . . .
I am also unsure of how to print the time (0,0,0) through the default constructor.
Unless I missing something subtle,
// Construct a Time object using the default constructor.
Time time2;
// Print it's hour
std::cout << time2.getHour() << std::endl;
The code you shared doesn't try to print an object constructed with the default constructor since getHour() is called by time1. The is doing what you tell it to do. If you want the full time from time1you'll have to call all of the getters eg: std::cout << time1.getHour() << " : " << time1.getMinute() << " : " << time1.getSecond() << std::endl
"I am also unsure of how to print the time (0,0,0) through the default constructor."
consider a displayTime() method that can be called on to display the time of any instance
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Enter the hour, minute, and second: " << std::endl;
int hour, minute, second;
std::cin >> hour >> minute >> second;
Time time1(hour, minute, second); // overloaded constructor
Time time2; // default constructor
// std::cout << time1.getHour() << std::endl;
// std::cout << time2.getHour() << std::endl;
time1.displayTime();
time2.displayTime();
getch();
return 0;
}
add displayTime() method
void Time::displayTime() const{
std::cout << this->hour << ","
<< this->minute << ","
<< this->second << std::endl;
}
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class TimeUnit
{
public:
TimeUnit(int m, int s)
{
this -> minutes = m;
this -> seconds = s;
}
string ToString()
{
ostringstream o;
o << minutes << " minutes and " << seconds << " seconds." << endl;
return o.str();
}
void Simplify()
{
if (seconds >= 60)
{
minutes += seconds / 60;
seconds %= 60;
}
}
TimeUnit Add(TimeUnit t2)
{
TimeUnit t3;
t3.seconds = seconds + t2.seconds;
if(t3.seconds >= 60)
{
t2.minutes += 1;
t3.seconds -= 60;
}
t3.minutes = minutes + t2.minutes;
return t3;
}
private:
int minutes;
int seconds;
};
int main(){
cout << "Hello World!" << endl;
TimeUnit t1(2,30);
cout << "Time1:" << t1.ToString() << endl;
TimeUnit t2(3,119);
cout << "Time2:" << t2.ToString();
t2.Simplify();
cout << " simplified: " << t2.ToString() << endl;
cout << "Added: " << t1.Add(t2).ToString() << endl;
//cout << " t1 + t2: " << (t1 + t2).ToString() << endl;
/*cout << "Postfix increment: " << (t2++).ToString() << endl;
cout << "After Postfix increment: " << t2.ToString() << endl;
++t2;
cout << "Prefix increment: " << t2.ToString() << endl;*/
}
I'm having problems with my Add method. Xcode is giving me this error: "No matching constructor for initialization of TimeUnit"
Could someone please tell me what I am doing wrong? I've literally tried everything that I know how to do, but I can't even get it to compile with this method.
Here are the instructions from my professor:
The TimeUnit class should be able to hold a time consisting of Minutes
and Seconds. It should have the following methods:
A constructor that takes a Minute and Second as parameters ToString()
- Should return the string equivilant of the time. "M minutes S seconds." Test1 Simplify() - This method should take the time and
simplify it. If the seconds is 60 seconds or over, it should reduce
the seconds down to below 60 and increase the minutes. For example, 2
Min 121 seconds should become 4 minutes 1 second. Test2 Add(t2) -
Should return a new time that is the simplified addition of the two
times Test3 operator + should do the same thing as Add Test4 pre and
postfix ++: should increase the time by 1 second and simplify Test5
In your TimeUnit::Add function, you tried to initialize t3 with default constructor. However, your TimeUnit doesn't have one:
TimeUnit Add(TimeUnit t2)
{
TimeUnit t3; ///<<<---- here
///.....
}
Try update TimeUnit::Add to this way:
TimeUnit Add(const TimeUnit& t2)
{
return TimeUnit(this->minutes+t2.minutes, this->seconds+t2.seconds);
}
The specific problem is because there is no TimeUnit::TimeUnit() defined, only TimeUnit(const int &m, const int &s).
I have an incoming data feed that gives me an int with the number of milliseconds since midnight.
I'd like to convert it into some sort of time object so I can display the time.
For example, 1000 = 00:00:01
Is there a simple way to do this? Do I need a time struct?
Thank you!
x = ms / 1000
seconds = x % 60
x /= 60
minutes = x % 60
x /= 60
hours = x % 24
Then you can cout the time that has been parsed for you. This is only for duration since midnight. It won't show you the date.
You could use a little helper struct for this, but this could be a little bit to much for your simple request:
#include <iostream>
#include <iomanip>
struct MidnightTime{
MidnightTime(unsigned int miliseconds) :
seconds((miliseconds/1000) % 60),
minutes((miliseconds/60000) % 60),
hours((miliseconds/3600000) % 24){}
unsigned int seconds, minutes, hours;
};
std::ostream& operator<<(std::ostream& out, const MidnightTime& t){
out << std::setfill('0') << std::setw(2) << t.hours << ":" <<
std::setfill('0') << std::setw(2) << t.minutes << ":" <<
std::setfill('0') << std::setw(2) << t.seconds;
return out;
}
int main(){
std::cout << MidnightTime(1000) << std::endl; // will result in 00:00:01
return 0;
}
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
time_t current = time(0);
cout << ctime(¤t) << endl;
return 0;
}
How can I get the future time, say 1 hour later, from the current time?
time(2) returns the number of seconds since 1970-01-01 00:00:00 +0000 (UTC). One hour later would be current + 3600.
time_t current = time(0);
time_t inOneHour = current + (60*60); // 60 minutes of 60 sec.
cout << "Now: " << ctime(¤t) << "\n"
<< "In 1 hour: " << ctime(&inOneHour)
<< "\n";