Again, just an example depicting that you may want to store this in your database. Of course, you could normalize this table a bit more and separate timezone information into a separate table, and relate it to users with a foreign key. Similarly, you could have timezone information related to users in your database: id Note that the code above is just an example as to how you might do this. UnknownTimeZoneError : print ( "That was not a valid timezone." ) raise with open ( "user_config.json", "w" ) as config : User_timezone = input ( "Enter your timezone: " ). You may want to save the user's timezone to a configuration file when developing a single-user application.īelow we ask the user for their timezone, check that it is valid using pytz, and then save it to a file. # Saving the user's timezone to a configuration file That's because one timezone can have multiple offsets when Daylight Savings comes into effect or out of effect. Remember when saving a user's timezone you want to save the timezone name, such as US/Eastern or Europe/Madrid instead of the offset. Well, we have to ask them! Usually we'll ask them once and save their timezone into a configuration file or even its own database record so that we don't have to ask them every time. For example, if Daylight Savings came into effect. They ran your application when their timezone was US/Eastern for example, and then they flew to the UK and ran it again. It's consistent to always be working with the same timezone in your database, so your applications can assume everything will be +00:00.The user might give you a date and time when they're in one timezone, but you want to display it in another timezone.I know that you have to convert to and from, and that may seem like a lot of work. Convert the UTC date to their timezone.Get the UTC (+00:00) date from the database.Store it in your database with a +00:00 offset.We just mentioned in the last lecture that as long as you always work with UTC, and convert to the user's local timezone when you are displaying information, it will be relatively simple.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |