chrono | A natural language date parser in Javascript | Natural Language Processing library
kandi X-RAY | chrono Summary
kandi X-RAY | chrono Summary
A natural language date parser in Javascript.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of chrono
chrono Key Features
chrono Examples and Code Snippets
npm i chrono-node
import { parseDate } from 'chrono-node';
const date = parseDate('This Friday at 13:00');
Community Discussions
Trending Discussions on chrono
QUESTION
I have multiple data frames with the following format:
...ANSWER
Answered 2022-Mar-30 at 19:26One option involving dplyr
and purrr
could be:
QUESTION
Excerpted from the cppref:
Implementations in which
std::time_t
is a 32-bit signed integer (many historical implementations) fail in the year 2038.
However, the documentation doesn't say how to detect whether the current implementation is 2038-safe. So, my question is:
Is it guaranteed to be 2038-safe if sizeof(std::time_t) == sizeof(std::uint64_t)
in C++?
ANSWER
Answered 2022-Mar-25 at 17:04Practically speaking yes. In all modern implementations in major OSes time_t
is the number of seconds since POSIX epoch, so if time_t
is larger than int32_t
then it's immune to the y2038 problem
You can also check if __USE_TIME_BITS64
is defined in 32-bit Linux and if _USE_32BIT_TIME_T
is not defined in 32-bit Windows to know if it's 2038-safe
However regarding the C++ standard, things aren't as simple. time_t
in C++ is defined in which has the same content as
in C standard. And in C
time_t
isn't defined to have any format
3. The types declared are
size_t
(described in 7.19);
QUESTION
In the following example I start a worker thread for my application. Later I post some work to it. To prevent it from returning prematurely I have to ensure "work" is outstanding. I do this with a work_guard object. However I have found two other ways to "ensure" work. Which one should I use throughout my application? Is there any difference?
...ANSWER
Answered 2022-Feb-21 at 17:14My knowledge comes from e.g. WG22 P0443R12 "A Unified Executors Proposal for C++".
Some differences up front: a work-guard
- does not alter the executor, instead just calling
on_work_started()
andon_work_finished()
on it. [It is possible to have an executor on which both of these have no effect.] - can be
reset()
independent of its lifetime, or that of any executor instance. Decoupled lifetime is a feature.
On the other hand, using prefer/require to apply outstanding_work
sub-properties:
- modifies existing executors
- notably when copied, all copies will have the same properties. This could be dangerous for something as invasive as keeping an execution context/resources around.
However, not all properties are requirable in the first place. Doing some reconaissance using Ex
defined as:
QUESTION
A program like this
...ANSWER
Answered 2022-Feb-19 at 16:23Just to be clear, there is no namespace date
in C++20. So the code in the question should look like:
QUESTION
The following behavior was seen under g++ 11.2.1 . The std::condition_variable
wait_for
method returns immediately if the timeout variable is too large. In particular in the program below, if num_years==1
, then the program hangs waiting as expected (presumably for 1 year), but if the variable num_years==1000
then the program returns immediatly.
Why does this happen? Is this a bug in g++? And a related question, how do you make the cv.wait_for()
wait indefinitely, instead of guessing a large timeout value?
ANSWER
Answered 2022-Feb-10 at 16:08This is an overflow bug under the hood of condition_variable::wait_for
. Internally it is waiting using steady_clock
which counts nanoseconds
. This clock overflows at +/-292 years. So when 1000 years gets converted to nanoseconds
, it is overflowing.
This looks like a standards bug as opposed to an implementation bug: http://eel.is/c++draft/thread.condition#condvar-24
The implementation should check for overflows of this type and in case found, just wait for the maximum time it is capable of waiting for.
QUESTION
ANSWER
Answered 2022-Jan-24 at 10:03Using a void *
as an argument compiled successfully for me. It's ABI-compatible with the pybind11 interfaces (an MPI_Comm
is a pointer in any case). All I had to change was this:
QUESTION
When I build my rust project in macOS with apple sillicon using this command:
...ANSWER
Answered 2021-Dec-17 at 20:55I can resolve with:
QUESTION
How would I convert a human readable time (in any format, such as Tue, 1 Jul 2003 10:52:37 +0200
) into a SystemTime
, in a cross-platform way? I know about chrono::DateTime::from_rfc2822()
, but I've been searching for quite a while and I can't find a way to convert a DateTime
into a SystemTime
. This conversion also needs to be cross-platform, so I can't use the platform-specific epochs (such as UNIX_EPOCH
).
Does anyone have any advice or ideas on how to do this?
...ANSWER
Answered 2022-Jan-02 at 20:10There is a conversion available for DateTime
to SystemTime
, so you just need to call .into()
:
QUESTION
#include
#include
using namespace std;
class MyTimer {
private:
std::chrono::time_point starter;
std::chrono::time_point ender;
public:
void startCounter() {
starter = std::chrono::steady_clock::now();
}
double getCounter() {
ender = std::chrono::steady_clock::now();
return double(std::chrono::duration_cast(ender - starter).count()) /
1000000; // millisecond output
}
// timer need to have nanosecond precision
int64_t getCounterNs() {
return std::chrono::duration_cast(std::chrono::steady_clock::now() - starter).count();
}
};
MyTimer timer1, timer2, timerMain;
volatile int64_t dummy = 0, res1 = 0, res2 = 0;
// time run without any time measure
void func0() {
dummy++;
}
// we're trying to measure the cost of startCounter() and getCounterNs(), not "dummy++"
void func1() {
timer1.startCounter();
dummy++;
res1 += timer1.getCounterNs();
}
void func2() {
// start your counter here
dummy++;
// res2 += end your counter here
}
int main()
{
int i, ntest = 1000 * 1000 * 100;
int64_t runtime0, runtime1, runtime2;
timerMain.startCounter();
for (i=1; i<=ntest; i++) func0();
runtime0 = timerMain.getCounter();
cout << "Time0 = " << runtime0 << "ms\n";
timerMain.startCounter();
for (i=1; i<=ntest; i++) func1();
runtime1 = timerMain.getCounter();
cout << "Time1 = " << runtime1 << "ms\n";
timerMain.startCounter();
for (i=1; i<=ntest; i++) func2();
runtime2 = timerMain.getCounter();
cout << "Time2 = " << runtime2 << "ms\n";
return 0;
}
...ANSWER
Answered 2021-Dec-22 at 15:19What you want is called "micro-benchmarking". It can get very complex. I assume you are using Ubuntu Linux on x86_64. This is not valid form ARM, ARM64 or any other platforms.
std::chrono is implemented at libstdc++ (gcc) and libc++ (clang) on Linux as simply a thin wrapper around the GLIBC, the C library, which does all the heavy lifting. If you look at std::chrono::steady_clock::now() you will see calls to clock_gettime().
clock_gettime() is a VDSO, ie it is kernel code that runs in userspace. It should be very fast but it might be that from time to time it has to do some housekeeping and take a long time every n-th call. So I would not recommend for microbenchmarking.
Almost every platform has a cycle counter and x86 has the assembly instruction rdtsc
. This instruction can be inserted in your code by crafting asm
calls or by using the compiler-specific builtins __builtin_ia32_rdtsc() or __rdtsc().
These calls will return a 64-bit integer representing the number of clocks since the machine power up. rdtsc is not immediate but fast, it will take roughly 15-40 cycles to complete.
It is not guaranteed in all platforms that this counter will be the same for each core so beware when the process gets moved from core to core. In modern systems this should not be a problem though.
Another problem with rdtsc is that compilers will often reorder instructions if they find they don't have side effects and unfortunately rdtsc is one of them. So you have to use fake barriers around these counter reads if you see that the compiler is playing tricks on you - look at the generated assembly.
Also a big problem is cpu out of order execution itself. Not only the compiler can change the order of execution but the cpu can as well. Since the x86 486 the Intel CPUs are pipelined so several instructions can be executed at the same time - roughly speaking. So you might end up measuring spurious execution.
I recommend you to get familiar with the quantum-like problems of micro-benchmarking. It is not straightforward.
Notice that rdtsc() will return the number of cycles. You have to convert to nanoseconds using the timestamp counter frequency.
Here is one example:
QUESTION
#include
#include
#include
#include
using namespace std;
const int p[9] = {1, 10, 100,
1000, 10000, 100000,
1000000, 10000000, 100000000};
class MyTimer {
private:
std::chrono::time_point starter;
std::chrono::time_point ender;
public:
void startCounter() {
starter = std::chrono::steady_clock::now();
}
double getCounter() {
ender = std::chrono::steady_clock::now();
return double(std::chrono::duration_cast(ender - starter).count()) /
1000000; // millisecond output
}
};
int convert1(char *a) {
int res = 0;
for (int i=0; i<9; i++) res = res * 10 + a[i] - 48;
return res;
}
int convert2(char *a) {
return (a[0] - 48) * p[8] + (a[1] - 48) * p[7] + (a[2] - 48) * p[6]
+ (a[3] - 48) * p[5] + (a[4] - 48) * p[4] + (a[5] - 48) * p[3]
+ (a[6] - 48) * p[2] + (a[7] - 48) * p[1] + (a[8] - 48) * p[0];
}
int convert3(char *a) {
return (a[0] - 48) * p[8] + a[1] * p[7] + a[2] * p[6] + a[3] * p[5]
+ a[4] * p[4] + a[5] * p[3] + a[6] * p[2] + a[7] * p[1] + a[8]
- 533333328;
}
const unsigned pu[9] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
100000000};
int convert4u(char *aa) {
const unsigned char *a = (const unsigned char*) aa;
return a[0] * pu[8] + a[1] * pu[7] + a[2] * pu[6] + a[3] * pu[5] + a[4] * pu[4]
+ a[5] * pu[3] + a[6] * pu[2] + a[7] * pu[1] + a[8] - (unsigned) 5333333328u;
}
int convert5(char* a) {
int val = 0;
for(size_t k =0;k <9;++k) {
val = (val << 3) + (val << 1) + (a[k]-'0');
}
return val;
}
const unsigned pu2[9] = {100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1};
int convert6u(char *a) {
return a[0]*pu2[0] + a[1]*pu2[1] + a[2]*pu2[2] + a[3] * pu2[3] + a[4] * pu2[4] + a[5] * pu2[5] + a[6] * pu2[6] + a[7] * pu2[7] + a[8] - (unsigned) 5333333328u;
}
using ConvertFunc = int(char*);
volatile int result = 0; // do something with the result of function to prevent unexpected optimization
void benchmark(ConvertFunc converter, string name, int numTest=10000) {
MyTimer timer;
const int N = 100000;
char *a = new char[9*N + 1];
double runtime = 0;
for (int t=1; t<=numTest; t++) {
// change something to prevent unexpected optimization
for (int i=0; i<9*N; i++) a[i] = rand() % 10 + '0';
timer.startCounter();
for (int i=0; i<9*N; i+= 9) result = converter(a+i);
runtime += timer.getCounter();
}
cout << name << ": " << runtime << "ms\n";
}
int main() {
benchmark(convert1, "slow");
benchmark(convert2, "normal");
benchmark(convert3, "fast");
benchmark(convert4u, "unsigned");
benchmark(convert5, "shifting");
benchmark(convert6u, "reverse");
return 0;
}
...ANSWER
Answered 2021-Dec-20 at 12:59An alternative candidate
Use unsigned
math to avoid UB of int
overflow and allow for taking all the - 48
out and then into a constant.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install chrono
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page