Revert "Make history entry start times more precise (down to the nanosecond) to make the recorded runtime more accurate, since currently start times are rounded to the nearest second. Though note that 'date' on MacOS doesn't support %N, so this doesn't apply to MacOS"

This reverts commit d404a73c9b since it appears to be causing test failures for linux
This commit is contained in:
David Dworken 2023-10-07 20:05:21 -07:00
parent 4bb0ebfb2e
commit 6281ae0601
No known key found for this signature in database
5 changed files with 25 additions and 27 deletions

View File

@ -152,7 +152,9 @@ func presaveHistoryEntry(ctx context.Context) {
// Don't save commands that start with a space
return
}
entry.StartTime = parseCrossPlatformTime(os.Args[4])
startTime, err := parseCrossPlatformInt(os.Args[4])
lib.CheckFatalError(err)
entry.StartTime = time.Unix(startTime, 0).UTC()
entry.EndTime = time.Unix(0, 0).UTC()
// And persist it locally.
@ -367,7 +369,11 @@ func buildHistoryEntry(ctx context.Context, args []string) (*data.HistoryEntry,
entry.ExitCode = exitCode
// start time
entry.StartTime = parseCrossPlatformTime(args[5])
seconds, err := parseCrossPlatformInt(args[5])
if err != nil {
return nil, fmt.Errorf("failed to parse start time %s as int: %w", args[5], err)
}
entry.StartTime = time.Unix(seconds, 0).UTC()
// end time
entry.EndTime = time.Now().UTC()
@ -543,16 +549,9 @@ func maybeSkipBashHistTimePrefix(cmdLine string) (string, error) {
return re.ReplaceAllLiteralString(cmdLine, ""), nil
}
func parseCrossPlatformTime(data string) time.Time {
data = strings.TrimSuffix(data, "N") // Trim the N suffix that is present on MacOS where the date CLI doesn't support %N
startTime, err := strconv.ParseInt(data, 10, 64)
lib.CheckFatalError(err)
if len(data) >= 18 {
return time.Unix(0, startTime).UTC()
} else {
return time.Unix(startTime, 0).UTC()
}
func parseCrossPlatformInt(data string) (int64, error) {
data = strings.TrimSuffix(data, "N")
return strconv.ParseInt(data, 10, 64)
}
func getLastCommand(history string) (string, error) {

View File

@ -135,18 +135,17 @@ func TestBuildHistoryEntryWithTimestampStripping(t *testing.T) {
}
}
func TestParseCrossPlatformTime(t *testing.T) {
res := parseCrossPlatformTime("1696715149")
require.Equal(t, time.Unix(1696715149, 0).UTC(), res)
res = parseCrossPlatformTime("1696715149N")
require.Equal(t, time.Unix(1696715149, 0).UTC(), res)
res = parseCrossPlatformTime("1696715218277655463")
require.Equal(t, time.Unix(0, 1696715218277655463).UTC(), res)
res = parseCrossPlatformTime("1696715218277655463N")
require.Equal(t, time.Unix(0, 1696715218277655463).UTC(), res)
func TestParseCrossPlatformInt(t *testing.T) {
res, err := parseCrossPlatformInt("123")
require.NoError(t, err)
if res != 123 {
t.Fatalf("failed to parse cross platform int %d", res)
}
res, err = parseCrossPlatformInt("123N")
require.NoError(t, err)
if res != 123 {
t.Fatalf("failed to parse cross platform int %d", res)
}
}
func TestBuildRegexFromTimeFormat(t *testing.T) {

View File

@ -1,7 +1,7 @@
function _hishtory_post_exec --on-event fish_preexec
# Runs after <ENTER>, but before the command is executed
set --global _hishtory_command $argv
set --global _hishtory_start_time (date +%s%N)
set --global _hishtory_start_time (date +%s)
hishtory presaveHistoryEntry fish "$_hishtory_command" $_hishtory_start_time & # Background Run
# hishtory presaveHistoryEntry fish "$_hishtory_command" $_hishtory_start_time # Foreground Run
end

View File

@ -13,7 +13,7 @@ function __hishtory_precommand() {
unset HISHTORY_AT_PROMPT
# Run before every command
HISHTORY_START_TIME=`date +%s%N`
HISHTORY_START_TIME=`date +%s`
CMD=`history 1`
if ! [ -z "CMD " ] ; then
(hishtory presaveHistoryEntry bash "$CMD" $HISHTORY_START_TIME &) # Background Run

View File

@ -8,7 +8,7 @@ function _hishtory_add() {
# Runs after <ENTER>, but before the command is executed
# $1 contains the command that was run
_hishtory_command=$1
_hishtory_start_time=`date +%s%N`
_hishtory_start_time=`date +%s`
if ! [ -z "$_hishtory_command " ]; then
(hishtory presaveHistoryEntry zsh "$_hishtory_command" $_hishtory_start_time &) # Background Run
# hishtory presaveHistoryEntry zsh "$_hishtory_command" $_hishtory_start_time # Foreground Run