How to manage timezones in a web application? - django

I wan't to manage the different timezones of my users in my web application, but I have no idea where to start. I have to save the local time of each user in my database?, or maybe make the conversion to a UTC time, save it, and then make the conversion again to show it?, or there is another way? For example, if one of my users make an appointment in his local time, I have to convert it to UTC store it in my database, and then when he need it, convert it again to his local time an show it?? By the way, I'm using Django. Thanks.

Store date/time as UTC (Not sure what the Python command for that is)
When outputting dates, wrap them in a helper function that gets the current user's time zone preference, then adjust the time/date for the offset, then output it from the application.
http://docs.python.org/library/time.html
http://docs.python.org/library/datetime.html

Try the Django snippit UTC DateTime field. It has everything you'll need right out of the box, practically.

make the conversion to a UTC time, save it, and then make the conversion again to show it?
Yes, even if you only have one local timezone, you should generally be storing your dates as UTC timestamps in the database, and converting it to and from text in the appropriate timezone in your webapps input and output stages. When you have a per-user timezone stored it's then easy to switch out the default timezone for the customised one.
pytz is the usual solution for selecting and converting timezones. (Although personally I hacked up my own less overwhelming collection of timezones.)

Using a single "database time" timezone is, IMO, the best options because:-
It greatly simplifies routines for dealing with people in different timezones
It allows for easier conversions to other timezones
Anyone seeing the data out of context of the user's view can assume that it's UTC, and not have to guess if "12:45" for one record is "12:45" for another
Standards are good
So, yes. Store the user's timezone in the database, then store all times in a common time (Like UTC). Convert them when the user views anything including time. It's the simplest solution that doesn't just force all users to pretend to be in the same timezone.

Related

Identify PST timezone on non-english Windows installation

I am facing this weird problem where I need to fetch PST timezone information (e.g. MUI_Std, Std, TZI etc.) from Japanese Windows OS machine but unable to find a reliable way to do it.
I have tried RegLoadMUIStringW API. But it returns me name of PST timezone in localized format. I have PST timezone name in English with me. So using output of this API I am not able to compare and tell whether the value being read from registry is for PST timezone.
One possible solution is to have MUI_Std value of PST timezone in code (the value is #tzres.dll,-212) and compare this value with the values read from registry. This way I will come to know if the timezone I am reading is PST or not. But I am not sure if this is a reliable way to detect PST timezone.
Let me know if anyone has any inputs on this.
Time zones are identified by their ID (aka "Key Name"), not their localized names. The IDs are not localized.
Thus, regardless of language of the OS, you'll find the Pacific time zone information at:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\Pacific Standard Time
Of the values within, only the Std, Dlt, and Display values are localized, and the MUI values point to where those strings are located in the resource files. The resource files are installed with the Windows language packs. Note, you shouldn't hardcoded a value (eg, -212) because a future update might provide a new string that could change it.
Also, if you just need to retrieve the time zone information for a given ID, you don't actually need to access the registry at all. Just use the EnumDynamicTimeZoneInformation function to iterate over the time zones looking for the one that matches the desired TimeZoneKeyName (which again is not localized).
If however, you're looking for the localizations of these in languages other than the current OS language, you could try taking a look at the TimeZoneWindowsResourceExtractor project.
I wrote a recent SQL Shack article that shows how to extract time zone / time zone adjustment information directly from a Windows PC registry. The technique shows C# and VB.net desktop apps that build text files with time zone and time zone adjustment information. I placed the code at my GitHub resource, and you can modify it if it does not specifically cover the exact data you need. Mr. Johnson-Pint's earlier content here at SO, and at his blog, really helped me build out my ideas.
HTH!

Should I create a model or just use a string

I'm writing a database (using django) and I can either use a string (max 4 chars) in a few places or I can create a model and reference it in those places. I've looked and I can't find good discussion on the merits of either solution. Because this database is going to be quite large, what solution scales better both in performance and size?
Thanks!
If you just have a string or a few strings which will be constant, just define it in settings.py. And use it like:
from django.conf import settings
print settings.my_string_1
If it is the case, you can save time by avoiding database access.
If you are going to use many strings which may vary over time, or need insertion, update or delete operations frequently, you have to use database to store it. If you already have a database like MySQL or postgres setup in the project, you can use it. If not, it is enough to use sqlite database if the database size is not going to be large enough.

Cycling through URLs to download csv files

I have a list of URLs which will access a webservice. The webservice downloads .csv files. I want to be able to cycle through them using a date field which is in a specific format in the url itself, thereby downloading the data day-by-day. The access seems fairly slow as even a manual url entry can take a couple of minutes to complete, and I suspect the issue is at the webservice' end.
The URL is in the format:
http://web.service.com/ws/XYZ/data/?key=mysecretkeyf&field1=X&start=YYYY-MM-DD 00:00&end= YYYY-MM-DD 00:00&field=Y&format=csv
So the way I envision it (and I am keen to take advice) is using a variable for the start year, month and day fields cycling onto the next URL as the previous .csv is downloaded, with the code ending when the current date is accessed.
Any ideas most welcome.
The coding is really straightforward which makes me wonder if you are looking to code this yourself or asking if there is a service out there that would help do this. If you are coding, I'd choose a language that works well for you. #Vivek mentioned Python which is what I would choose as well.
If you do not want to go the coding approach, you could check out DownThemAll. I have used this utility for batch downloading where you have to increment numbers in parts of the URL. Check it out, it may be a good non-programming solution: DownThemAll

Storing, tracking and updating an SQLite database version in C++ application

I have an application written in C++ which uses an SQLite database to store information. I need a way of assigning a version number to the database. By this I mean, I need to be able to assign a version number to the state of the database, and if a new 'state' (version) is available, then I need to update the current database state to match the state of the updated version.
I am wondering whether it would be good practice to store the information required for this to happen in a table. I would need the version number, and then some way of storing the tables and their columns related to each version number. This would allow me to make comparisons etc.
I realise that this question Set a version to a SQLite database file is related, however it doesn't quite answer my question, as I am unsure if my approach is correct, and if so, how I go about achieving this.
All help is much appreciated.
Use PRAGMA user_version to read and store an integer value in the database file.
When the version in your code and database file are the same, do nothing. When they are different, upgrade/downgrade accordingly and update the version number.

Django textarea for 50,000,000 character data

I've a django application that deals with large text files, up roughly 50,000,000 characters. For a variety of reasons it's desirable to store them as a model field.
We are using sqlite for dev and postgres for production.
Users do not need to enter the data via any UI.
The field does not need to be visible in the admin or elsewhere to the user.
Several questions:
Is it practicable to store this much text in a textarea field?
What, if any, performance issues will this likely create?
Would using a binary field improve performance?
Any guidance would be greatly appreciated.
Another consideration is that when you are querying that model, make sure you use defer on your querysets, so you aren't transferring 50MB of data down the pipe everytime you want to retrieve an object from the db.
I highly recommend storing those files on disk or S3 or equivalent in a FileField though. You won't really be able to query on the contents of those files efficiently.
This is more related to the database you use. You use SQLite so look at the limits of SQLite:
The maximum number of bytes in a string or BLOB in SQLite is defined
by the preprocessor macro SQLITE_MAX_LENGTH. The default value of this
macro is 1 billion (1 thousand million or 1,000,000,000).
http://www.sqlite.org/limits.html
Besides that, it's probably better to use a TextField in Django.
A binary field wouldn't improve performance. Binary fields are meant for binary data, and you are storing text.
After some experimentation we decided to use a Django file field and not store the file contents in Postgresql. Performance was the primary decision driver. With file field we are able to query very quickly to get the underlying field file which in turn can be accessed directly at the OS level with much higher performance than is available if the data is stored in a Postgresql table.
Thanks for the input. It was a big help.