No video

A Deep Dive Into Date And Time In Python

  Рет қаралды 41,245

ArjanCodes

ArjanCodes

Күн бұрын

Пікірлер: 108
@ArjanCodes
@ArjanCodes 10 ай бұрын
👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis
@iChrisBirch
@iChrisBirch Жыл бұрын
I want to mention that python has an added standard library module `zoneinfo` since 3.9 that can eliminate external dependencies for timezone handling in a lot of cases.
@ArjanCodes
@ArjanCodes Жыл бұрын
Good point, thanks for mentioning that!
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
Just make sure your zoneinfo is up to date. Changes happen several times a year.
@handle1138
@handle1138 Жыл бұрын
If you happen to not be running the > 3.9 version of python, when Running normal Cpython the time.localtime() function will also return the timezone information using the localtime() C library of the same name for the machine it is running on. This has been available for years without installing any additional packages to vanilla python. However you would have to DIY that information into your own Datetime aware object from the datetime module. You would also have to trust the computer has its own clock set correctly.
@MutleyXIII
@MutleyXIII Жыл бұрын
Thanks for the video, Arjan. By the way, I love this 'series' you going through the standard library. Though I am already aware of their features, there is always something to learn or to get used to. By the way, didn't know you normally record your videos a month before you launch them here :) Side note: I didn't found the link you mentioned at 7:30;
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks Mauricio! It's not always a month before, but I do follow a pretty strict production schedule at the moment since there are more people involved now in the team. Sorry about the missing link - it's in the description of the video.
@munteanionut3993
@munteanionut3993 Жыл бұрын
More people? CoooL! You launching off Arjan! Actually you were already! Hard work pays off
@gargolito
@gargolito Жыл бұрын
Hi Arjan, I've learned a lot from your videos. Was wondering if you have tried using "#%%" in vscode on your videos so you don't have to change to terminal to show code output? For those who may not know. Once you have the Python extension installed in VSCode, if you add "#%%" on top a code block, you'll be able to either click a button to run that code block, or all blocks below it. The output is shown in a new tab on the right by default. For example: #%% print("Hi Friend")
Жыл бұрын
Quote from PostgreSQL docs on date/time: The first century starts at 0001-01-01 00:00:00 AD, although they did not know it at the time. This definition applies to all Gregorian calendar countries. There is no century number 0, you go from -1 century to 1 century. If you disagree with this, please write your complaint to: Pope, Cathedral Saint-Peter of Roma, Vatican.
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
If they didn’t know it at the time, is there any record of it actually happening?
@saurabh2mishra
@saurabh2mishra Жыл бұрын
Hey Arjan, thank you very much for your video. I'm following you from the very beginning. There are very few channels who are worth mentioning including yourself and @MathByte Academy. Kudos to you for making us a better programmer and learner every week. Keep doing great work !
@havenisse2009
@havenisse2009 Жыл бұрын
This is interesting, and somewhat of a rabbit hole. I work in the industrial field (product coding). Dealing with dates there is always a challenge, since companies invent new date coding demands all the time. Commonly "Julian" dates are used, so something along "2391349" could mean "day 239 13:49". And there are special needs for encrypting days, dealing with offsets (around jan/feb/mar!), etc etc. Btw, you don't talk too much about the process of going from string->datetime, dealing with dates in random formats. This could be a topic for another video, assuming you have dates as string "23/10/22" or "20221023" or "23. oktober '22", and having to convert to a datetime. This is often asked in fora. Thank you for great content.
@Mr1995Musicman
@Mr1995Musicman Жыл бұрын
Try the dateutil library. It tries to do this and actually does decently well. Way better than you could probably do fron scratch, but obviously not 100%. It's also probably not gonna handle super specialized datetime encodings either, but it's a really good starting point.
@NateROCKS112
@NateROCKS112 Жыл бұрын
4:30 pretty sure UTC follows leap seconds, meaning that some days can have more seconds than others.
@MessLeadingProgramming
@MessLeadingProgramming Жыл бұрын
I have a recent fight with timezones. Not fun. You think all timezones are utc +- integer value? Wrong. Nepal has +45 minutes because of mountain alignment I believe. You think every country has one timezone? Wrong! You think every US state follows daylight saving consistently? Wrong! You think every city has one timezone? Wrong! There was an amazing article about misconceptions about timezones, need to find it and link here…
@Megaman3451
@Megaman3451 Жыл бұрын
Tom Scott did a great video on the issue on the computerphile channel kzbin.info/www/bejne/Y2baoaBjnMqmhbs
@jambosuss
@jambosuss Жыл бұрын
Tom scott also did a video on the nightmare which is timezones
@RoamingAdhocrat
@RoamingAdhocrat Жыл бұрын
similar with the many, many assumptions one can make about people's names. "A person's name will be at least two characters long."
@jambosuss
@jambosuss Жыл бұрын
@@RoamingAdhocrat Yeah, when 'O' is a common korean name
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
This is why we rely on the Olson zoneinfo database to manage all those headaches for us. Use standard library routines and let them worry about it.
@JGnLAU8OAWF6
@JGnLAU8OAWF6 Жыл бұрын
Use built-in zoneinfo (Python 3.9+) instead of pytz.
@hudabdulwahab2499
@hudabdulwahab2499 Жыл бұрын
Nice Arjan, now I know, when to take out my broetchen in NL time. But seriously, such a good tool. I didnt catch this feature here, but for those who want to use datetime in a for loop over a period between two dates with a specific interval e.g. every 2 days or every 13h37mins - looping through this using pendulum is a breeze!
@janHodle
@janHodle Жыл бұрын
Really one of the best contents on KZbin, in general. Your videos help me to become a better developer!
@shashishekhar----
@shashishekhar---- Жыл бұрын
Arjan is the big brother/ Mentor that we all needed not sure if we deserved tho.
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks Shashi. Glad the content is helpful.
@Tweakimp
@Tweakimp Жыл бұрын
There is also the freezgun package to help with datetime sensitive tests
@solidaeon
@solidaeon Жыл бұрын
Thanks for this very informative video, I am currently working on a date related project this was a huge life saver.
@ArjanCodes
@ArjanCodes Жыл бұрын
You're welcome!
@thomasjohanns7661
@thomasjohanns7661 Жыл бұрын
I thought: "what could I possibly learn about datetime in Python, but I'll watch the video anyway".... and I'm glad I did :) pendulum looks really nice and I only used it to generate timezones
@priyabratapanda1216
@priyabratapanda1216 Жыл бұрын
Thanks for this Videos! I was facing the timezone conversion problems earlier. This should have come bit earlier😂😂 which would have saved me a lot of time in past. Anyways its never too late. Please keep uploading more videos like this
@thichuyen1597
@thichuyen1597 Жыл бұрын
Worked smoothly, tysm
@cybetica
@cybetica Жыл бұрын
At first sight, I'm assuming pendulum's 'en' locale is short for 'English/England', based on the use of abbreviations of 'it' & 'nl' in your examples, but in fact the locale is 'American (English)', since we (English/British) also write the day before the month, and generally will use the 24hr clock. I hope they sort this out in later versions.
@chris0628
@chris0628 Жыл бұрын
Awesome content, to the point with no fluff 👍🏽
@digiryde
@digiryde Жыл бұрын
Yeah. I learned Zulu time in the military. I learned to use it for all coding to avoid all of the silly date/time issues you run into. Now, if we can just have a standard neutral data type for all types of data....
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
12:51 Yeah, but _starting_ daylight saving is the easy transition to handle; what about _ending_ daylight saving, when 3 a.m. occurs twice?
@Medan1993
@Medan1993 Жыл бұрын
datetime as a class is veery old thing, going back to Python 2.3 era where standarization wasn't as strictly enforced, hence we have this lowercase class. I guess it wasn't changed with python2 -> 3 transition due to sheer number of uses of it, so we are kinda stuck with it.
@spaghettiking653
@spaghettiking653 Жыл бұрын
I guess this is the same reason why methods in the unittest module are named in camelCase instead of snake_case like we're used to these days.
@undergroundbodega9766
@undergroundbodega9766 Жыл бұрын
Great vids!!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks!
@Phaust94
@Phaust94 Жыл бұрын
Great video! Thanks Arjan!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you, glad you liked the video!
@pacersgo
@pacersgo Жыл бұрын
Interestingly the pytz has the timezone of Asia/Chongqing but not Aisa/Beijing
@miguelvasquez9849
@miguelvasquez9849 Жыл бұрын
Nice! If you prefer a native package, last version of python have its own timezone package.
@handle1138
@handle1138 Жыл бұрын
If you happen to not be running the latest version of python, when Running normal Cpython the time.localtime() function will also return the timezone information using the localtime() C library of the same name for the machine it is running on. This has been available for years without installing any additional packages to vanilla python. However you would have to DIY that information into your own Datetime aware object from the datetime module. You would also have to trust the computer has its own clock set correctly.
@ChongFrisbee
@ChongFrisbee Жыл бұрын
I agree that Datetime is better for the class. I use Databricks a lot and have bumped into bugs that should not exist in python where different parts of code expected different meanings for datetime (import datetime and from datetime import datetima were both present in the code) But my main reasoning is still for convention consistency. I have the same opinion for functools.Partial, for example. It was eye opening (usefull) finding out that is a class.
@adityapal7764
@adityapal7764 Жыл бұрын
Am I the only one who noticed "adjusting for time traveling with Tacheon Particles" (The flash) reference?
@leftblank5315
@leftblank5315 Жыл бұрын
3.9 has ZoneInfo now, however if you're on windows you need to provision the timezones database, which is tzdata on pypi. So pytz is pretty much here to stay for a while...
@JGnLAU8OAWF6
@JGnLAU8OAWF6 Жыл бұрын
Why does it need to stay? It's external dependency anyway, tzdata or pytz. And zoneinfo is arguably better than pytz.
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
If you avoid deploying mission-critical systems on Windows, that reduces your embarrassment exposure.
@leftblank5315
@leftblank5315 Жыл бұрын
@@JGnLAU8OAWF6 it doesn't need to stay. What I'm saying is all packages have been using pytz for years. When you build your project you may as well keep using pytz because it's already installed. Secondly I don't recall fucking around with tzdata when I write on windows for pytz. It just works when you install it, unlike zoneinfo. At least that's my experience.
@leftblank5315
@leftblank5315 Жыл бұрын
@@lawrencedoliveiro9104 some people develop on windows and need seamless experience on multiple platforms. Magic is a prime example, you have to jump through hoops to get libmagic on windows.
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
@@leftblank5315 Precisely. If you are after a “seamless” experience, then you have to avoid Windows.
@StephenRayner
@StephenRayner Жыл бұрын
Excellent video
@ArjanCodes
@ArjanCodes Жыл бұрын
Thank you very much Stephen, happy you’re enjoying the content!
@ramimashalfontenla1312
@ramimashalfontenla1312 Жыл бұрын
As always, really useful video!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks Rami, happy you’re enjoying the content! :)
@0xDEAD_Inside
@0xDEAD_Inside Жыл бұрын
I recommend dateutil package to everyone who is working with dates. Makes life easy!
@ArjanCodes
@ArjanCodes Жыл бұрын
Thanks for the suggestion - I'll take a look!
@94Schuck
@94Schuck Жыл бұрын
Is the Medium link he mentioned about the history of timezone database missing in the description?
@ArjanCodes
@ArjanCodes Жыл бұрын
Apologies - it's in the video description now.
@94Schuck
@94Schuck Жыл бұрын
@@ArjanCodes Thanks👏👏
@astronemir
@astronemir Жыл бұрын
I use astropy.Time for parsing or working with times.
@RoamingAdhocrat
@RoamingAdhocrat Жыл бұрын
Recording on Sept 16? Greetings from the future, Arjan! We have flying skateboards now! I think you can get VSCode to only lint when you save, which can cut down further on the red squigglies when you've paused to explain a point.
@ArielVolovik
@ArielVolovik Жыл бұрын
You mentioned an article in the description at 7:40 - I can't seem to find it
@ArjanCodes
@ArjanCodes Жыл бұрын
Apologies - it's in the video description now.
@samarbid13
@samarbid13 Жыл бұрын
Small tip: for these type of examples with one file, you can use the new Jupyter Notebook plugin for Vscode, it will be more convenient for you: #%% your code execute with ctr enter
@digiryde
@digiryde Жыл бұрын
Pendulum links: pypi.org/project/pendulum/ github.com/sdispater/pendulum pendulum.eustace.io/ Homepage Looks there is some development, but not much, probably just maintenance. The sdispater account, which owns the project, is active in the issues answering questions.
@vijaybabaria3253
@vijaybabaria3253 Жыл бұрын
@arjancodes - love your channel. May we suggest topic for future video pls. "available options for a good async job scheduler in python on windows or cross platform similar to cron jobs on linux." thank you for all your hard work.
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
Just use cron (or systemd timers) on Linux.
@Rebeljah
@Rebeljah Жыл бұрын
thank you for the video!
@ArjanCodes
@ArjanCodes Жыл бұрын
You're Welcome and Thank you Rebijah, glad you liked the video!
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
9:43 Still, it could be worse: it could be Java. I was doing some date/time conversions in an Android app some years ago, and it took me a while to realize that while every single method in a class might be deprecated, the class itself was still required elsewhere!
@spaghettiking653
@spaghettiking653 Жыл бұрын
Every single method!!!!
@t67m
@t67m Жыл бұрын
Sadly a UTC day is not always 24 hours (86400 seconds) long. Leap seconds exist in UTC resulting in occasional days which are 86401 seconds, or, theoretically, 86399 seconds long. It is a shame that datetime doesn't support TAI time 😒
@ArjanCodes
@ArjanCodes Жыл бұрын
You could argue that a UTC day is actually always 24 hours, but one of the 24 hours is 1 second more or less in very rare circumstances :). But still, that's one more thing that makes dealing with dates and time a challenge.
@VojtechMach
@VojtechMach Жыл бұрын
Is the unix time actually stored in signed int instead of unsigned int? It doesnt make any sense, does it?
@AndreaDalseno
@AndreaDalseno Жыл бұрын
Great video, as usual. What about a tutorial on VIM? You mentioned a plugin for VsCode in a previous video, but I can't find a specific video for VIM shortcuts.
@ArjanCodes
@ArjanCodes Жыл бұрын
Great suggestion Andrea, thank you!
@LionelLomberget
@LionelLomberget Жыл бұрын
J'ai apprécié la prononciation en Français! Et le reste aussi
@ArjanCodes
@ArjanCodes Жыл бұрын
Merci Lionel!
@ratulr
@ratulr Жыл бұрын
ohmygod, thank you so much for this!
@ArjanCodes
@ArjanCodes Жыл бұрын
You're Welcome Ratul. Thanks so much, glad you liked it!
@JTYLER41
@JTYLER41 Жыл бұрын
Love the video. It is however Chronoton particles that are used for time travel. Tachyons are more cloaking devices!
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
Travelling faster than light inevitably leads to time travel.
@kayakMike1000
@kayakMike1000 Жыл бұрын
All dates everywhere must be ISO 8601!
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
16:25 Actually days of the week should numbered from 0, aren’t they?
@unusedTV
@unusedTV Жыл бұрын
7:15 where is the promised link to the article?
@ArjanCodes
@ArjanCodes Жыл бұрын
Apologies - it's in the video description now.
@mesaber86
@mesaber86 Жыл бұрын
Hahaha i like the grin you made after pronouncing that sbfkeldkgnkwkdll 😂 4:25
@ArjanCodes
@ArjanCodes Жыл бұрын
Hahaha. Thank you, glad you liked the video!
@mesaber86
@mesaber86 Жыл бұрын
@@ArjanCodes always do... keep it up master Arjan.
@edip_c
@edip_c 6 ай бұрын
🙇‍♂Took the "time" to hit the like! :)
@ArjanCodes
@ArjanCodes 6 ай бұрын
Ahaha, thank you so much!
@allo5668
@allo5668 Жыл бұрын
In a demo video like this, the red squiggles are really distracting. Consider disabling them when appropriate
@Mr1995Musicman
@Mr1995Musicman Жыл бұрын
Quick note: "import datetime as dt" is the classic way to handle the datetime.datetime probelm. Not making a value judgement one way or the other, but for newcomers to the language this is a common idiom you'll see.
@bocckoka
@bocckoka Жыл бұрын
when did Arjan become so funny?
@salec7592
@salec7592 Жыл бұрын
5:28 six PM, not six AM 6:05 seconds, not milliseconds
@italobuitron1165
@italobuitron1165 Жыл бұрын
Like if want to Uncle Bob tell the story about the california time guy.
@Mr1995Musicman
@Mr1995Musicman Жыл бұрын
Small pronunciation tidbit: at around 5:00 you pronounced "alias" very strangely. Normally, it's pronounced AY-lee-us, whereas yours sounded more like uh-LIE-us. Edit: better timestamp and pronunciation
@TomTrval
@TomTrval Жыл бұрын
Personally I prefer an arrow python package :) I really like the arrow.get method :D
@gshan994
@gshan994 Жыл бұрын
I jus do datetime.now() I get int timestamp. Use it for hash key in Dynamodb for events. 😂
@3sawy20
@3sawy20 Жыл бұрын
pytz.exceptions.UnknownTimeZoneError: 'America/New York '
You Are Going To Like These New Features In Python 3.11
10:58
ArjanCodes
Рет қаралды 160 М.
F-strings In Python: Everything You Need To Know
23:29
ArjanCodes
Рет қаралды 49 М.
SPILLED CHOCKY MILK PRANK ON BROTHER 😂 #shorts
00:12
Savage Vlogs
Рет қаралды 46 МЛН
WHO CAN RUN FASTER?
00:23
Zhong
Рет қаралды 42 МЛН
Refactoring Conway's Game of Life | ArjanCodes Code Roast
31:49
ArjanCodes
Рет қаралды 28 М.
Pandas Datetime Tutorial - Working with Date and Time in Pandas
49:30
A Deep Dive Into Iterators and Itertools in Python
21:01
ArjanCodes
Рет қаралды 60 М.
The Ultimate Guide to Writing Functions
24:31
ArjanCodes
Рет қаралды 180 М.
The Ultimate Guide to Writing Classes in Python
25:39
ArjanCodes
Рет қаралды 110 М.
5 Tips To Achieve Low Coupling In Your Python Code
18:30
ArjanCodes
Рет қаралды 95 М.
The Windows Source Code Revealed: Task Manager (E01)
27:11
Dave's Garage
Рет қаралды 450 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 382 М.
Modern Python logging
21:32
mCoding
Рет қаралды 177 М.
Protocol Or ABC In Python - When to Use Which One?
23:45
ArjanCodes
Рет қаралды 200 М.