convert timestamp to ISO 8601 standard

pull/172/head
larteyoh 11 months ago
parent f15c36e07e
commit 937b8708af

@ -452,6 +452,7 @@ set(neroshop_tools_src
${NEROSHOP_CORE_SRC_DIR}/tools/process.cpp
${NEROSHOP_CORE_SRC_DIR}/tools/regex.cpp
${NEROSHOP_CORE_SRC_DIR}/tools/script.cpp
${NEROSHOP_CORE_SRC_DIR}/tools/timestamp.cpp
${NEROSHOP_CORE_SRC_DIR}/tools/updater.cpp
)
@ -484,7 +485,7 @@ endif()
######################################
# neroshop-daemon
set(daemon_executable "neromon")
set(daemon_src ${neroshop_crypto_src} ${neroshop_database_src} ${neroshop_network_src} ${NEROSHOP_CORE_SRC_DIR}/protocol/messages/msgpack.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/p2p/kademlia.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/p2p/mapper.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/p2p/node.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/p2p/routing_table.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/rpc/json_rpc.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/transport/client.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/transport/ip_address.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/transport/server.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/transport/zmq_client.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/transport/zmq_server.cpp ${NEROSHOP_CORE_SRC_DIR}/tools/base64.cpp ${NEROSHOP_CORE_SRC_DIR}/tools/logger.cpp ${NEROSHOP_CORE_SRC_DIR}/tools/timer.cpp)
set(daemon_src ${neroshop_crypto_src} ${neroshop_database_src} ${neroshop_network_src} ${NEROSHOP_CORE_SRC_DIR}/protocol/messages/msgpack.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/p2p/kademlia.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/p2p/mapper.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/p2p/node.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/p2p/routing_table.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/rpc/json_rpc.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/transport/client.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/transport/ip_address.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/transport/server.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/transport/zmq_client.cpp ${NEROSHOP_CORE_SRC_DIR}/protocol/transport/zmq_server.cpp ${NEROSHOP_CORE_SRC_DIR}/tools/base64.cpp ${NEROSHOP_CORE_SRC_DIR}/tools/logger.cpp ${NEROSHOP_CORE_SRC_DIR}/tools/timer.cpp ${NEROSHOP_CORE_SRC_DIR}/tools/timestamp.cpp)
add_executable(${daemon_executable} src/daemon/main.cpp ${daemon_src})#target_link_libraries(daemon ${curl_src} ${OPENSSL_LIBRARIES}) # curl requires both openssl(used in monero) and zlib(used in dokun-ui)
install(TARGETS ${daemon_executable} DESTINATION bin)
if(NEROSHOP_USE_LIBJUICE)

@ -553,53 +553,92 @@ Page {
Flickable {
anchors.fill: parent
contentWidth: parent.width; contentHeight: (ratingsList.delegateHeight * ratingsList.count) + (ratingsList.spacing * (ratingsList.count - 1))
contentWidth: parent.width; contentHeight: ratingsList.contentHeight + (ratingsList.margins * 2) // to fill the top + bottom padding
clip: true
/*ScrollBar.vertical: ScrollBar {
ScrollBar.vertical: ScrollBar {
policy: ScrollBar.AsNeeded
}*/
ListView {
id: ratingsList
/*property int margins: 10
anchors.left: parent.left; anchors.top: parent.top
anchors.margins: margins*/
anchors.fill: parent//width: parent.width
property real margins: 15
anchors.topMargin: ratingsList.margins; anchors.bottomMargin: anchors.topMargin
//height: childrenRect.height
model: profilePage.ratingsModel
spacing: 10
property real delegateHeight: 100
delegate: Rectangle {
anchors.left: parent.left; anchors.right: parent.right
anchors.leftMargin: ratingsList.margins; anchors.rightMargin: anchors.leftMargin
width: parent.width; height: ratingsList.delegateHeight
color: index % 2 === 0 ? "#f0f0f0" : "#e0e0e0"
radius: 3
// Display review data using Text elements
Text {
id: commentsLabel
anchors.left: parent.left
anchors.leftMargin: 10
text: "Comments: " + modelData.comments
}
Text {
id: scoreLabel
anchors.right: parent.right
anchors.rightMargin: 10
anchors.top: parent.top
text: "Score: " + modelData.score
}
Text {
anchors.right: parent.right
anchors.rightMargin: 10
anchors.top: scoreLabel.bottom
text: "Rater ID: " + modelData.rater_id
}
}
}
} // ScrollView
}
}
ListView {
id: ratingsList
/*property int margins: 10
anchors.left: parent.left; anchors.top: parent.top
anchors.margins: margins*/
anchors.fill: parent//width: parent.width
property real margins: 15
anchors.topMargin: ratingsList.margins; anchors.bottomMargin: anchors.topMargin
model: profilePage.ratingsModel
spacing: 10
property real delegateHeight: 100
delegate: Rectangle {
anchors.left: parent.left; anchors.right: parent.right
anchors.leftMargin: ratingsList.margins; anchors.rightMargin: anchors.leftMargin
width: parent.width; height: Math.max(ratingsList.delegateHeight, ratingsList.delegateHeight + commentsLabel.height)
color: "transparent"//(NeroshopComponents.Style.darkTheme) ? "#101010" : "#efefef"//index % 2 === 0 ? "#f0f0f0" : "#e0e0e0"
radius: 6
property string textColor: (NeroshopComponents.Style.darkTheme) ? "#ffffff" : "#000000"
// Display review data using Text elements
Component.onCompleted: console.log("commentsLabel.height",commentsLabel.height)
Rectangle {
id: avatarRect
anchors.left: parent.left
anchors.leftMargin: 10
anchors.verticalCenter: parent.verticalCenter
width: parent.height - 20; height: width
Image {
anchors.centerIn: parent
width: parent.width - (avatarRect.border.width * 2); height: width
fillMode: Image.PreserveAspectCrop
mipmap: true
source: "https://api.dicebear.com/6.x/identicon/png?seed=%1".arg(modelData.rater_id)
}
}
Column {
anchors.left: avatarRect.right
anchors.leftMargin: 10
anchors.top: avatarRect.top
width: parent.width - (avatarRect.anchors.leftMargin + avatarRect.width + anchors.leftMargin + parent.anchors.rightMargin)
//height: parent.height
spacing: 30//15
Text {
anchors.left: parent.left
anchors.leftMargin: 0
text: modelData.rater_id
color: parent.parent.textColor
font.bold: true
wrapMode: Text.WordWrap
elide: Text.ElideRight
width: parent.width
}
Text {//TextArea {
id: commentsLabel
//anchors.left: parent.left
//anchors.leftMargin: 0
text: modelData.comments// reviews cannot surpass 1024 bytes
color: parent.parent.textColor
////readOnly: true
wrapMode: Text.WordWrap////Text.Wrap//
width: parent.width
height: (text.length >= 1024) ? 200 : 100//Math.min(100, (text.length * 10) / parent.height)//200
////background: Rectangle { color: "transparent" }
elide: Text.ElideRight
}
} // Column
// TODO: add timestamp too
Text {
id: scoreLabel
anchors.right: parent.right
anchors.rightMargin: 10
//anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.bottomMargin: 10
text: "Score: " + modelData.score
color: parent.textColor
}
} // delegate
} // ListView
} // Flickable
} // StackLayout Item
} // StackLayout
} // root ColumnLayout
} // end of Page

@ -9,6 +9,7 @@
#include "../../version.hpp"
#include "../../tools/base64.hpp"
#include "mapper.hpp"
#include "../../tools/timestamp.hpp"
#include <nlohmann/json.hpp>
@ -20,6 +21,7 @@
#include <unordered_set>
namespace neroshop_crypto = neroshop::crypto;
namespace neroshop_timestamp = neroshop::timestamp;
neroshop::Node::Node(const std::string& address, int port, bool local) : sockfd(-1), bootstrap(false), check_counter(0) {
// Convert URL to IP (in case it happens to be a url)
@ -195,26 +197,6 @@ std::string neroshop::Node::generate_node_id(const std::string& address, int por
//-----------------------------------------------------------------------------
static std::string get_most_recent_timestamp(const std::string& timestamp1, const std::string& timestamp2) {
std::tm tm1{};
std::istringstream ss1(timestamp1);
ss1 >> std::get_time(&tm1, "%Y-%m-%d %H:%M:%S");
std::time_t time1 = std::mktime(&tm1);
std::tm tm2{};
std::istringstream ss2(timestamp2);
ss2 >> std::get_time(&tm2, "%Y-%m-%d %H:%M:%S");
std::time_t time2 = std::mktime(&tm2);
if (time1 > time2) {
return timestamp1;
} else {
return timestamp2;
}
}
//-----------------------------------------------------------------------------
// Define the list of bootstrap nodes
std::vector<neroshop::Peer> bootstrap_nodes = {
{"node.neroshop.org", NEROSHOP_P2P_DEFAULT_PORT},
@ -418,7 +400,7 @@ int neroshop::Node::set(const std::string& key, const std::string& value) {
std::string current_last_updated = current_json["last_updated"].get<std::string>();
// Compare the new json's last_updated timestamp with the current json's own
// And choose whichever has the most recent timestamp then exit the function
std::string most_recent_timestamp = get_most_recent_timestamp(last_updated, current_last_updated);
std::string most_recent_timestamp = neroshop_timestamp::get_most_recent_timestamp(last_updated, current_last_updated);
// If this node has the up-to-date value, return true as there is no need to update
if(most_recent_timestamp == current_last_updated) {
std::cout << "Value for key (" << key << ") is already up-to-date" << std::endl;
@ -880,20 +862,6 @@ void neroshop::Node::republish() {
//-----------------------------------------------------------------------------
static bool is_expired(const std::string& expiration_date) {
// Get the current UTC time
std::time_t current_time = std::time(nullptr);
std::tm* current_tm = std::gmtime(&current_time);
// Parse the expiration date string
std::tm expiration_tm{};
std::istringstream ss(expiration_date);
ss >> std::get_time(&expiration_tm, "%Y-%m-%d %H:%M:%S");
// Compare the expiration time with the current time
return (std::mktime(&expiration_tm) <= std::mktime(current_tm));
}
bool neroshop::Node::validate(const std::string& key, const std::string& value) {
assert(key.length() == 64 && "Key length is not 64 characters");
assert(!value.empty() && "Value is empty");
@ -917,7 +885,7 @@ bool neroshop::Node::validate(const std::string& key, const std::string& value)
if(json.contains("expiration_date")) {
assert(json["expiration_date"].is_string());
std::string expiration_date = json["expiration_date"].get<std::string>();
if(is_expired(expiration_date)) {
if(neroshop_timestamp::is_expired(expiration_date)) {
std::cerr << "Data has expired (exp date: " << expiration_date << " UTC)\n";
// Remove the data from hash table if it was previously stored
if(has_key(key)) {

@ -75,7 +75,7 @@ std::string neroshop::Seller::list_item(
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now); // current time
std::stringstream date;
date << std::put_time(std::gmtime(&in_time_t), "%Y-%m-%d %H:%M:%S");
date << std::put_time(std::gmtime(&in_time_t), "%Y-%m-%dT%H:%M:%SZ");
std::string utc_time = date.str();//std::cout << "utc time: " << utc_time << "\n";
std::string signature = wallet->sign_message(listing_id, monero_message_signature_type::SIGN_WITH_SPEND_KEY);//std::cout << "signature: " << signature << "\n\n";
@ -148,7 +148,7 @@ void neroshop::Seller::delist_item(const std::string& listing_key) {
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now); // current time
std::stringstream datetime;
datetime << std::put_time(std::gmtime(&in_time_t), "%Y-%m-%d %H:%M:%S");
datetime << std::put_time(std::gmtime(&in_time_t), "%Y-%m-%dT%H:%M:%SZ");
std::string utc_time = datetime.str();
value_obj["expiration_date"] = utc_time;//value_obj["valid_until"] = utc_time;
// Send set request containing the updated value with the same key as before
@ -362,7 +362,7 @@ void neroshop::Seller::set_stock_quantity(const std::string& listing_key, int qu
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now); // current time
std::stringstream datetime;
datetime << std::put_time(std::gmtime(&in_time_t), "%Y-%m-%d %H:%M:%S");
datetime << std::put_time(std::gmtime(&in_time_t), "%Y-%m-%dT%H:%M:%SZ");
std::string utc_time = datetime.str();
value_obj["last_updated"] = utc_time;
// Send set request containing the updated value with the same key as before

@ -0,0 +1,61 @@
#include "timestamp.hpp"
#include <chrono>
#include <iomanip>
#include <sstream>
std::string neroshop::timestamp::get_current_utc_timestamp() {
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now); // current time
std::stringstream datetime;
datetime << std::put_time(std::gmtime(&in_time_t), "%Y-%m-%dT%H:%M:%SZ");
std::string utc_timestamp = datetime.str();
return utc_timestamp;
}
bool neroshop::timestamp::is_expired(const std::string& expiration_date) {
// Get the current UTC time
std::time_t current_time = std::time(nullptr);
std::tm* current_tm = std::gmtime(&current_time);
// Parse the expiration date string
std::tm expiration_tm{};
std::istringstream ss(expiration_date);
ss >> std::get_time(&expiration_tm, "%Y-%m-%dT%H:%M:%SZ");
// Compare the expiration time with the current time
return (std::mktime(&expiration_tm) <= std::mktime(current_tm));
}
std::string neroshop::timestamp::get_most_recent_timestamp(const std::string& timestamp1, const std::string& timestamp2) {
std::tm tm1{};
std::istringstream ss1(timestamp1);
ss1 >> std::get_time(&tm1, "%Y-%m-%dT%H:%M:%SZ");
std::time_t time1 = std::mktime(&tm1);
std::tm tm2{};
std::istringstream ss2(timestamp2);
ss2 >> std::get_time(&tm2, "%Y-%m-%dT%H:%M:%SZ");
std::time_t time2 = std::mktime(&tm2);
if (time1 > time2) {
return timestamp1;
} else {
return timestamp2;
}
}
// Convert Unix timestamp to UTC time
std::tm neroshop::timestamp::unix_timestamp_to_utc(time_t unix_timestamp) {
std::tm utc_time;
gmtime_r(&unix_timestamp, &utc_time);
return utc_time;
}
// Convert UTC time to Unix timestamp
time_t neroshop::timestamp::utc_to_unix_timestamp(const std::string& utc_time_str) {
std::tm utc_time = {};
std::istringstream ss(utc_time_str);
ss >> std::get_time(&utc_time, "%Y-%m-%dT%H:%M:%SZ");
return timegm(&utc_time);
}

@ -0,0 +1,20 @@
#pragma once
#include <iostream>
#include <string>
#include <ctime>
namespace neroshop {
namespace timestamp {
std::string get_current_utc_timestamp();
std::string get_most_recent_timestamp(const std::string& timestamp1, const std::string& timestamp2);
bool is_expired(const std::string& expiration_date);
std::tm unix_timestamp_to_utc(time_t unix_timestamp);
time_t utc_to_unix_timestamp(const std::string& utc_time_str);
}
}
Loading…
Cancel
Save