Skip to content

Commit

Permalink
Collect time of git commit for display in Keyhotee, add function for …
Browse files Browse the repository at this point in the history
…pretty-printing times
  • Loading branch information
emfrias committed Mar 31, 2014
1 parent 6c989e7 commit 5543044
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ INCLUDE( SetupTargetMacros )
INCLUDE(GetGitRevisionDescription)

get_git_head_revision(GIT_REFSPEC GIT_SHA3)
get_git_unix_timestamp(GIT_UNIX_TIMESTAMP3)

SET( DEFAULT_HEADER_INSTALL_DIR include/\${target} )
SET( DEFAULT_LIBRARY_INSTALL_DIR lib/ )
Expand Down
3 changes: 3 additions & 0 deletions GitSHA3.cpp.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <stdint.h>
#include "GitSHA3.h"

#define 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;
1 change: 1 addition & 0 deletions GitSHA3.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __GITSHA3_H

extern const char* const g_GIT_SHA3;
extern const uint32_t g_GIT_UNIX_TIMESTAMP3;

#define APPLICATION_VERSION "1.0 Beta1"

Expand Down
46 changes: 46 additions & 0 deletions GitVersionGen/GetGitRevisionDescription.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,52 @@ function(git_describe _var)
set(${_var} "${out}" PARENT_SCOPE)
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)
git_describe(out --exact-match ${ARGN})
set(${_var} "${out}" PARENT_SCOPE)
Expand Down
6 changes: 6 additions & 0 deletions include/fc/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ namespace fc {
};

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
Expand Down
60 changes: 60 additions & 0 deletions src/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <boost/chrono/system_clocks.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <sstream>
#include <fc/string.hpp>
#include <fc/io/sstream.hpp>

namespace fc {
namespace bch = boost::chrono;
Expand Down Expand Up @@ -33,4 +35,62 @@ namespace fc {
void from_variant( const fc::variant& v, fc::time_point_sec& t ) {
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));
}
}

0 comments on commit 5543044

Please sign in to comment.