Mar-01-2021, 02:30 PM
I have a running code the send to a client current_time
I have notice that it send him time in this way:
I want to send it gmt +2
Mar-01-2021, 03:12 PM
Someone introduced it without thinking about the consequences.
The bad thing with utcnow is, that it returns a datetime object with UTC0 but not timezoneinfo attached to this datetime object. So it's in the context of timezone aware use useless.
If you pass timezone
Since Python 3.9 the module
nilamo likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Mar-01-2021, 03:22 PM
thank you for the explain and example!
|Possibly Related Threads…|
|Need to add hours min and seconds||tester_V||5||775||Jun-02-2020, 05:29 PM|
Last Post: tester_V
But datetime.now is documented as returning 'the current local date and time' (assuming no tx= argument is provided) while datetime.utcnow returns 'the current UTC date and time'. So I would expect the two to provide a similar value only if your system/process local time zone is set to UTC. Python DateTime to Unix timestamp 13 digits. Now, we can see DateTime to Unix timestamp 13 digits in python. In this example, I have imported modules like time and datetime.; The datetime.datetime.now is used to get the present time. The datetime module includes functions and classes for doing date and time parsing, formatting, and arithmetic. Datetime contains functions and classes for working with dates and times, separately and together. Warning: datetime also provides datetime.utcnow , which returns an instance of datetime at the current UTC. However, the Python documentation recommends against using this method because it doesn’t include any time zone information in the resulting instance.
Timestamps as opinionated RFC 3339 (Date and Time on the Internet) formatted strings
Timestamps as RFC 3339 (Date & Time on the Internet) formatted strings with conversion functinonality from other timestamp formats or for timestamps on other timezones. Additionally converts timestamps from datetime objets and other common date utilities. Follow modern practices when developing API interfaces.
The elevator pitch – purpose for developers – the why
NOTE – OPINIONATED SOFTWARE
utcnow is opinionated about the format of string based timestamps. For example, that timestamps as strings should be stored using the same formatting and preferably using the same length, as well as adhering to the current best practices – which for computer-to-computer comms should be by following 'RFC 3339 (Date and Time on the Internet: Timestamps)'.
TIMESTAMPS WILL USE UTC
String based timestamps that are meant for logs, API responses and database records shall always be stored with timezone UTC.
Someone – somewhere:'Why UTC? It's not even a timezone for our markets.'
Devs (and wikipedia):'Coordinated Universal Time or Universal Time Coordinated, UTC for short, is still currently the primary time standard and is not affected by daylight saving time, which is usually not something that servers or software developers would want to work around.'
'It's pretty simple – modern internet applications shouldn't use any other timezone in their databases, logs, API:s or other computer to computer interfaces.'
Good timestamps and UTC – really no wild and crazy opinions. Generailly this lib is just about making it
easier to follow common best practices harder to do something wrong – and that's also why
utcnow doesn't have any configuration options. The library does what it should do – 'shoganai'.
RULES FOR RETURNED TIMESTAMPS
The following ruleset are applied to timestamps returned by
utcnow when requesting a string based format:
- Timestamps follow RFC 3339 (Date and Time on the Internet: Timestamps): https://tools.ietf.org/html/rfc3339.
- Timestamps are converted to UTC timezone which we'll note in the timestamp with the 'Z' syntax instead of the also accepted '+00:00'. 'Z' stands for UTC+0 or 'Zulu time' and refers to the zone description of zero hours.
- Timestamps are expressed as a date-time, including the full date (the 'T' between the date and the time is optional in RFC 3339 (but not in ISO 8601) and usually describes the beginning of the time part.
- Timestamps are 27 characters long in the format: 'YYYY-MM-DDTHH:mm:ss.ffffffZ'. 4 digit year, 2 digit month, 2 digit days. 'T', 2 digit hours, 2 digit minutes, 2 digit seconds, 6 fractional second digits (microseconds -> nanoseconds), followed by the timezone identifier for UTC: 'Z'.
utcnow is defined to return timestamps with 6 fractional second digits, which means timestamps down to the microsecond level. Having a six-digit fraction of a second is currently the most common way that timestamps are shown at this date.
When using a fixed length return value for string based timestamps it'll even make the returned strings comparable to each other.
Where to use this – for what kind of applications or interfaces
Some examples of timestamps where this formatting would be reasonable to use includes, but are not limited to any timestamp that is written to a database / datastore as a string, also when timestamps are used in log output or used within a JSON response for an API such as a REST or GraphQL based API, maybe even using custom DateTime scalars.
If any of this sounds like the use-cases within your domains, try
utcnow out – might do the trick.
If your work require a complex mix and match back and forth using different timezones even within internal applications (which may be true for legacy systems or on purely domestic use-cases), then go for
arrow. Also iterating: Modern internet applications shouldn't use any other timezone than UTC in app to app / computer to computer interfaces.
Note that this library is built with backend developers in mind and while there's a good need for human readability and timestamp conversion into local timezones within a service's user interface, frontend applications, etc. Interfaces where conversion into date and time formats meant for human eyes will obviously also reap the benefits from well defined backends that delivers timestamp values in one standardized format.
Python Datetime Utcnow Seconds
Supported input values for timestamp conversion
This library aims at going for simplicity by being explicit about the choices allowed to make.
utcnow however allows the conversion methods to be called with the following kind of argument values:
- RFC 3339 compliant strings, which at the very least must include the full date, but could omit the time part of a date-time, leaving only the date, or by not including the seconds, microseconds or even laving out the timezone information –
utcnowsupports all of the use-cases of RFC 3339 inputs and then converts the input into an even more complete RFC 3339 timestamp in UTC timezone.
- The most common format for handling dates and datetimes in Python, the builtin
datetime.datetimeobject values (both timezone aware values, as well as values that aren't timezone aware, as for which we'll assume UTC).
- Also supporting object values from other commonly used libraries, such as
- As a bonus – Unix time, mainly for convinience (
time.time()) (we have many names for the things we love: epoch time, posix time, seconds since epoch, 2038-bug on 32-bit unsigned ints to time-travel back to the first radio-transmission across the atlantic, there will be movies about this ).
A neat side-effect of defaulted string output – comparison as strings
If date and time components are ordered from least precise to most precise, then a useful property is achieved. Assuming that the time zones of the dates and times are the same (e.g., all in UTC), expressed using the same string (e.g., all 'Z' or all '+00:00'), and all times have the same number of fractional second digits, then the date and time strings may be sorted as strings and a time-ordered sequence will result. he presence of optional punctuation would violate this characteristic.
Here follows a few examples of the problems with having to work with mismatching timestamps, even though the four example statements all use RFC 3339 compliant values. For example an API is kind enough for users to submit timestamps as long as they're good enough and for where the backend application has to convert inputs to values good for the cause.
Matching two dates of different formats using strings won't go well at all. All of the following four string comparisons would've given an opposite result if compared as actual timestamps instead of as strings, where comparison is just alphabetic.
utcnow on the same set of timestamps, which returns a string value for comparison.
*This shown the returned values from the
utcnow calls, and for what the comparisons is actually evaluated on.
Some additional examples of timestamps and to what they whould be converted. Thre first three examples are from the RFC document.
Python Datetime Utcnow Wrong
Like you would install any other Python package, use
pipenv or your weapon of choice.
Usage and examples
This is not a fullblown date library at all – it's simple and basically it just output timestamps into the fixes length string format
YYYY-MM-DDTHH:mm:ss.uuuuuuZ (or as
%Y-%m-%dT%H:%M:%SZ as if used with
datetime.datetime.strftime). Always uses UTC in output and always appends the UTC timezone as a
Z to the string (instead of using
There's no other external dependencies required. A convenient utility package for when you need to store timestamps in a datastore as a string, adding it to a JSON response or using a shared and common standard in your log outputs.
Wether you choose to use this library or anything else, or just specify this is how we do it in a documement, it'll be worth it. It's never too late to start aligning your formatting standards and interfaces.
Release historyRelease notifications RSS feed
Python Datetime Module
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size utcnow-0.2.2-py3-none-any.whl (10.5 kB)||File type Wheel||Python version py3||Upload date||Hashes|
|Filename, size utcnow-0.2.2.tar.gz (16.5 kB)||File type Source||Python version None||Upload date||Hashes|
Hashes for utcnow-0.2.2-py3-none-any.whl
Hashes for utcnow-0.2.2.tar.gz