I have this code below:
interval_time=int(input('how many seconds each interval?'))
print(time.strftime("%H:%M"))
while time.strftime("%H:%M")!=str('19:45'):
time.sleep(interval_time)
winsound.PlaySound("SystemExit",winsound.SND_ALIAS)
winsound.PlaySound("SystemEnter",winsound.SND_ALIAS)
The thing is, I know how to get current time. I want it to look something like:
while *script_first_ran_time*!=*script_first_ran_time+30minutes*
I know how to convert the minutes, but it keeps re-taking the current time and adding 30 minutes to it. How do I make the initial time.strf() remain constant?
Thanks a lot!
You've decided to make this task harder by using string representation of times. As these strings have no concept of being dates, performing date-related operations on them is troublesome. As an alternative, you should use dedicated library designed to work with dates and times, namely datetime.
Boilerplate code would look like this:
from datetime import datetime, timedelta
thirty_minutes_later = datetime.now() + timedelta(minutes=30)
while datetime.now() < thirty_minutes_later:
pass # do something
Here, datetime.now returns special objects that represents current time. timedelta returns special objects that, when added or subtracted from datetime instance, returns another datetime, shifted in time according to timedelta's state.
Also, notice that I've changed your condition from not equal to bigger than - it'll stop processing even if you won't hit your time spot precisely (and with millisecond resolution, you'd have to be extremely lucky to hit that).
Related
I am trying hard to get Unix Timestamp for current time in ADF. Basically I need number of number of milliseconds (ms) since epoch. I tried dabbling with built-in ticks function in ADF but it's not what I need. Also the documentation on the ticks is not really clear. It says number of ticks since specified timestamp is what the function returns. 1 tick = 100 nanoseconds & 1000000 nanoseconds = 1 ms. So considering this, I used the following expression in set variable activity:-
#{ div(ticks('1970-01-01T00:00:00.0000000Z'),10000) }
So the expectation is that whenever I run it, it should give me number of milliseconds since the epoch ( up to the moment of execution ) -- so by this definition every time I run it, it should return me a different value. But it returns a fixed value 62135568000000 every time I run it. So either the documentation is not correct or it's not really calculating what I really need.
Function ticks() return the number of ticks from '0001-01-01T00:00:00.0000000Z' to parameter of ticks(), not from '1970-01-01T00:00:00.0000000Z'.This is why you always get a fixed value 62135568000000.
I have tried #{ticks('0001-01-01T00:00:00.0000000Z')}, result is 0.
So if you want to get ms from '1970-01-01T00:00:00.0000000Z' to current time, you can try this:#{div(sub(ticks(utcnow()),ticks('1970-01-01T00:00:00.0000000Z')),10000)}.
I'm trying to create a program that will initiate a few other functions at a certain second tonight, for new years, but I can't find any answers that work in Python 3. Does anyone have an answer to this? It happens to be 11:58:43 that needs to be executed on.
You can use the datetime module in python to get the present date and time in any required format. Then you can easily implement what you are looking for provided , your python program keeps running checking for this time to be reached.
For this checking loop , it will check time for each second if you put a delay in your checking function as 1 second .
import time
time.sleep(1) # sleeps for 1 second.
I suggest a better method for your purpose .
On windows :- Use the inbuilt task scheduler to run your python program.
On Linux :- Take a look at 'cron' jobs to do any tasks at any specified time pattern . Its very easy to implement.
Thanks to the help of our above friend Natesh bhat, (who will keep his correct answer) I made this script:
import time
time.strftime("%H:%M:%S")
currenttime = str(time.strftime("%H:%M:%S"))
print(currenttime)
#Actual Start Time
timetogo = "23:58:43"
#Time for testing (uncomment)
#timetogo = "18:00:00"
while True:
currenttime = str(time.strftime("%H:%M:%S"))
if currenttime == timetogo:
your_function_here()
Feel free to use it for yourself.
What is the starting time for process.hrtime (node.js version of microTime)? I couldn't find any event that starts it. Is the timestamp of the start saved anywhere?
I need my server to be able to get latency to client both ways and for that, I need reliable microtime measure. (+ for some other things)
The starting time is arbitrary; its actual value alone is meaningless. Call hrtime when you want to start the stopwatch, and call it again when your operation is done. Subtract the former from the latter and you have the elapsed time.
process.hrtime()
Returns the current high-resolution real time in a
[seconds, nanoseconds] tuple Array. It is relative to an arbitrary
time in the past. It is not related to the time of day and therefore
not subject to clock drift. The primary use is for measuring
performance between intervals.
You may pass in the result of a previous call to process.hrtime() to
get a diff reading, useful for benchmarks and measuring intervals:
I am writing an app and need to do something functionally similar to what url shortening websites do. I will be generating 6 character (case insensitive alphanumeric) random strings which would identify their longer versions of the link. This leads to 2176782336 possibilities ((10+26)^6). While assigning these strings, there are two approaches I can think about.
Approach 1: the system generates a random string at the runtime and checks for it uniqueness in the system, if it is not unique it tries again. and finally reaches a unique string somehow. But it might create issues if the user is "unlucky" maybe.
Approach 2: I generate a pool of some possible values and assign them as soon as they are needed, this however would make sure the user is always allocated a unique string almost instantly, while this could at the same time also mean, I would have to do plenty of computation in crons beforehand and will increase over the period of time.
While I already have the code to generate such values, a help on approach might be insightful as I am looking forward to a highly accelerated app experience. I could not find any comparative study on this.
Cheers!
What I do in similar situations is to keep N values queued up so that I can instantly assign them, and then when the queue's size falls below a certain threshold (say, .2 * N) I have a background task add another N items to the queue. It probably makes sense to start this background task as soon as your program starts (as opposed to generating the first N values offline and then loading them at startup), operating on the assumption that there will be some delay between startup and requests for values from the queue.
I have some metrics (like counts of logged in users, or SQL queries, or whatever), and I want to gather some time-dependent stats on a regular basis.
For example I want to know how many users were registered in some particular year, month, week, day or even hour.
I thought maybe Redis can be a good fit. But I can't imagine a good strategy for storing such stats. The only idea I have is to store independent counters for days, weeks, etc, and bump them all at once.
How do you do it? I need a good trick. Or maybe Redis isn't any good for my task.
If all you need is a count for each period, the multiple counter approach you suggest is a good one. Incrementing 5 counters in a single pipelined transaction is O(1), while set operations are O(log n + m) with potentially large values of n/m.
The set solution Frank suggested does have its place - I use something similar where I need to know which actions happened rather than just how many. Obviously storing details of each action takes more memory than the counters, but with the amount of RAM typically available these days you can store millions of records before that becomes a problem.
I would just use a sorted sets where the score is the timestamp in seconds since the epoch (unix time). Say you have a sorted set of logins and you want to see how many logins occured in the year 2010, just convert 20101231 23:59:59 and 20100101 00:00:00 to seconds and use those are the max and min arguments to zcount.
The obviously difficulty here is handling the time conversion yourself, but its actually very easy because it the standard Unix format. You can use the date command with %S (on linux at least) or use the system calls time(), localtime() and mktime(), as well as any of the myriad ways available within specific languages that are built on top of these system calls.
I am sure there is some equivalent paradigm in Windows, but that I don't have any experience there.