« first day (231 days earlier)      last day (2622 days later) » 

6:07 AM
I need to find no. of seconds elapsed since Jan 1st 1970 UTC time in c++. How to do that
I wrote this program. time_t timer;
struct tm * y1970 = { 0 };
time(&timer);

y1970->tm_hour = 0; y1970->tm_min = 0; y1970->tm_sec = 0;
y1970->tm_year = 70; y1970->tm_mon = 0; y1970->tm_mday = 1;

y1970 = gmtime(&timer);
but I need the result in double
Can someone pls help me out finding No. of seconds elapsed since Jan 1st 1970 UTC time
 
4 messages moved from Lounge<C++>
 
 
1 hour later…
nwp
7:26 AM
@geetha what is the y1970 for? It doesn't do what you want it to do.
 
 
3 hours later…
10:39 AM
@nwp sorry, I actually posted you an incorrect code
what I tried is I tried to use time_t and got my gmtime. its working fine in online compiler. but somehow in VS2015, it reduces 10 hours from the correct gmtime and shows the result (Same code works fine with online compiler but shows less 10 hours in VS console application)
So I thought of finding gmtime in seconds using chrono library
do you have idea of how to get gmtime since jan 1 1970 in seconds
?
#include <iostream>
#include <chrono>
#include <ctime>

int main()
{
    std::chrono::time_point<std::chrono::system_clock> p1, p2, p3;

    p2 = std::chrono::system_clock::now();
    p3 = p2 - std::chrono::hours(24);

    std::time_t epoch_time = std::chrono::system_clock::to_time_t(p1);
    std::cout << "epoch: " << std::ctime(&epoch_time);
    std::time_t today_time = std::chrono::system_clock::to_time_t(p2);
    std::cout << "today: " << std::ctime(&today_time);

    std::cout << "seconds since epoch: "
Found out
Thank you for your time
 
time_t gives seconds from the epoch?
you already have the information
try std::cout<<epoch_time;
 
std::chrono::duration_cast<std::chrono::seconds>
yes this i just found out now
Earlier also I found out how to get seconds difference from jan 1 1970 till current utc time
 
user2015064
I can't find any way to format the text to the right.
 
my time is +10 hours from GMTime
but VS somehow shows -10hrs from current GMTime
 
which country are you from?
 
10:50 AM
where as the same code gives correct result in CPlusplus.com
AU
 
Can you show the code which gives -10 hrs?
 
user2015064
Hello?
 
@xersi can you elaborate on what you are doing?
 
user2015064
I tried "cin >> setw(5) >> right >> number;" but I still have to input text from the left instead of from the right. What happened?
 
are you using using namespace std;?
 
user2015064
10:54 AM
Yes with std:: the code is "std::cin >> std::setw(5) >> std::right >> number;"
 
if you are writing code for your school project, it is fine to use using namespace std; but it is not a good practice
 
user2015064
I wrote the code with std::. So help me already.
 
try std::cout<<std::right<<std::setw(5);
 
> Has no effect on input.
 
oops, I thought you were using cout
 
user2015064
10:59 AM
Thanks I will try it out. I am trying to input a matrix that is why I want to input the numbers from the right instead of from the left (as we usually do).
 
streams are always sequential
If you want to read the input on a line in different order, read the entire line, and then manipulate it however you see fit
 
user2015064
I will try the other method. I know pressing Enter will generate a new line anyways so I am stupid. I thought I might want to input multiple numbers in a line in an arranged way.
 
user2015064
Thanks :)
 
1:48 PM
Sorry for interrupting, but can you make a std::vector of different std::tuple? I thought of std::any, but that isn't avaliable in VS yet, maybe I'll try boost::any. Is that a good aproach? Should I do something more like template <typename ...Args> std::vector<std::tuple<boost::any, ...Args>>?
 
1 message moved from Lounge<C++>
 
nwp
@kim366 do you want to have 1 vector of different types or different vectors per type?
 
Preferrably a single vector, the tuples will be added dynamically
 
nwp
how will you access an element from the vector without knowing what is in it? You will need to have some commonality between the types or some way to retrieve the original type from the common type.
 
I will not use the single values, only compare two tuples, the look-up table being the vector
 
nwp
1:56 PM
Tuples of different types always compare as different? tuple<int>{0} != tuple<unsigned>{0} is correct?
 
Yes, correct
Okay, maybe I'll take a step back, maybe I've been doing it all wrong.
I am trying to use the observer pattern, but with arguments and it should only notify, when the arguments are the same. I defining events like

namespace pth
{
using OnTileClick = std::tuple<Tile*>;
}
 
nwp
If all you care about is if the tuple has been inserted already you could use the variadic template version and std::find the given tuple in there. You then don't need to deal with comparing tuples of different types.
 
Okay, now I have another problem.. What if I have two events that use the same syntax? It will also fire then
I guess I should take an entirely different approach? Online I have never seen the observer pattern with arguments yet..
 
nwp
I think using tagged unions is the common way to do this, but that essentially limits you to PODs. If that is not a problem for you then maybe that is the way to go.
 
2:44 PM
At this point I think I'll scrap the whole observer pattern, which would also interfere with the state pattern, anyways. Instead I'll use the Passkey pattern.
 
nwp
Maybe you should not put so much faith in patterns.
 
I still have to base the core of the project on something. Do you have any other suggestion on implementing an EventManager?
 
nwp
@kim366 Use one of the bazillion event managers that already exist and have been thoroughly tested by millions of users as a basis.
 
Can you recommend me one? I can't seem to find any
I think I found, what I am looking for! stackoverflow.com/a/25723086/3560202
 
nwp
@kim366 There is Qt, SFML, SDL and anything that calls itself a game engine usually has an event handler too.
 
2:59 PM
SFML doesn't have it, that's the problem. I am using it and I wanted to expand it.
Qt would be a pretty big expansion and learning curve again...
 
all you really need is a fifo queue and some code regularly checking it
 
3:22 PM
True, but I still can't store any arguments in a queue
 
by pointer or union
 
 
5 hours later…
8:43 PM
if I want to start a program from command line, and know that I may risk getting into an infinite loop, is there a way to not make it bring the computer down with it?
 
infinite loops in general don't bring the computer down with it
you need to make more effort than that
(see fork bombs)
 
well, say the program includes while(true){std::cout<<"heh"} on the main thread
 
task manager -> kill the program
 
I won't be able to stop the program via cmd line
or do anything with the computer
well, first of all I'm a linux person, and when I do ./main (if I outputted to main) from the command line, then any interaction with the computer is skipped
I need to hardware force it to shut down
 
or if you're using linux, killall name_of_the_program
 
8:47 PM
except I can't use the command line or interact with the computer at all
was wondering if there was somehting else I could do instead of ./main
 
@towc this is not true unless you have an ancient PC
also multiple terminal sessions are a thing
 
I use debian without a DE
 
well then you get what you're asking for
 
which makes everything quite a lot faster than windows or ubuntu, or simply debian with a DE
which is why I'm asking if there's an easy workaround, there's got to be
 
also there are terminal multiplexers (tmux, screen)
also even without GUI or terminal multiplexers there are multiple login sessions
just Alt+F2 -> Alt+F6 over them
 
8:50 PM
sorry, I probably didn't explain myself too well
I have a window manager, just not a DE
debian+i3
which works really well
and that's not the problem
 
in which case you do $mod+Enter to launch a new console and type in killall main
 
if I run ./main on a terminal, the whole computer seems to be locked
inputs aren't parsed
the i3status bar still works, though
so stuff is still running, but keys aren't captured and mouse won't move
so I can't close the terminal or go to another one
almost all non-vital resources seem to be going to that program
 
Works on My Machineā„¢
 
I know there's nice stuff you can do with terminals, like (./main) will run it not as a sub-process of the current terminal
so I was wondering if there was some other trick for running in an easily killable thread, or one with restricted resources anyway
 
also: Ctrl+C doesn't work?
there's limit utility which you could use
 
8:58 PM
if keys aren't captured, ^C won't work, no...
and looking into limit now, thanks
 
I'll say it again: a simple while(true){ std::cout << "heh"; } has no way in hell to bring down your computer because it uses only one core
 
maybe your computer :P
mine isn't ancient, but it did
and I think windows has some clever management for that
and I'm grateful for linux's flexibility, I just need to find a way to restrict it when I want to
 
nwp
@towc My computer gets stuck running a single program, but it is really fast at that :P
 
so what's your solution?
if any
other than "write better code damnit"
I'm experimenting and stuffs
I forgot to increment a variable in a while loop for example, and accidentally ran it
 
nwp
@towc I would say use a better OS. I'm on regular Debian right now and it has none of those problems. I think you screwed something important up if you can't even CTRL-C a program.
 
9:02 PM
9 mins ago, by milleniumbug
Works on My Machineā„¢
 

« first day (231 days earlier)      last day (2622 days later) »