0

I am using stock data from xignite.

I seriously beleive I messed up dates because (unable to say well) its definitely dealing with multiple timezones(due to multiple countries).

How to make timezones well?

For example if someone sees data for 25th May, 2015, it shouldnt be shown as 24th or 26th due to neglecting of timezones.(I am currently not managing timezones).

I get data by csv format, I then use python to extra/modify data and save to db using python(not Django). I dont touch date(other than saving it).

And then I use Django and will use that data from that StockData model.

Adding code in a couple of mins to show data.

StockData model:

#Saved initially using python(not Django)
# but later I might get StockData objects and save them to populate
#some other fields of that table during save (django, not simple python).
date = models.DateTimeField()
country = models.ForeignKey
company = models.ForeignKey
open, close, high ... values

Whenever I want to display I will simply do

StockData.objects.filter(...) #No timezone work as of now

I added some random timezone in settings.py

TIME_ZONE = 'America/New_York' #Dont ask my why NewYork 
#because its just random for now. StockData belongs to some other continent which I cant reveal(let me know if I have to). For now, please assume contient as ABC continent. But that python script is run in US.
user1305989
  • 3,231
  • 3
  • 27
  • 34
  • It's quite a broad question right now. Could you add some specific example? – Rohit Jain Dec 18 '15 at 18:10
  • I would be glad to do that. If I display the stock data in various ways, like print obj.date, print on html page(as table data) and show in chart. The full data should be correct and even 1 day variation due to bad timezone management should be fixed. @RohitJain Is this example enought? If not, please let me know what I have to ? – user1305989 Dec 18 '15 at 18:13
  • Well, when I say example, I mean some code example. In the sense, how you're getting the data, where you're storing it, or rather you're just displaying it. Basic idea is to store dates in UTC timezone, and then do the conversion to respective user timezone for display. For more help, you might have to add concrete example, with actual dates. – Rohit Jain Dec 18 '15 at 18:15
  • Sure, I will provide in 10min. – user1305989 Dec 18 '15 at 18:16
  • @RohitJain Updated question. Please let me know for more details. – user1305989 Dec 18 '15 at 18:37
  • Does the date you receive in CSV contain any timezone information? Can you show a sample date? If it doesn't have any timezone information, then in what timezone do you interpret that? – Rohit Jain Dec 18 '15 at 18:40
  • It has "1/1/2014" under the heading "date" in excel sheet. Doesnt provide any tijmzone details But provides country id, so may I should do that. But I didnt do anything. @RohitJain Thanks for your patience by the way. – user1305989 Dec 18 '15 at 18:57
  • Then I think, you should just have a `DateField` instead of `DateTimeField`. And create a `date` object from that string. No need to attach any timezone here. – Rohit Jain Dec 18 '15 at 18:59
  • Yes, I will make it datefield. Actually I dont have that csv till now. Its run by someone else and I maintain Django. And for charts purpose I need to convert to millisecond format. Any fix needed with this ? – user1305989 Dec 18 '15 at 18:59
  • For all values in csv, just do - `datetime.strptime(date_string, "%d/%m/%Y").date()`, and store in DB. – Rohit Jain Dec 18 '15 at 19:02
  • By `millisecond`, do you mean `millisecond` since `epoch`? – Rohit Jain Dec 18 '15 at 19:04
  • def to_millis(utc_time, epoch=datetime.datetime(1970,1,1)): td = utc_time - epoch assert td.resolution >= datetime.timedelta(microseconds=1) # return td.total_seconds()*1000 analog return (td.microseconds + (td.seconds + td.days * 86400) * 10**6) / 10**3 But planning to update millis to initial python script so I need not update table twice. But not sure if it affects timezone. – user1305989 Dec 18 '15 at 19:06
  • Please suggest best case pythonscript(better to save time) or django and then how to get utc_time(first param in to_millis func) from date field. – user1305989 Dec 18 '15 at 19:07
  • Since you're anyways working with naive datetime object, it should not be an issue here. – Rohit Jain Dec 18 '15 at 19:12
  • Thankyou................@RohitJain – user1305989 Dec 18 '15 at 19:16

0 Answers0