function now
Returns the current UTC datetime with timezone information.
/tf/active/vicechatdev/rmcl/utils.py
7 - 8
simple
Purpose
This function provides a convenient way to get the current date and time in UTC (Coordinated Universal Time) with timezone awareness. It's useful for timestamping events, logging, or any operation requiring consistent timezone-aware datetime objects. Using UTC ensures consistency across different timezones and avoids ambiguity in datetime comparisons and storage.
Source Code
def now():
return datetime.datetime.now(datetime.timezone.utc)
Return Value
Returns a datetime.datetime object representing the current moment in UTC timezone. The returned object is timezone-aware (has tzinfo set to datetime.timezone.utc), which makes it suitable for timezone-aware datetime operations and comparisons. The datetime includes year, month, day, hour, minute, second, and microsecond precision.
Dependencies
datetime
Required Imports
import datetime
Usage Example
import datetime
def now():
return datetime.datetime.now(datetime.timezone.utc)
# Get current UTC time
current_time = now()
print(f"Current UTC time: {current_time}")
print(f"ISO format: {current_time.isoformat()}")
print(f"Timezone: {current_time.tzinfo}")
# Use for timestamping
event_timestamp = now()
print(f"Event occurred at: {event_timestamp}")
# Compare with another datetime
later_time = now()
time_difference = later_time - event_timestamp
print(f"Time elapsed: {time_difference.total_seconds()} seconds")
Best Practices
- Always use timezone-aware datetimes when working with timestamps across different timezones to avoid ambiguity
- UTC is the recommended timezone for storing timestamps in databases and logs as it avoids daylight saving time complications
- When displaying times to users, convert from UTC to their local timezone rather than storing times in local timezones
- This function returns a new datetime object on each call, so cache the result if you need to use the same timestamp multiple times in a single operation
- For high-precision timing or performance measurements, consider using time.perf_counter() instead
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function format_datetime_v1 60.7% similar
-
function cftime_to_timestamp 49.8% similar
-
function dt_to_int 47.8% similar
-
function parse_datetime_v2 45.1% similar
-
function get_file_times 44.0% similar