How to convert UTC or do date arithmetic

Hi,

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?

TEST_20240625_1110_73fb93e3_quickbrownfox.txt
TEST_20240625_1636_6e3ae299_quickbrownfox.txt
TEST_20240625_1856_d06ad186_quickbrownfox.txt
TEST_20240625_2002_38f159f0_quickbrownfox.txt
TEST_20240625_2045_39a9e47a_quickbrownfox.txt
TEST_20240625_2045_5135e921_quickbrownfox.txt
TEST_20240625_2046_9a77e033_quickbrownfox.txt
TEST_20240625_2240_052f25fc_quickbrownfox.txt
TEST_20240625_2242_1cdcbbcd_quickbrownfox.txt
TEST_20240626_0238_026623d4_quickbrownfox.txt
TEST_20240626_0239_cba2d8f8_quickbrownfox.txt
TEST_20240626_0452_27f3681d_quickbrownfox.txt
TEST_20240626_0452_95225d67_quickbrownfox.txt
TEST_20240626_0453_a108f395_quickbrownfox.txt
TEST_20240626_0455_16e53500_quickbrownfox.txt
TEST_20240626_0455_193c79c1_quickbrownfox.txt
TEST_20240626_0456_66313bc9_quickbrownfox.txt
TEST_20240626_0456_91542fc9_quickbrownfox.txt
TEST_20240626_0457_6be970d0_quickbrownfox.txt
TEST_20240626_0457_e5c34000_quickbrownfox.txt
TEST_20240626_0500_5f703a58_quickbrownfox.txt
TEST_20240626_0501_949f74ee_quickbrownfox.txt
TEST_20240626_0502_028147fc_quickbrownfox.txt
TEST_20240626_0503_853bc31b_quickbrownfox.txt
TEST_20240626_0547_1a2f0525_quickbrownfox.txt
TEST_20240626_0550_989a6829_quickbrownfox.txt
TEST_20240626_0713_f8ac598b_quickbrownfox.txt
TEST_20240626_1617_31e6538e_quickbrownfox.txt
TEST_20240626_2150_68e9d7ca_quickbrownfox.txt

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 :slight_smile:

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 :frowning:

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? ...

You've got the idea...

Good luck

Hi @newbie_01,

here's a first idea in python:

#!/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 :frowning: 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.