-
Notifications
You must be signed in to change notification settings - Fork 429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use UTC datetimes everywhere #69
base: master
Are you sure you want to change the base?
Conversation
👏 thanks! i want to
before merging. |
Sure, let me know if anything goes wrong. |
Maybe I can open a PR with the linting, get it merged and then rebase my branch on master so that only the relevant changes are left in this PR? That would make it easier to review. |
I still see a little bit of timezone mutation on this branch
|
While I agree that storage in UTC is the right way to go, I'd really like to be able to display data in my local timezone. This prevents me from having to do timezone math in my head everytime I look at some data. Any thoughts on the best way to do that? |
@rubik merged your previous PR. |
Is it worth writing a data migration for this? I know there's 5-10 others from slack that are running the repo, so I know it's not just me that has old data in the DB. |
Fair enough, I don't think there's a good solution to this. I'll just revert the |
for b in Balance.objects.filter(date_str='0'): | ||
# django timezone stuff , FML | ||
b.date_str = datetime.datetime.strftime(b.created_on - datetime.timedelta(hours=int(7)),'%Y-%m-%d %H:%M') | ||
for b in Balance.objects.filter(date_str=''): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is date_str=''
correct or was it correct before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes you are correct https://github.com/owocki/pytrader/blob/master/history/models.py#L207
I suggest you test locally, since now I'm not able to. If everything works, I think it's simpler to merge this one after #72. |
this will need a rebase before merging. |
@@ -564,7 +567,8 @@ def profit_view(request): | |||
# get data | |||
data = {} | |||
for t in Trade.objects.filter(symbol=symbol, status='fill').order_by('-created_on').all(): | |||
date = datetime.datetime.strftime(t.created_on-datetime.timedelta(hours=7), '%Y-%m-%d') | |||
mst_dt = timezone.localtime(t.created_on, pytz.timezone('MST')) | |||
date = datetime.datetime.strftime(mst_dt, '%Y-%m-%d') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had forgotten this one. It should be MST since it's in a view.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MST was just what i used because thats where i live. might be worth making this a settings.DISPLAY_TIMEONE
for others to configure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then DBs would be inconsistent between each other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's best to leave it MST and look for a django-chartit alternative.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then DBs would be inconsistent between each other.
could be solved with a migration
Maybe it's best to leave it MST and look for a django-chartit alternative.
i dont feel strongly about this enoguh to hold up the PR. just a suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be solved with a migration
Well in that case we would have to store the data in UTC, but at that point it would be useless. We could remove created_on_str
and add an attribute on the fly. Or use a Python property. Is it possible? Currently I don't have data to create a chart and check myself. I only have the seed from #2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, django-chartit requires the data to be available within the queryset, so a python property will likely not work.
data is posted @ #2 (comment) not asking that you make the change, just telling you about this because now that you've made a PR you should at least have access to the data so you can trade :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this line be:
dt = timezone.localtime(t.created_on, pytz.timezone(settings.DISPLAY_TZ))
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I think it should be
There you go. It should be good now. |
symbol=symbol, created_on__gte=start_time).filter(status__in=['fill', 'open', 'error']).order_by('id') | ||
else: | ||
trades = Trade.objects.exclude(created_on_str="").filter( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment below about created_on_str=''
.
From my experiments, This PR needs #74. |
I'm good with merge here. @igorpejic @Snipa22 @t0mk ? |
@@ -35,6 +35,4 @@ def handle(self, *args, **options): | |||
d.status = status | |||
d.created_on = created_on | |||
d.modified_on = created_on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line has no effect since, auto_now_add
is used for modified_on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! We are in the situation described here:
#75 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @owocki intention was to capture the time of creation on the poloniex server with this.
A proposal could be we add deposit_created_on
to Deposit
model and use created_on
for the creation of the model in our database.
In short:
deposit_created_on
- time of creation at poloniex server
created_on
- time of saving in our database
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems reasonable. The only downside of this approach is that we will have to remember to use deposit_created_on
instead of created_on
.
would like to get another to sign off on merge here... @Snipa22 @igorpejic @t0mk ? |
@rubik could you describe how you tested this? does your personal instance of pytrader have data flowing through the admin? |
@owocki I had price data in the admin and the chart was displaying correctly. Then I generated fake data in another Django project to test PivotChart and it was working correctly. What do you think about adding |
What changed
DateTimeField
s are guaranteed to be already UTC since whenUSE_TZ = True
Django will set PostgreSQL's timezone config option to UTC, as explained here:https://docs.djangoproject.com/en/1.9/ref/databases/#optimizing-postgresql-s-configuration;
created_on
andmodified_on
do not rely anymore onget_time
, everything is done automatically withauto_now=True
andauto_now_add=True
. Those two options use internallytimezone.now()
;created_on_str
is now saved in UTC. In this way the DB is not tied to a specific timezone. However, it's still not perfect, since the old data is still in MST and a migration could be necessary.created_on_str
remained in MST.Thanks to django-chartit2,
created_on_str
anddate_str
have been removed.Migration
Since PostgreSQL stores all the datetimes as UTC, no DB migration is required. However, some Django fields have been altered, so a
python manage.py migrate
is necessary to get things working.Notes
created_on
andmodified_on
are no longer shown in the admin. This is due to the fact that those two attributes are now managed automatically by Django.For reference, see #9.