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
Related
So I'm writing a program to count the execution time of a function using clock and I used iomanip to change the output to decimal with 9 zeros.
This is the code that I am using:
#include <time.h>
#include <iomanip>
using namespace std;
void linearFunction(int input)
{
for(int i = 0; i < input; i++)
{
}
}
void execution_time(int input)
{
clock_t start_time, end_time;
start_time = clock();
linearFunction(input);
end_time = clock();
double time_taken = double(end_time - start_time) / double(CLOCKS_PER_SEC);
cout << "Time taken by function for input = " << input << " is : " << fixed
<< time_taken << setprecision(9);
cout << " sec " << endl;
}
int main()
{
execution_time(10000);
execution_time(100000);
execution_time(1000000);
execution_time(10000000);
execution_time(100000000);
execution_time(1000000000);
return 0;
}
And the output shows:
Time taken by function for input = 10000 is : 0.000000 sec
Time taken by function for input = 100000 is : 0.001000000 sec
Time taken by function for input = 1000000 is : 0.002000000 sec
Time taken by function for input = 10000000 is : 0.038000000 sec
Time taken by function for input = 100000000 is : 0.316000000 sec
Time taken by function for input = 1000000000 is : 3.288000000 sec
As you can see, the first time I call the function, it doesn't follow the setprecision(9) that I wrote. Why is this and how can I solve this? Thanks you in advance.
Look at the following line properly:
cout << "Time taken by function for input = " << input << " is : " << fixed << time_taken << setprecision(9);
See? You are setting the precision after printing out time_taken. So for the first time, you don't see the result of setprecision(). But for the second time and onwards, as setprecision() has already been executed, you get the desired decimal places.
So to fix this issue, move setprecision() before time_taken as such:
cout << "Time taken by function for input = " << input << " is : " << fixed << setprecision(9) << time_taken;
..or you can also do something like this:
cout.precision(9);
cout << "Time taken by function for input = " << input << " is : " << fixed << time_taken;
Also, consider not using the following line in your code:
using namespace std;
..as it's considered as a bad practice. Instead use std:: every time like this:
std::cout.precision(9);
std::cout << "Time taken by function for input = " << input << " is : " << std::fixed << time_taken;
For more information on this, look up to why is "using namespace std" considered as a bad practice.
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
I am trying to find the average UTC time of when a function was called. So I do:
boost::posix_time::ptime current_time_before(boost::posix_time::microsec_clock::universal_time());
DoStuff();
boost::posix_time::ptime current_time_after(boost::posix_time::microsec_clock::universal_time());
How do I go about calculating the averages between these two times?
I tried:
double time_avg = (current_time_before+current_time_after)*0.5;
But I get an error on a linux system that seems to have a problem with "+" but not "-" .
Thank you for your help.
Just... write it naturally?
ptime midpoint(ptime const& a, ptime const& b) {
return a + (b-a)/2; // TODO check for special case `b==a`
}
Live demo:
Live On Coliru
#include <boost/date_time/posix_time/posix_time.hpp>
using boost::posix_time::ptime;
ptime midpoint(ptime const& a, ptime const& b) {
return a + (b-a)/2;
}
int main() {
ptime a = boost::posix_time::second_clock::local_time();
ptime b = a + boost::posix_time::hours(3);
std::cout << "Mid of " << a << " and " << b << " is " << midpoint(a,b) << "\n";
std::swap(a,b);
std::cout << "Mid of " << a << " and " << b << " is " << midpoint(a,b) << "\n";
}
Prints
Mid of 2016-Sep-15 11:17:10 and 2016-Sep-15 14:17:10 is 2016-Sep-15 12:47:10
Mid of 2016-Sep-15 14:17:10 and 2016-Sep-15 11:17:10 is 2016-Sep-15 12:47:10
#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).
Okay, I'm working on a project for school, and we need to have a linked list of a class within another class (a linked list of the class "task" inside a class called "objectives"), so for this i'm using the STL class . Now I almost have it set up, but in my display function, to display the contents of the tasks, I'm using an iterator. But I can't assign taskList.begin() to the iterator because it gives me an error.
The following is the code that I think is relevant.
objective.h
#ifndef OBJECTIVE_H
#define OBJECTIVE_H
#include <string>
#include <list>
#include "date.h"
#include "task.h"
using namespace std;
namespace team2
{
class objective
{
private:
string objective_name, objective_desc, resources[10];
int category, priority, res_used;
double time;
date start, end;
int status;
std::list<task> taskList;
public:
// CONSTRUCTORS
objective();
objective(string objN, string objD, int c, int p, date s, date e, double t, string res[], int resU, int stat, list<task>& tList);
...
// CONSTANT MEMBER FUNCTIONS
void display() const;
...
};
}
#endif
objective.cpp (This is where I get the error)
#include "objective.h"
#include "date.h"
#include <cstdlib>
#include <cassert>
#include <string>
#include <list>
#include "task.h"
using namespace std;
namespace team2
{
void objective::display() const // display() - Displays the complete contents of a single objective
{
int days, hours, minutes;
std::list<task>::iterator taskIterator;
days = floor(time/24.0); // Find the max number of days based off of the time (in hours)
hours = floor(time - days*24); // Find the max number of hours after deduction of days
minutes = floor((time - (days*24 + hours))*60); // Find the number or minutes after taking into account hours and days
cout << "\nObjective Name: " << objective_name << endl;
cout << "Objective Description: " << objective_desc << endl;
cout << "Category: Quad " << category << endl;
cout << "Priority: " << priority << endl;
cout << "Starting Date: " << start.getMonth() << "/" << start.getDay() << "/" << start.getYear() << endl;
cout << "Ending Date: " << end.getMonth() << "/" << end.getDay() << "/" << end.getYear() << endl;
cout << "Time Required: " << days << " Days " << hours << " Hours " << minutes << " Minutes " << endl;
cout << "Resources: " << endl;
if(res_used == 0)
cout << "\tNo Resources" << endl;
for(int i = 0; i < res_used; i++)
cout << "\t" << resources [i] << endl;
cout << "Current Status: ";
if(status == 1)
cout << "Completed" << endl;
else if(status == 0)
cout << "Incomplete" << endl;
cout << "Tasks: " << endl;
if(taskList.empty())
cout << "\tNo Resources" << endl;
for(taskIterator = taskList.begin(); taskIterator != taskList.end(); taskIterator++)
{
(*taskIterator).display();
cout << endl;
}
}
}
The tasks class is almost Identical to the objective class, with a few fields omitted. The error occurs in the for loop. for(taskIterator = taskList.begin();...) Anyone know the cause for the problem? I can also provide more code if necessary. Thank you in advance!
The method is const, taskList is a member, so you can't have a non-const iterator on it.
Making a member method const is a contract that that method will not change non-mutable class members nor call non-const member methods. By having a non-const iterator, you're breaking that contract.
Since display is const, you can use a const iterator:
std::list<task>::const_iterator taskIterator;