#๐ Python datetime problem
77 messages ยท Page 1 of 1 (latest)
@round wasp
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
I want to add microsecond timings with the datetime module to the senaryoXX sections of the code I wrote, but no matter what I did, I couldn't succeed. Can you help me? Also, if there are any obvious errors in the code, I would be glad if you could help me, thank you.
!e
from datetime import datetime
ct = datetime.now()
print([ct.replace(minute=m, second=s) for m, s in [(5, 12), (5, 54)]]
)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[datetime.datetime(2024, 8, 25, 18, 5, 12, 382811), datetime.datetime(2024, 8, 25, 18, 5, 54, 382811)]
wheres the error this works as intended doesnt it?
also as there is a pattern in ur times u should think to reduce that hardcode part
The code works without any problems.
so in which line u try to add microseconds?
I just want to add milliseconds or microseconds next to the minutes and seconds in the section you showed, but I couldn't do it.
for example
from datetime import datetime
ct = datetime.now()
print([ct.replace(minute=m, second=s, milisecond=ms) for m, s, ms in [(5, 12, 500000), (5, 543, 355555)]]
)
!e
from datetime import datetime
ct = datetime.now()
print(ct.replace(microsecond=1))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
2024-08-25 18:12:27.000001
?
this works like this but when i write in this part the code isn't working
also consider to combine/generalize ur senaryoXX functions and the list of tuple timings
https://paste.pythondiscord.com/GFAA this is example
i changed part of my code like this but it isn't working
if (current_time.hour == target_time.hour and
current_time.minute == target_time.minute and
current_time.second == target_time.second and
current_time.microsecond // 1000 == target_time.microsecond // 1000 and
why u dont compare dt.obj with dt.obj?
For example, the code snippet required for hour (5, 12, 500) is not executed.
what do you want to say
i dont understand
can you explain more explanatory
u comapre all values expect for date (hour, min, sec, msec) why not compare the dt obj themself?
the current time might not be exact, as it might be some microseconds off from the target time, I guess.
mh i see ok
though you could probably just round and replace it as needed, and then just compare them
the date should be the same anyway
my thoughts as it looks ugly and unnecessarily complicated
this should work as 500*1000 is 0,5sec
!e
from datetime import datetime
ct = datetime.now()
new_time_tuple = (0, 1, 500*1000)
print(ct.replace(minute=new_time_tuple[0], second=new_time_tuple[1], microsecond=new_time_tuple[-1]))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
2024-08-25 18:00:01.500000
i agree with you but its not working
I'm making a mistake in some part but I don't know where.
what exactly is that code even supposed to do?
makes the necessary clicks and keystrokes at the specified local times
it works local minutes and seconds
for what purpose?
to automate a task that requires manual effort in a game
so i smell ToS
the problem is not this
5. Do not provide or request help on projects that may violate terms of service, or that may be deemed inappropriate, malicious, or illegal.
this is just a game
I'd assume your time.sleep can cause isses
you jump 500 ms with that, so probably miss a lot of exact ms matches
@short ocean so its cool to help out even tho might violate ToS?
iI tried removing the timesleeps at the end of the senaryo functions but it didn't work
locally running games are not services. Nothing so far was said that this is a live service game 
okay
I definitely get matches with your code. I only reduced the conditions to the datetime parts and removed the "execute_times" stuff. So no idea if that might cause issues. Aside from the time.sleep, I don't really see any issues with your code.
(except of course that nearly all your values stop at around 24 minute, and it's 20:36 for me, so I needed to add some test values)
in terms of performance bottlenecks i would suggest to try numpy instead of datetime for comparisons
i'm testing like this who wants to wait ten hours ๐
thank you for efforts by the way i am grateful
so ur whileloop will be x4 times faster, even increasing with size (exponential scaling)
I will try this in my next project thanks
I'd at least try to add a print into your fi bodies to see if any are true.
It is very interesting that it works for minutes and seconds but does not work when microseconds are involved.
create a elif which excludes the ms match requiered and check the returns
to not let this die as im kinda curious now i think python isnt the best lang to write such code maybe C would be more appropriate
!e
from datetime import datetime
import numpy as np
executed_times = {}
ct = datetime.now()
tt = [ct.replace(minute=m, second=s, microsecond=ms) for m, s, ms in [
(5, 12, 500), (5, 54, 200), (6, 0, 0), (6, 12, 300), (6, 54, 600), (7, 0, 100),
(7, 12, 800), (7, 24, 400), (8, 24, 200), (8, 30, 500), (9, 0, 0), (9, 12, 700),
(9, 42, 500), (9, 54, 300), (10, 0, 0), (10, 42, 800), (10, 54, 100), (11, 30, 600),
(11, 54, 200), (12, 24, 500), (12, 30, 300), (13, 0, 0), (13, 12, 600), (13, 24, 700),
(13, 30, 100), (13, 42, 400), (14, 24, 300), (14, 30, 600), (14, 42, 100),
(15, 54, 500), (16, 0, 0), (16, 12, 200), (17, 0, 700), (17, 24, 500),
(18, 42, 300), (18, 54, 400), (19, 0, 0), (19, 12, 600), (19, 54, 700),
(20, 0, 100), (20, 12, 800), (21, 12, 200), (21, 24, 400), (21, 42, 600),
(22, 24, 300), (22, 30, 500), (22, 42, 700), (23, 42, 800), (23, 54, 200),
(27, 24, 100), (27, 30, 300), (27, 42, 600)]]
np_tt = np.array(tt).astype(np.datetime64)
while len(executed_times.keys()) <= 0:
ct = datetime.now()
ct = ct.replace(microsecond=ct.microsecond // 1000)
# if ct.microsecond % 100 == 0: # maybe check if u can pre-sample to reduce resources
np_ct = np.array(ct).astype(np.datetime64)
if np.array(np_ct).astype(np.datetime64) in np_tt:
executed_times[int(np.where([np_ct == np_tt])[-1][0])] = True
@round wasp think this improves the needed resources let me know how much RAM u used before and now ๐
i'll change my code like this but I still can't figure out the millisecond problem.
the problem was that ur convertion was faulty
atleast i think that was the problem
also see what i marked out could help to pre-sample
note these now arent 1/10 sec they are indeed microsecs, so this runs super fast and efficient while finding matches
iI understand you now, I'll try this in a moment.
from datetime import datetime
import numpy as np
def generate_times(current_time, second_increments, microsecond_increments):
times = []
for second_increment in second_increments:
for microsecond_increment in microsecond_increments:
new_time = current_time.replace(second=second_increment % 60, microsecond=microsecond_increment)
times.append(new_time)
return times
executed_times = {}
ct = datetime.now()
tt = generate_times(ct, [0, 10, 20, 30, 40, 50], [0, 500])
np_tt = np.array(tt).astype(np.datetime64)
print(len(np_tt))
mask = np_tt > np.datetime64(ct)
np_tt = np_tt[mask]
print(len(np_tt))
while len(executed_times.keys()) != len(np_tt):
ct = datetime.now()
ct = ct.replace(microsecond=ct.microsecond // 1000)
# if ct.microsecond % 100 == 0: # maybe check if u can pre-sample to reduce resources
np_ct = np.array(ct).astype(np.datetime64)
if np.array(np_ct).astype(np.datetime64) in np_tt:
idx = int(np.where([np_ct == np_tt])[-1][0])
if idx in executed_times.keys():
continue
else:
executed_times[idx] = True
ontop of checking if matches are even possible u should adjust so the code resets after each minute or so, if the script misses one value ull run eternally
@round wasp still waiting on ur resource reply ๐
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.