I have the following set of filenames that are unfortunately in UTC format. They were named as such and I unfortunately do not have the option to set it so that the file get created in the local timezone. I am 12hours ahead of UTC, so basically, needed to add 12 to the date and time below?
Anyone have a clue how to do date arithmetic and add 12 to the UTC date and time above to come up with the local time zone? Maybe someone has already done it in their lifetime before
Initial thought is adding 12 to the hour and if less than or equal to 23, then no date change and that is the local timezone. If over 23, then subtract 24 to it and add 1 to the date? But what if the date becomes greater than the supposed number of days for the month? I am sooo lost
Since that situation means change of month and day=01, it means you will have to add 1 to month value and day=01...
Then it will be what if month>12? ...
#!/usr/bin/python3
import glob
from datetime import datetime, timedelta
for file in glob.glob("TEST_*"):
items = file.split("_", 3)
# convert 2nd & 3rd item to datetime object & add hours
dt_12 = datetime.strptime(items[1] + items[2], "%Y%m%d%H%M") + timedelta(hours=12)
# convert back to string w/o seconds & join all together
print("mv", file, "_".join([items[0], dt_12.strftime("%Y%m%d_%H%M"), items[3]]))
Note that this assumes that the files are named like TEST_<YYYYMMDD>_<HHMM>_<REST>. Also, only shell commands are printed, nothing will be changed. If the output is acceptable, you can rename them e.g. via
$ python3 script.py > rename && bash rename
Of course this could also be done in shell, perl, etc.
Is that file name format fixed? My solution as presented (below) is dependent on character location. You may need to adjust accordingly, or extract parts and make things more dynamic.
In short, don't do this yourself. Date and time are fraught with gotchas, corner cases, dark alleys, and some sharp edges. Instead have the system do it for you.
You didn't say what platform you're on. -- I'm going to assume that you're on Linux or otherwise have access to the GNU date command.
Place the file name in a variable; e.g. FILE.
% FILE=TEST_20240626_2150_68e9d7ca_quickbrownfox.txt
% date -d "${FILE:5:4}-${FILE:9:2}-${FILE:11:2} ${FILE:14:2}:${FILE:16:2} UTC"
Wed Jun 26 04:50:00 PM CDT 2024
Leverage GNU date's ability to easily do the conversion for you if you can.
Yes, the file name format is fixed sort of, delimited by underscore. That's how I will be receiving the names. Sender doesn't or most probably don't know or doesn't have the option to change the generated names to our local timezone.
My only concern is hoping I don't change the system date by mistake I am not running as root anyway, so I think I'll be safe on that note.
I'm sure that you can play string manipulation games to split the file name apart and access the date fields.
You don't need to worry about altering the system date if you aren't running with root privileges. Furthermore, GNU's date command needs the --set / -s option to and root privileges to actually change the system date / time.