Replace Unix timestamp
-
@Dr-N ,
It takes math to convert from unix timestamp to actual date and time. Notepad++ search and replace doesn’t do math.
See our FAQ for more, including plugins that might help you implement the math.
https://community.notepad-plus-plus.org/topic/23170/faq-can-i-do-a-mathematical-replacement
-
@PeterJones said in Replace Unix timestamp:
Notepad++ search and replace doesn’t do math.
True. Fortunately any scripting language can, and every PC at least has PowerShell pre-installed. So you could save a script like this, then have N++ run it by pressing
F5
and filling in the box with:powershell %USERPROFILE%\epoch_to_date.ps1 $(CURRENT_WORD) $(FULL_CURRENT_PATH)
where
$(FULL_CURRENT_PATH)
is the active file (must be saved to disk first, notnew #
), and$(CURRENT_WORD)
is a Unix timestamp that’s under the caret:# %UserProfile%\epoch_to_date.ps1 <# .SYNOPSIS Coverts all occurrences of a Unix timestamp to a calendar date in a given file. .PARAMETER UnixEpoch A Unix timestamp. .PARAMETER OutFile Full path to the file to process. .EXAMPLE PS C:\> ./epoch_to_date.ps1 1704393593 C:\file.txt #> param( [Int64]$UnixEpoch = 0L, [string]$OutFile ) $WindowsTime=($UnixEpoch*10000000)+116444736000000000 $Date=[datetime]::FromFileTimeUtc($WindowsTime) (Get-Content -Path $OutFile) -Replace $UnixEpoch.ToString(), $Date | Set-Content -Path $OutFile
-
P.S.
I should have mentioned that — since version 7.7 — reloaded files cannot be restored bySCI_UNDO
(CTRL+Z
,Edit > Undo
, etc.).@Dr-N, you would do better to wait till someone posts the obligatory Python script.
-
@Dr-N said in Replace Unix timestamp:
How can a 13 digit unix timestamp be replaced with a readable time with Find and Replace?
I believe you can do this with the MultiReplace plugin.
This appeared to work when I tried it:
Find what:
(time=)(\d{13})
Replace with:set(CAP1..os.date("%c",CAP2/1000+os.time({year=1970,month=1,day=1})))
but I’ll leave it to @Thomas-Knoefel, the author of the plugin, to clarify the finer points, which I might have all mixed up. The plugin uses Lua expressions; I found the date and time functions described here. It should be possible to format the date/time from the timestamp as desired using the first argument of os.date. The os.time function is there to adjust the epoch from Unix to Windows, and the division by 1000 is because Lua’s time/date functions work in seconds but Unix timestamps are in milliseconds.
-
This PythonScript solution should work fine
''' requires PythonScript: https://github.com/bruderstein/PythonScript/releases docs: https://docs.python.org/3/library/datetime.html ref: https://community.notepad-plus-plus.org/topic/25308/replace-unix-timestamp/5 ''' # needed if using print() in Python 2 from __future__ import print_function import datetime from Npp import editor THING_PRECEDING_TIMESTAMPS = '^time=' # see https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior # for docs on this time format TIME_FORMAT = '%Y-%m-%d %H:%M:%S' PATTERN_TO_REPLACE = r'(%s)(\d{13})' % (THING_PRECEDING_TIMESTAMPS,) def timestamp_str_to_date(timestamp_str, time_format=TIME_FORMAT): '''converts a 13-digit timestamp string into a date string in the desired format (default YYYY-MM-DD hh:mm:ss) ''' timestamp = float(timestamp_str) / 1000 dt = datetime.datetime.fromtimestamp(timestamp) # print(dt) return dt.strftime(time_format) editor.rereplace(PATTERN_TO_REPLACE, lambda m: m.group(1) + timestamp_str_to_date(m.group(2)))
-
-
@Dr-N
@Coises, said in Replace Unix timestamp:I believe you can do this with the MultiReplace plugin.
I did converted the timestamp into a readable format with MultiReplace close to @Coises solution:
Time to be converted: time=1704393593000
Find what:(time=)(\d{13})
Replace with:set(CAP1..os.date("%c", math.floor(CAP2/1000)))
Result is: time=Thu Jan 4 19:39:53 2024Time to be converted: time=1704393593
Find what:(time=)(\d{10})
Replace with:set(CAP1..os.date("%Y-%m-%d %H:%M:%S", CAP2))
Result is: time=2024-01-04 19:39:53You have to enable the Options “Use Variables” and “Regex” in MultiReplace.
-
Hello, @dr-n, @peterjones, @rdipardo, @coises, @mark-olson, @thomas-knoefel and All,
@thomas-knoefel, I think that your result
2024-01-04 19:39:53
exceeds the right answer of one hour !Indeed, for the Unix datestamp
1704393593
, expressed in seconds, the correct result is2024-01-04 18:39:53
Note that it is the @rdipardo result !
You can verify my assumption with the 3 on-line converters, below :
https://www.unixtimestamp.com/
https://www.epochconverter.com/
Remark :
- When using the first two sites, if you do the reverse operation ( Date to Epoch timestamp ), you must use the time indicated in the row
Your time zone
in order to get the correct number1704393593
Best Regards,
guy038
- When using the first two sites, if you do the reverse operation ( Date to Epoch timestamp ), you must use the time indicated in the row
-
@Thomas-Knoefel said in Replace Unix timestamp:
set(CAP1…os.date(“%c”, math.floor(CAP2/1000)))
Thanks Thomas…
Replacing the 13 digit unix timestamp showed this error, see picture.
-
@Dr-N said in Replace Unix timestamp:
Replacing the 13 digit unix timestamp showed this error, see picture.
My example, was designed for two capture groups. In regex, capture groups are enclosed in parentheses. In MultiReplace, they are captured in the numbered CAP variables.
As you have only one capture group in your Find, which holds the timestamp, the second capture group (CAP2) is empty, as indicated by nil.My previous example:
String to search for: time=1704393593000
Find what: (time=)(\d{13})
CAP1 = ‘time=’
CAP2 = 13 digit timestamp
Replace with:set(CAP1..os.date(“%c”, math.floor(CAP2/1000)))
you probably changed it to that:
String to search for: 1704393593000
Find what: (\d{13})
CAP1 = 13 digit timestamp
for that the replace phrase should look like this
Replace with:set(os.date("%c", math.floor(CAP1/1000)))
You can find more details here:
MultiReplace “Use Variables” manualI hope this helps …
-
@Dr-N ,@Coises ,@PeterJones
@guy038 said in Replace Unix timestamp:I think that your result 2024-01-04 19:39:53 exceeds the right answer of one hour !
Thanks for pointing this out!
Indeed, in my example, my local time zone has been automatically included into the calculation.
In case it should be UTC ( which is one hour west of my location) this should be indicated by a “!” in the format string.
So the Replace of my (two) Cature Group example should look like this .Previous example with local time zone (Thu Jan 4 19:39:53 2024):
set(CAP1..os.date("%c", math.floor(CAP2/1000)))
New example with UTC time (Thu Jan 4 18:39:53 2024):
set(CAP1..os.date("!%c", math.floor(CAP2/1000)))
Details are described here:
Lua os.date description