Rajendra Dharmkar

Rajendra Dharmkar

160 Articles Published

Articles by Rajendra Dharmkar

Page 6 of 16

How to get computer's UTC offset in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 3K+ Views

The computer's UTC offset is the timezone set on your computer. You can get this timezone information using Python's time module or datetime module. The UTC offset represents the time difference from Coordinated Universal Time (UTC) in seconds. Using time.timezone The time.timezone attribute returns the UTC offset in seconds. Note that it returns a negative value, so we negate it to get the actual offset ? import time # Get UTC offset in seconds (negated because time.timezone is negative) utc_offset = -time.timezone print("UTC offset in seconds:", utc_offset) # Convert to hours hours = utc_offset ...

Read More

How can I apply an offset on the current time in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 5K+ Views

Whenever you want to add or subtract (apply an offset) to a date/time, use a datetime.datetime(), then add or subtract datetime.timedelta() instances. A timedelta object represents a duration, the difference between two dates or times. Syntax The timedelta constructor has the following function signature − datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]) Note − All arguments are optional and default to 0. Arguments may be ints, longs, or floats, and may be positive or negative. Adding Time Offset Here's how to add time to the current datetime ? import datetime ...

Read More

How can we do date and time math in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 4K+ Views

It is very easy to do date and time math in Python using timedelta objects. Whenever you want to add or subtract to a date/time, use a datetime.datetime(), then add or subtract datetime.timedelta() instances. A timedelta object represents a duration, the difference between two dates or times. Syntax The timedelta constructor has the following function signature ? datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]) Note: All arguments are optional and default to 0. Arguments may be integers, longs, or floats, and may be positive or negative. Adding and Subtracting Time An example ...

Read More

How to convert a datetime string to millisecond UNIX time stamp?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ Views

A millisecond UNIX timestamp is a number that shows how many milliseconds have elapsed since the beginning of the Unix epoch, which is January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC) up to the current moment or specified date and time. Instead of counting time in days, hours, or minutes, it counts in milliseconds (1 second = 1000 milliseconds). In Python, the common way to convert a datetime string to a milliseconds timestamp involves using the strptime() function (to parse a string into a datetime object), then converting this datetime object into a UNIX timestamp by using the ...

Read More

Why do I get different timestamps in python on different machines?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 378 Views

A timestamp represents a specific point in time as a numerical value. It typically measures the number of seconds that have elapsed since the Unix epoch, which is January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC). The main reasons behind timestamp variations on different machines include differences in time zones, system clocks, locale settings, and the use of UTC and local time (such as IST, PST, or CET). In this article, we will explore timestamp differences across different machines by considering the following key factors. Different System Time Zones If each system were set to different ...

Read More

How to measure elapsed time in python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ Views

To measure time elapsed during program execution, Python provides several methods. The most common approaches are using time.time(), time.perf_counter(), or the timeit module for benchmarking purposes. Using time.time() The simplest method uses time.time() to capture timestamps before and after code execution − import time t0 = time.time() print("Hello") time.sleep(0.001) # Simulate some work t1 = time.time() - t0 print("Time elapsed:", t1, "seconds") Hello Time elapsed: 0.0015020370483398438 seconds Using time.perf_counter() (Recommended) For more precise measurements, use time.perf_counter() which provides the highest available resolution − import time ...

Read More

How to compare Python string formatting: % with .format?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 305 Views

Python provides two main approaches for string formatting: the older % formatting (printf-style) and the newer .format() method. Understanding their differences helps you choose the right approach for your code. % Formatting Issues The % operator can take either a variable or a tuple, which creates potential confusion. Here's a common pitfall ? my_tuple = (1, 2, 3) try: result = "My tuple: %s" % my_tuple print(result) except TypeError as e: print(f"Error: {e}") Error: not enough arguments for format string ...

Read More

How to get the timing Execution Speed of Python Code?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ Views

Measuring the execution time of Python code is essential for performance optimization and benchmarking. Python provides several built-in modules like time and timeit to accurately measure code execution speed. Using time Module The time.perf_counter() function provides the highest available resolution and is recommended for measuring short durations ? import time t0 = time.perf_counter() print("Hello") t1 = time.perf_counter() print("Time elapsed:", t1 - t0, "seconds") Hello Time elapsed: 2.7499999851558823e-05 seconds Using time.time() For wall-clock time measurement, you can use time.time() ? import time start = time.time() # Simulate ...

Read More

How to find if 24 hrs have passed between datetimes in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 3K+ Views

To determine if 24 hours have passed between two datetimes in Python, you need to calculate the time difference and compare it to a 24-hour duration. The datetime module provides tools to perform this calculation using timedelta objects. Basic Approach Using total_seconds() Subtract two datetime objects to get a timedelta, then use total_seconds() to compare with 24 hours in seconds ⏤ from datetime import datetime NUMBER_OF_SECONDS = 86400 # seconds in 24 hours first = datetime(2017, 10, 10) second = datetime(2017, 10, 12) if abs((second - first).total_seconds()) > NUMBER_OF_SECONDS: ...

Read More

How to convert unix timestamp string to readable date in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 6K+ Views

You can use the fromtimestamp() function from the datetime module to convert a UNIX timestamp string to a readable date format. This function takes the timestamp as input and returns the datetime object corresponding to the timestamp. Converting Unix Timestamp to Readable Date Here's how to convert a Unix timestamp string to a readable date format ? import datetime # Convert timestamp string to readable date timestamp_str = "1500000000" timestamp = datetime.datetime.fromtimestamp(int(timestamp_str)) print(timestamp.strftime('%Y-%m-%d %H:%M:%S')) The output of the above code is ? 2017-07-14 08:10:00 Using Different Date Formats ...

Read More
Showing 51–60 of 160 articles
« Prev 1 4 5 6 7 8 16 Next »
Advertisements