Is there a way to get a Unix timestamp in Stata? - stata

I searched online and found several sources that talk about converting Unix timestamps to various workable formats, but none that allow me to actually get such a timestamp from within Stata. As of now, I use variations on
local curr_date = c(current_date)
local curr_time = c(current_time)
to apply timestamps to logs, data sets, etc. but I'd like to just use the Unix timestamp in seconds, if possible.

Are you familiar with help datetime? My understanding is that the Unix time stamp would be something like
display %12.0g clock("`c(current_date)' `c(current_time)'", "DMY hms" )/1000 - clock("1 Jan 1970", "DMY" )/1000
which of course you can use in other circumstances as well. (I am not a C programmer, I am a Stata user, but I do understand that it is easier for most people on this site to write a snippet of C code that would go into the guts of Stata than to RTFM... which is admirable in its own ways from where I sit, of course.)

One way to achieve this is to write a simple plugin. Compile this code, in a file called unixtimestamp.c
#include "stplugin.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
STDLL stata_call(int argc, char *argv[])
{
time_t seconds;
seconds = time(NULL);
char buf[33];
sprintf(buf, "%d", seconds);
SF_display(buf);
return(0);
}
with stplugin.h and stplugin.c in the same directory using this command (for a Linux system):
gcc -O3 -shared -DSYSTEM=OPUNIX -fPIC stplugin.c unix_timestamp.c -o unixtimestamp.plugin
The guide to creating plugins uses this command:
gcc -shared -DSYSTEM=OPUNIX stplugin.c unixtimestamp.c -o unixtimestamp.plugin
but on some systems, this gives an error instructing you to use the -fPIC flag, which is why I include it in my command. Also, optimizations aren't really necessary for such a simple plugin, but I included them regardless.
unixtimestamp.plugin should be placed in the ado/personal/ directory. Run Stata's sysdir function to find its location. On my system, it's HOME/ado/personal/ so I copied the plugin there. Then, from Stata, load the plugin:
program unixtimestamp, plugin
If no error message is displayed, run the plugin with:
plugin call unixtimestamp
As with any Stata command, you can also use a macro to simplify this if you plan to use this command frequently:
local unixtime plugin call unixtimestamp
`unixtime'

I use the following to grab a date/time stamp:
di "DateTime: $S_DATE $S_TIME"
Updated:
You may use Unix shell commands directly inside Stata by prefixing them an exclamation mark. To echo Unix time try:
!date +%s
1344341160

Related

How to use time library in Arduino?

I am trying to upload this simple program to my Arduino that gets to clock time:
#include <Time.h>
time_t nowTime;
void setup() {
nowTime = now();
}
However, its failing to compile:
exit status 1
'now' was not declared in this scope
Why is now() not declared in this scope? The Time.h file was included. So why wouldn't now() be declared? How can I get around this?
The compilation fails because the Time.h file that the compiler finds has nothing to do with time libraries such as that by Paul Stoffregen (https://github.com/PaulStoffregen/Time).
I tried your Sketch, compiled for an Arduino Uno, and saw the same error you see: that Time.h resolves (the file exists somewhere), yet now() is not defined by that Time.h
After searching my Windows PC for a while, I finally found what I think is the file that #include includes on my installation: C:\Users\Brad\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\firmwares\wifishield\wifiHD\src\time.h or perhaps C:\Users\Brad\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5\avr\include\time.h
Neither of those files defines the now() function.
If you want to use Paul Stoffregen's Time library, download and install it from https://github.com/PaulStoffregen/Time. If instead you wish to use Michael Margolis' Time library, you can find and install it in the Arduino IDE, under Tools / Manage Libraries... and entering "Time" (without quotes) in the search term.
As others have pointed out, the Arduino environment doesn't always know the current date and time. The functions mills() and micros(), return the number of milliseconds or microseconds, respectively, since the Arduino booted. For just looking at the passage of time, most people use millis() or micros() instead of a more complex library.

Include file read from terminal into function using #include

I have written a code which reads in mathematical functions from a text file which are in a C++ compatible format and applies mathematical operations to them. This is a brief summary of the code. I am aware that #include is a preprocessor directive so once the contents of the file have been read in it compiles the code via a bash script each time.
double myfunc(long double x){
return
#include "/.../filename"
}
int main{
"Maths stuff happens here"
}
This works as intended, but I want to be able to speed the process up and read the filename from the terminal rather than entering it into the script each time. To do this I tried using the following:
int g_argc;
char **g_argv;
void process_command_()
{
filename=argv[1]
}
double myfunc(long double x){
return
#include filename
}
int main{
"Maths stuff happens here"
}
It was somewhat of a stretch to think this would work, but I am unsure of how to read the filename from the terminal into my function as though I had typed it in myself due to the function being outside of int main(). I have looked around StackExchange and have found similar problems but none of the solutions has worked for my case.
Any help is greatly appreciated.
Edit for clarity This code is a numerical integration code which takes a very large integrand as input from a text file and the integration is done using the CUBA library. I moved the process from Maple into C++ to get a speed and accuracy increase. The script works wonderfully and can replicate known values ~ 400 times faster than what Maple/Mathematica can do.
Perhaps the easiest way for you would be this.
double myfunc(long double x){
return
#include MY_FILENAME
}
Then when you the compiler in process_command(), pass it an additional argument:
"-DMY_FILENAME=/path/to/your/file"
This is a string you need to build out of argv[1] or whatever stores your filename.
If you invoke a bash script that invokes the compiler, you need to pass your filename as an argument to the script, and arrange for the script to pass it along with -DMY_FILENAME= to the compiler itself.
WhiZTiM's comment/link made me realise this is a really silly question, so apologies for wasting your time. What I want can be done in the bash script. I just define a placeholder in the main.cpp
double myfunc(long double x){
return
#include <filename>
}
Next I put together a simple bash script to copy the main.cpp run file to the directory where the input .txt file is and using sed's find and replace command it switches the filename in main.cpp for the actual file which is taken from the terminal:
input_file=( "$PWD/${#}" )
cp ${HOME}/.../main.cpp $PWD
sed -i "s|filename|${input_file}|g" main.cpp
And then compile and run commands at the end of the script.
Thank you for all your help with this question. I can't believe I didn't see the solution was so simple.

stdout to a variable c/c++

I am using int res = system("uname -p"); in my c++ code.
It will give the the result in standard output by using
fprintf(stdout,"execution returned %d.\n",res);
I want to store this result string in a variable, I am unable to store it.
I google it but unable to find proper solution, Can any one tell me the correct way.
First, you don't need to run the uname command programmatically to get your processor. You can simply run the uname(2) syscall (which the uname command invokes). And you could also read and parse /proc/cpuinfo from your program.
If you wanted to read the output of some command, use popen(3) library function.
See also my answer to a related question.

Running c++ in browser

I have written some basic c++ programs in one of my classes for school. I was wondering if it was possible to somehow virtually run the program in a broswer. I would like to post the program to my website. Once its posted, a person could access the program, run the program, and, interact with the program. I'm not trying to write C++ for my website, it would be more for an interactive portfolio.
Is this possible?
Use codepad, a website which lets you compile and share code online.
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Hello, Stack Overflow!" << std::endl;
return 0;
}
There is also Google Native Client SDK that allows C++ code to run in browser. Microsoft Active X is also a viable option. I am only saying it is possible not recommended.
You can only run the program on your server, not on the client's machine.
At least not without downloading and manually executing it. Anything else would be an open door for malware...
I see two options, but both very overkill:
Write (or find) a C++ interpreter in JavaScript
Use a VM running an operating system (e.g. jslinux and demonstrate your programs there.
The sensible option is to just give people a way to view and download the source code, I guess.
Google chrome supports this: http://www.readwriteweb.com/cloud/2011/08/google-officially-announces-cc.php
But it's by no means "mainstream" or standards-based.
Another solution (codepad like) would be to use https://ideone.com/ which seems much nicer to use than codepad, more user-friendly, but does the same:
Allow you to write C++ (60 languages possibles) directly from the browser and compile it and render result in the browser (I tried using printf and it worked fine). Possibility of forking source code.
https://ideone.com/baYzfe
The following two programs are quite useful :
1) Ideone
2) Codepad
You can compile, run, and share code online in any browser.
You can use Emscripten to compile C++ to Javascript. Emscripten can compile LLVM bitcode to Javascript. Some demos of Emscripten can be found here, including a raytracer and a text-to-speech engine that was compiled from C++ to Javascript.
To run x86 binaries in a web browser, you could also use an emulator such as v86. This is one possible way to compile and run C++ programs in a browser.
One of the best sites for running C++ and other multiple languages online is Repl.it
This example: repl.it/#abranhe/stackoverflow
#include <iostream>
int main() {
std::cout << "Hello Stackoverflow\n";
return 0;
}
One of the biggest pros it has is that you can work with multiple files, working with header (header.h) files etc. None of the below websites provide this option:
Codepad.org
JSLinux
IDEone
I really recommend it! You will love it!
Also wanted to add Google Colab here as an option:
Cell 1:
%%writefile hello.cpp
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Hello, Stack Overflow!" << std::endl;
return 0;
}
Cell 2:
%%script bash
g++ hello.cpp -o test
ls -laX
./test

Timing the runtime of a program via the Terminal

I've written a C++ program of which i would like to time the length of time it takes to complete - is there some terminal command i could use?
You can use the "time" command available in most (maybe all) the linux distributions. It will print the time spent as system, as user, and the total time.
For example
bash-4.1$ time (sleep 1; sleep 1)
will output something like
real 0m2.020s
user 0m0.014s
sys 0m0.005s
As you can see with the parenthesis you can launch every command chain you wish.
It's called time in *nix
Iterate over the function several times (1000's probably) so you can get a large enough number. Then use time.h to create two variables of type time_t - one before execution, one after. Subtract the two and divide by the iterations.
Or Measure-Command in PowerShell.
I try to better explain :)
If you have compiled your code using g++, for example:
g++ -std=c++14 c++/dijkstra_shortest_reach_2.cpp -o dsq
In order to run it, you type:
./dsq
In order to run it with a file content as an input, you type:
./dsq < input07Dijkstra.txt
Now for the answer.
In order to get the duration of the program output to the screen, just type:
time(./dsq < input07Dijkstra.txt)
Or without an input:
time(./dsq)
For the first command my output is:
real 0m16.082s
user 0m15.968s
sys 0m0.089s
Hope it helps!