Comments (2)
Yeah, we should definitely always use UTC.
A PR is definitely welcome, the question is how to upgrade w/o breaking currently working servers as a side effect ...
from jwcrypto.
Actually, time.time()
is UTC. The confusion most likely stems from comparing timestamps from datetime.datetime.utcnow()
vs datetime.datetime.now()
I'm currently in EDT, so UTC-04.
Running the code below:
import datetime
import time
print("time.time(): ", time.time())
print("datetime.datetime.utcnow(): ", datetime.datetime.utcnow().timestamp())
print("datetime.datetime.now(): ", datetime.datetime.now().timestamp())
print("datetime.datetime.utcnow(): ", datetime.datetime.utcnow())
print("datetime.datetime.now(): ", datetime.datetime.now())
Gives us:
time.time(): 1687834846.4905422
datetime.datetime.utcnow(): 1687849246.490579
datetime.datetime.now(): 1687834846.490584
datetime.datetime.utcnow(): 2023-06-27 03:00:46.490598
datetime.datetime.now(): 2023-06-26 23:00:46.490612
The time.time()
and datetime.datetime.now()
are the same which makes it seem as though time.time()
is giving us local time.
Running the below code instead:
import datetime
import time
print("time.time(): ", time.time())
print("datetime.datetime.utcnow(): ", datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).timestamp())
print("datetime.datetime.now(): ", datetime.datetime.now().replace(tzinfo=datetime.timezone.utc).timestamp())
print("datetime.datetime.utcnow(): ", datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc))
print("datetime.datetime.now(): ", datetime.datetime.now().replace(tzinfo=datetime.timezone.utc))
Gives us:
time.time(): 1687832852.3228416
datetime.datetime.utcnow(): 1687832852.322877
datetime.datetime.now(): 1687818452.322895
datetime.datetime.utcnow(): 2023-06-27 02:27:32.322908+00:00
datetime.datetime.now(): 2023-06-26 22:27:32.322923+00:00
Here we can see that time.time()
and datetime.datetime.utcnow()
are the same which is what we desire.
This happens because the datetime.timestamp()
function, as according to the Python docs, assumes any timezone naive datetime objects are in local time and converts them to UTC, and both datetime.datetime.now()
and datetime.datetime.utcnow()
are timezone naive objects. Hence why we made them timezone aware in the second example.
It's worth noting that in my example, I incorrectly labeled my current local time from datetime.datetime.now()
as UTC to demonstrate the point better, but obviously, if we had the correct timezone on it, then it would give the same timestamp as time.time()
See:
import datetime
import time
from pytz import timezone
print("time.time(): ", time.time())
print("datetime.datetime.utcnow(): ", datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).timestamp())
print("datetime.datetime.now(): ", datetime.datetime.now(timezone('US/Eastern')).timestamp())
print("datetime.datetime.utcnow(): ", datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc))
print("datetime.datetime.now(): ", datetime.datetime.now(timezone('US/Eastern')))
Gives us:
time.time(): 1687836103.5745661
datetime.datetime.utcnow(): 1687836103.574606
datetime.datetime.now(): 1687836103.578064
datetime.datetime.utcnow(): 2023-06-27 03:21:43.578093+00:00
datetime.datetime.now(): 2023-06-26 23:21:43.578112-04:00
from jwcrypto.
Related Issues (20)
- Key size on generation is not always computed correctly
- Migrate from setuptools/tox to hatch HOT 16
- Issue with cryptography dependency declaration HOT 1
- Crytography Vulnerabilties HOT 1
- Protected Header is still readable for JWE HOT 2
- Project Status
- Release the current state HOT 4
- Fix or discontinu ppc64le CI testing HOT 1
- Lack of wheel in distribution causes it to break builds targeting separate platforms HOT 12
- variables naming convention HOT 2
- key_id deprecated. What's the alternative?
- InvalidJWSSignature error always occurs HOT 7
- Shouldn't `jwk.import_from_pyca` also provide a `kid` param and set it to `thumbprint()` by default? HOT 2
- ValueError: wrapper has not been initialized in jwcrypto Library
- Drop Python 3.6 support since 1.5.2 HOT 5
- Dependency to typing_extensions broken HOT 6
- Jwt.JWT allows parsing tokens with json serialization
- Improve ergonomics or document usage to emulate python-jose/PyJWT HOT 13
- Docs on readthedocs are broken since 1.5.1
- Can't sign a JWT with an ed25519 JWK HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jwcrypto.