Collect time of git commit for display in Keyhotee, add function for pretty-printing times
This commit is contained in:
parent
6c989e7b7a
commit
5543044bd3
6 changed files with 117 additions and 0 deletions
|
|
@ -16,6 +16,7 @@ INCLUDE( SetupTargetMacros )
|
||||||
INCLUDE(GetGitRevisionDescription)
|
INCLUDE(GetGitRevisionDescription)
|
||||||
|
|
||||||
get_git_head_revision(GIT_REFSPEC GIT_SHA3)
|
get_git_head_revision(GIT_REFSPEC GIT_SHA3)
|
||||||
|
get_git_unix_timestamp(GIT_UNIX_TIMESTAMP3)
|
||||||
|
|
||||||
SET( DEFAULT_HEADER_INSTALL_DIR include/\${target} )
|
SET( DEFAULT_HEADER_INSTALL_DIR include/\${target} )
|
||||||
SET( DEFAULT_LIBRARY_INSTALL_DIR lib/ )
|
SET( DEFAULT_LIBRARY_INSTALL_DIR lib/ )
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
|
#include <stdint.h>
|
||||||
#include "GitSHA3.h"
|
#include "GitSHA3.h"
|
||||||
|
|
||||||
#define GIT_SHA3 "@GIT_SHA3@"
|
#define GIT_SHA3 "@GIT_SHA3@"
|
||||||
const char* const g_GIT_SHA3 = GIT_SHA3;
|
const char* const g_GIT_SHA3 = GIT_SHA3;
|
||||||
|
#define GIT_UNIX_TIMESTAMP3 @GIT_UNIX_TIMESTAMP3@
|
||||||
|
const uint32_t g_GIT_UNIX_TIMESTAMP3 = GIT_UNIX_TIMESTAMP3;
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#define __GITSHA3_H
|
#define __GITSHA3_H
|
||||||
|
|
||||||
extern const char* const g_GIT_SHA3;
|
extern const char* const g_GIT_SHA3;
|
||||||
|
extern const uint32_t g_GIT_UNIX_TIMESTAMP3;
|
||||||
|
|
||||||
#define APPLICATION_VERSION "1.0 Beta1"
|
#define APPLICATION_VERSION "1.0 Beta1"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -124,6 +124,52 @@ function(git_describe _var)
|
||||||
set(${_var} "${out}" PARENT_SCOPE)
|
set(${_var} "${out}" PARENT_SCOPE)
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
|
function(get_git_unix_timestamp _var)
|
||||||
|
if(NOT GIT_FOUND)
|
||||||
|
find_package(Git QUIET)
|
||||||
|
endif()
|
||||||
|
get_git_head_revision(refspec hash)
|
||||||
|
if(NOT GIT_FOUND)
|
||||||
|
set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
|
||||||
|
return()
|
||||||
|
endif()
|
||||||
|
if(NOT hash)
|
||||||
|
set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
|
||||||
|
return()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# TODO sanitize
|
||||||
|
#if((${ARGN}" MATCHES "&&") OR
|
||||||
|
# (ARGN MATCHES "||") OR
|
||||||
|
# (ARGN MATCHES "\\;"))
|
||||||
|
# message("Please report the following error to the project!")
|
||||||
|
# message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
|
||||||
|
#endif()
|
||||||
|
|
||||||
|
# message(STATUS "Arguments to execute_process: ${ARGN}")
|
||||||
|
|
||||||
|
execute_process(COMMAND
|
||||||
|
"${GIT_EXECUTABLE}"
|
||||||
|
"show"
|
||||||
|
"-s"
|
||||||
|
"--format=%ct"
|
||||||
|
${hash}
|
||||||
|
${ARGN}
|
||||||
|
WORKING_DIRECTORY
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}"
|
||||||
|
RESULT_VARIABLE
|
||||||
|
res
|
||||||
|
OUTPUT_VARIABLE
|
||||||
|
out
|
||||||
|
ERROR_QUIET
|
||||||
|
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||||
|
if(NOT res EQUAL 0)
|
||||||
|
set(out "${out}-${res}-NOTFOUND")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(${_var} "${out}" PARENT_SCOPE)
|
||||||
|
endfunction()
|
||||||
|
|
||||||
function(git_get_exact_tag _var)
|
function(git_get_exact_tag _var)
|
||||||
git_describe(out --exact-match ${ARGN})
|
git_describe(out --exact-match ${ARGN})
|
||||||
set(${_var} "${out}" PARENT_SCOPE)
|
set(${_var} "${out}" PARENT_SCOPE)
|
||||||
|
|
|
||||||
|
|
@ -98,6 +98,12 @@ namespace fc {
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef fc::optional<time_point> otime_point;
|
typedef fc::optional<time_point> otime_point;
|
||||||
|
|
||||||
|
/** return a human-readable approximate time, relative to now()
|
||||||
|
* e.g., "4 hours ago", "2 months ago", etc.
|
||||||
|
*/
|
||||||
|
string get_approximate_relative_time_string(const time_point_sec& event_time);
|
||||||
|
string get_approximate_relative_time_string(const time_point& event_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
|
|
|
||||||
60
src/time.cpp
60
src/time.cpp
|
|
@ -3,6 +3,8 @@
|
||||||
#include <boost/chrono/system_clocks.hpp>
|
#include <boost/chrono/system_clocks.hpp>
|
||||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <fc/string.hpp>
|
||||||
|
#include <fc/io/sstream.hpp>
|
||||||
|
|
||||||
namespace fc {
|
namespace fc {
|
||||||
namespace bch = boost::chrono;
|
namespace bch = boost::chrono;
|
||||||
|
|
@ -33,4 +35,62 @@ namespace fc {
|
||||||
void from_variant( const fc::variant& v, fc::time_point_sec& t ) {
|
void from_variant( const fc::variant& v, fc::time_point_sec& t ) {
|
||||||
t = fc::time_point::from_iso_string(v.as_string());
|
t = fc::time_point::from_iso_string(v.as_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// inspired by show_date_relative() in git's date.c
|
||||||
|
string get_approximate_relative_time_string(const time_point_sec& event_time) {
|
||||||
|
time_point_sec now_in_sec(time_point::now());
|
||||||
|
if (event_time > now_in_sec)
|
||||||
|
return "in the future";
|
||||||
|
stringstream result;
|
||||||
|
uint32_t seconds_ago = now_in_sec.sec_since_epoch() - event_time.sec_since_epoch();
|
||||||
|
if (seconds_ago < 90)
|
||||||
|
{
|
||||||
|
result << seconds_ago << " second" << (seconds_ago > 1 ? "s" : "") << " ago";
|
||||||
|
return result.str();
|
||||||
|
}
|
||||||
|
uint32_t minutes_ago = (seconds_ago + 30) / 60;
|
||||||
|
if (minutes_ago < 90)
|
||||||
|
{
|
||||||
|
result << minutes_ago << " minute" << (minutes_ago > 1 ? "s" : "") << " ago";
|
||||||
|
return result.str();
|
||||||
|
}
|
||||||
|
uint32_t hours_ago = (minutes_ago + 30) / 60;
|
||||||
|
if (hours_ago < 90)
|
||||||
|
{
|
||||||
|
result << hours_ago << " hour" << (hours_ago > 1 ? "s" : "") << " ago";
|
||||||
|
return result.str();
|
||||||
|
}
|
||||||
|
uint32_t days_ago = (hours_ago + 12) / 24;
|
||||||
|
if (days_ago < 90)
|
||||||
|
{
|
||||||
|
result << days_ago << " day" << (days_ago > 1 ? "s" : "") << " ago";
|
||||||
|
return result.str();
|
||||||
|
}
|
||||||
|
uint32_t weeks_ago = (days_ago + 3) / 7;
|
||||||
|
if (weeks_ago < 70)
|
||||||
|
{
|
||||||
|
result << weeks_ago << " week" << (weeks_ago > 1 ? "s" : "") << " ago";
|
||||||
|
return result.str();
|
||||||
|
}
|
||||||
|
uint32_t months_ago = (days_ago + 15) / 30;
|
||||||
|
if (months_ago < 12)
|
||||||
|
{
|
||||||
|
result << months_ago << " month" << (months_ago > 1 ? "s" : "") << " ago";
|
||||||
|
return result.str();
|
||||||
|
}
|
||||||
|
uint32_t years_ago = days_ago / 365;
|
||||||
|
result << years_ago << " year" << (months_ago > 1 ? "s" : "");
|
||||||
|
if (months_ago < 12 * 5)
|
||||||
|
{
|
||||||
|
uint32_t leftover_days = days_ago - (years_ago * 365);
|
||||||
|
uint32_t leftover_months = (leftover_days + 15) / 30;
|
||||||
|
if (leftover_months)
|
||||||
|
result << leftover_months << " month" << (months_ago > 1 ? "s" : "");
|
||||||
|
}
|
||||||
|
result << " ago";
|
||||||
|
return result.str();
|
||||||
|
}
|
||||||
|
string get_approximate_relative_time_string(const time_point& event_time) {
|
||||||
|
return get_approximate_relative_time_string(time_point_sec(event_time));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue