• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Solved [C++] Current system time

Shadow Dan

Sh4dowDan
Joined
Jun 5, 2010
Messages
344
Reaction score
90
Location
Poland
Hi everyone.

So here is my problem:
I want to see current time when someone log into my server.
I got this:
Code:
[God] Shadow has logged in.
and want this in console (current my pc time):
Code:
03:22:55 [God] Shadow has logged in.

In TFS for tibia 8.6 it was something like:
Code:
    std::clog << ">> Loading mods..." << std::endl;
Code:
[2:16:57.625] >> Loading mods...
So current time 2:16:57.

my player.cpp
Code:
std::cout << name << " has logged in." << std::endl;

here is full file player.cpp
http://paste.ofcode.org/36AQLL37HTFzegeJywQXmPz

Sadly its too hard for me so i ask for help :<

so far i managed to do it in lua but it takes 1 more line in console
login.lua
Code:
local timenow = os.date("%H:%M:%S", os.time())
print(timenow)
it looks like this:
Code:
[God] Shadow has logged in.
03:03:49
I want only 1 line per login :/

This looks really crap
Code:
[God] Shadow has logged out
[God] Shadow has logged in.
03:22:51
03:22:51
[God] Shadow has logged out
[God] Shadow has logged in.
03:22:52
03:22:52
[God] Shadow has logged out
[God] Shadow has logged in.
03:22:53
03:22:54
[God] Shadow has logged out
[God] Shadow has logged in.
03:22:55
 
Last edited:
If you want something like this:
Xqj3gqG.png


Apply this patch to your source root (the directory that contains data/ and src/). If you don't know how to apply patches, on Linux you have to run:
Code:
patch -p1 -i <path to patch file>
On Windows, I don't know. You might just look at the patch and copy the relevant lines though.

Patch code, in case you can not open the link:
Code:
From b05c021f0023fb17ab502e0b7dc0baecedcb0c7f Mon Sep 17 00:00:00 2001
From: Ranieri Althoff <[email protected]>
Date: Mon, 1 Jun 2015 02:16:42 -0300
Subject: [PATCH] Add timestamp to player log in/out message

---
src/player.cpp | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/player.cpp b/src/player.cpp
index 82425d7..251fa6c 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -1160,7 +1160,10 @@ void Player::onCreatureAppear(Creature* creature, bool isLogin)
             bed->wakeUp(this);
         }
-        std::cout << name << " has logged in." << std::endl;
+        using clk = std::chrono::system_clock;
+        auto now = clk::to_time_t(clk::now());
+        std::cout << "[" <<  std::put_time(std::localtime(&now), "%T")
+            << "] " << name << " has logged in." << std::endl;
         if (guild) {
             guild->addMember(this);
@@ -1375,7 +1378,10 @@ void Player::onRemoveCreature(Creature* creature, bool isLogout)
         g_chat->removeUserFromAllChannels(*this);
-        std::cout << getName() << " has logged out." << std::endl;
+        using clk = std::chrono::system_clock;
+        auto now = clk::to_time_t(clk::now());
+        std::cout << "[" <<  std::put_time(std::localtime(&now), "%T")
+            << "] " << name << " has logged out." << std::endl;
         if (guild) {
             guild->removeMember(this);
--
2.4.2
 
Last edited:
Hmm it's hard to know without testing, but surely it can't be too hard?

This line:
Code:
std::cout << name << " has logged in." << std::endl;

Prints the name " has logged in" and ends that line, hence the "std::endl;". What you want to do is to add some time prior to the name, in this manner:
Code:
std::cout << time << name << " has logged in." << std::endl;

The question is what the time variable should contain. Maybe you could use this line:
Code:
std::cout << formatDate(time(nullptr)).c_str() << " " << name << " has logged in." << std::endl;

There are lots of methods to try, there must be some method this server has already used to get the current date.

Ignazio
 
Back
Top