Skip to content

Instantly share code, notes, and snippets.

@nazavode
Last active March 10, 2019 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nazavode/88bb66bbba09a1acf16ddb9bbc1a314e to your computer and use it in GitHub Desktop.
Save nazavode/88bb66bbba09a1acf16ddb9bbc1a314e to your computer and use it in GitHub Desktop.
Remap a timepoint to a given time frame in the same day, meant to be used to move git commits to another point in time
#!/usr/bin/env python
# Copyright: (c) 2019, Federico Ficarelli <federico.ficarelli@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
import argparse
import datetime as dt
TIME_FORMAT = "%H:%M"
parser = argparse.ArgumentParser(
description="""Remap a timepoint to a given time frame in the same day,
respecting the original time of day relative position in the 24 hours time
frame.
$ redate "@1552049994 +0100"
Fri Mar 8 13:59:19 2019 +0100
$ redate -f 5:00 -t 10:00 "@1552049994 +0100"
Fri Mar 8 07:54:58 2019 +0100
This is meant to be used to move git commits inside a specified time
frame:
$ git log --format="%cd"
Tue Feb 19 01:23:44 2019 +0100
Sun Feb 17 20:35:01 2019 +0100
Sun Feb 17 18:25:34 2019 +0100
Sun Feb 17 13:04:21 2019 +0100
Fri Feb 15 17:53:17 2019 +0100
$ git filter-branch --env-filter '
GIT_COMMITTER_DATE=`redate -f 9:30 -t 14:00 "${GIT_COMMITTER_DATE}"`
GIT_AUTHOR_DATE="${GIT_COMMITTER_DATE}"'
$ git log --format="%cd"
Tue Feb 19 09:45:42 2019 +0100
Sun Feb 17 13:21:33 2019 +0100
Sun Feb 17 12:57:17 2019 +0100
Sun Feb 17 11:57:03 2019 +0100
Fri Feb 15 12:51:14 2019 +0100
""",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"timepoint",
metavar="GIT_TIMESTAMP",
type=str,
help="""the timepoint to be remapped in git internal format
(utc timestamp with timezone offset, e.g.: @1552049994 +0100)""",
)
parser.add_argument(
"-f",
"--from",
dest="t_from",
metavar="TIME",
type=str,
default=dt.time.min.strftime(TIME_FORMAT),
help="""the time of day that marks the beginning of the valid
time frame (default: {})""".format(
dt.time.min.strftime(TIME_FORMAT)
),
)
parser.add_argument(
"-t",
"--to",
dest="t_to",
metavar="TIME",
type=str,
default=dt.time.max.strftime(TIME_FORMAT),
help="""the time of day that marks the end of the valid time frame
(default: {})""".format(
dt.time.max.strftime(TIME_FORMAT)
),
)
def remap(timepoint, timepoint_from, timepoint_to):
midnight = timepoint.replace(hour=0, minute=0, second=0, microsecond=0)
orig_seconds_since_midnight = (timepoint - midnight).total_seconds()
orig_seconds_interval = 24 * 60 * 60
dest_seconds_interval = (timepoint_to - timepoint_from).total_seconds()
dest_seconds_since_midnight = int(
(float(orig_seconds_since_midnight) / orig_seconds_interval)
* dest_seconds_interval
)
return timepoint_from + dt.timedelta(seconds=dest_seconds_since_midnight)
if __name__ == "__main__":
args = parser.parse_args()
# Parse git's internal format and extract a proper UTC timepoint
# Format is: @1552049994 +0100
s_utc_timestamp, s_utc_offset = args.timepoint.strip("@").split()
utc_timepoint = dt.datetime.fromtimestamp(int(s_utc_timestamp))
utc_timepoint_from = dt.datetime.combine(
utc_timepoint, dt.datetime.strptime(args.t_from, TIME_FORMAT).time()
)
utc_timepoint_to = dt.datetime.combine(
utc_timepoint, dt.datetime.strptime(args.t_to, TIME_FORMAT).time()
)
remapped_utc_timepoint = remap(utc_timepoint, utc_timepoint_from, utc_timepoint_to)
print("{} {}".format(remapped_utc_timepoint.strftime("%c"), s_utc_offset))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment