Measuring elapsed time with the Time module

start_time = time.time() # your code elapsed_time = time.time() – start_time You can also write simple decorator to simplify measurement of execution time of various functions: import time from functools import wraps PROF_DATA = {} def profile(fn): @wraps(fn) def with_profiling(*args, **kwargs): start_time = time.time() ret = fn(*args, **kwargs) elapsed_time = time.time() – start_time if fn.__name__ … Read more

Get first and last date of current month with JavaScript or jQuery [duplicate]

Very simple, no library required: var date = new Date(); var firstDay = new Date(date.getFullYear(), date.getMonth(), 1); var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0); or you might prefer: var date = new Date(), y = date.getFullYear(), m = date.getMonth(); var firstDay = new Date(y, m, 1); var lastDay = new Date(y, m + … Read more

JavaScript seconds to time string with format hh:mm:ss

String.prototype.toHHMMSS = function () { var sec_num = parseInt(this, 10); // don’t forget the second param var hours = Math.floor(sec_num / 3600); var minutes = Math.floor((sec_num – (hours * 3600)) / 60); var seconds = sec_num – (hours * 3600) – (minutes * 60); if (hours < 10) {hours = “0”+hours;} if (minutes < 10) … Read more

Command to get time in milliseconds

date +”%T.%N” returns the current time with nanoseconds. 06:46:41.431857000 date +”%T.%6N” returns the current time with nanoseconds rounded to the first 6 digits, which is microseconds. 06:47:07.183172 date +”%T.%3N” returns the current time with nanoseconds rounded to the first 3 digits, which is milliseconds. 06:47:42.773 In general, every field of the date command’s format can … Read more

time.sleep — sleeps thread or process?

It blocks the thread. If you look in Modules/timemodule.c in the Python source, you’ll see that in the call to floatsleep(), the substantive part of the sleep operation is wrapped in a Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS block, allowing other threads to continue to execute while the current one sleeps. You can also test this with a … Read more

How to change time in DateTime?

You can’t change a DateTime value – it’s immutable. However, you can change the variable to have a new value. The easiest way of doing that to change just the time is to create a TimeSpan with the relevant time, and use the DateTime.Date property: DateTime s = …; TimeSpan ts = new TimeSpan(10, 30, … Read more