Pythonanywhere Timezone

264 views
Skip to first unread message

Jeff Riley

unread,
Jul 7, 2016, 3:19:10 PM7/7/16
to web2py-users
Hello all.  Me again.  I have run into an interesting issue.  I have installed and followed niphlod's w2p_timezone_plugin.  In order to make it work on my local PC I need to setup my db.py as follows.

import pytz
user_timezone = session.plugin_timezone_tx or 'UTC'


db.define_table('events',
Field('event_time', 'datetime', default = request.now, update = request.now,
requires=IS_DATETIME(format=('%m-%d-%Y %H:%M'), timezone=pytz.timezone(user_timezone))),


But to get it to work on pythonanywhere.com I have to setup my db.py as follows. Any ideas why?


import pytz

db.define_table('events',
Field('event_time', 'datetime', default = request.now, update = request.now,
requires=IS_DATETIME(format=('%m-%d-%Y %H:%M'), timezone=pytz.timezone("US/Central"))),



Marlysson Silva

unread,
Jul 7, 2016, 6:10:14 PM7/7/16
to web2py-users
Where you are inicialize session.plugin_timezone_tx ?

I too already had this issue, this happen because the script get timezone based on the server localization.

I solved this using other way, converting user timezone to UTC and at showing converted in user timezone.

They are some steps to do this.

Jeff Riley

unread,
Jul 7, 2016, 7:01:47 PM7/7/16
to web...@googlegroups.com
I have that in the default.py file. I will send the code when I get to my computer.

Thank you,
Jeff Riley
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/wL2y7s-FJWU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeff Riley

unread,
Jul 7, 2016, 10:52:30 PM7/7/16
to web2py-users
Here is the code as promised for the db.py and default.py.
default.py.docx
db.py.docx

Jeff Riley

unread,
Jul 8, 2016, 8:56:48 AM7/8/16
to web2py-users
Just in case anyone is wondering why I would build such a strange little web app, it is for my personal use.  I was dianosed with brain cancer 6 weeks ago and these are the items my doctors want me to track everyday.  So I thought Web2py would be perfect.


On Thursday, July 7, 2016 at 2:19:10 PM UTC-5, Jeff Riley wrote:
Message has been deleted

Marlysson Silva

unread,
Jul 8, 2016, 9:16:46 AM7/8/16
to web2py-users

Are you trying to get the timezone just of the your location? 
Case yes, you set your timezone at code, and deploy in pythonanywhere with value personal of the timezone.

But you want timezone by any place, then I go prepare a article showing what I made to solved this. 

Jeff Riley

unread,
Jul 8, 2016, 9:52:26 AM7/8/16
to web2py-users
I would like to know how a timezone by any place would be done, so I look forward to your article.  For me I am just going to keep it set to US/Central.  Also if anyone wants to use the code I have provided to build this sort of web app for the general public, please feel free to use what I have provided.

Marlysson Silva

unread,
Jul 8, 2016, 10:08:54 AM7/8/16
to web2py-users
Great, I will preparing them. Put your code on github to follow the development, and to contribuit in something.

Jeff Riley

unread,
Jul 8, 2016, 11:44:42 AM7/8/16
to Marlysson Silva, web2py-users

--

Marlysson Silva

unread,
Jul 10, 2016, 5:39:30 PM7/10/16
to web2py-users
You try to make this:

1. Add library in the front-end to get user timezone ( this code need to be in the first page that user access ), to get timezone first.

{{if timezone_is_unknown: }}
<script>
  $
(function() {
   
var tz_name = jstz.determine();
    $
.get("{{=URL('default', 'define_timezone')}}", tz_name);
 
});
</script>


2. Add  the controller responsible by determine timezone:

def define_timezone():
   
"""Call ajax to determine user timezone"""

    tz_name
= request.vars.name

   
from pytz import all_timezones_set

   
if tz_name in all_timezones_set:
        session
.user_timezone = tz_name


3. And in the your model you put:

from pytz import timezone

timezone_user
= session.user_timezone or timezone('UTC')
timezone_is_unknown
= session.user_timezone is None


4. Use in the your model related with timezone:

IS_DATETIME(format=("%m-%d-%Y %H:%H), timezone = pytz.timezone(timezone_user) )

There are other form, that is insert everything in UTC datetime and create a function responsible to convert this timezone in UTC datetime inserted in database. Just delayed a lot of kkkk, but yet I will create a web2py_timezone_starter with app ready to receive apps that are handled by timezones.

I'm creating this application to demonstrate this, if you want look..


At app, the Hour "your PC" is datetime.now()  simulating datetime of user, just that it's datetime of the pythonanywhere.

Em quinta-feira, 7 de julho de 2016 16:19:10 UTC-3, Jeff Riley escreveu:

Jeff Riley

unread,
Jul 10, 2016, 5:42:05 PM7/10/16
to web...@googlegroups.com
Great thank you Marlysson.  I will try this as soon as I can.  Hopefully tomorrow.

Thank you,
Jeff Riley

Jeff Riley

unread,
Jul 11, 2016, 8:19:21 AM7/11/16
to web2py-users
Hello Marlysson.  I am getting the following error.  I have removed the module pytx_timezone, and I have verified that I have pip installed pytz.  Not sure what else to dheck.

<type 'exceptions.ImportError'> No module named pytz

Marlysson Silva

unread,
Jul 11, 2016, 8:25:17 AM7/11/16
to web...@googlegroups.com
Hm, in which file?

You removed the pytz library or a plugin that already was in your code previously?


Jeff Riley

unread,
Jul 11, 2016, 8:27:54 AM7/11/16
to web...@googlegroups.com
The plugin

Thank you,
Jeff Riley
Message has been deleted

Jeff Riley

unread,
Jul 11, 2016, 8:31:04 AM7/11/16
to web...@googlegroups.com
Let me attach the new code or are you able to see the git hub. If so I will push to that.

Thank you,
Jeff Riley

On Jul 11, 2016, at 7:29 AM, Marlysson Silva <marly...@gmail.com> wrote:

I think that I know where are missing pytz.. you put library in controller define_timezone() ..

Jeff Riley

unread,
Jul 11, 2016, 8:37:28 AM7/11/16
to web2py-users
here is the new db.py, default.py, and index.html since that is the opening page.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
index.html.docx
db.py.docx
default.py.docx
Message has been deleted

Marlysson Silva

unread,
Jul 11, 2016, 8:38:51 AM7/11/16
to web2py-users
Might put on github , but dont I can access them now

I will analyse your code by here , by while.

Later I will access github.

In error page, which file show the error?

Jeff Riley

unread,
Jul 11, 2016, 8:40:44 AM7/11/16
to web...@googlegroups.com
I have tried to get to the index page and I have gone to admin and then the database tab so I can setup the authorized user. Both give me the same error

Thank you,
Jeff Riley

On Jul 11, 2016, at 7:38 AM, Marlysson Silva <marly...@gmail.com> wrote:

Might put on github , but dont I can access them now

I will analyse your code by here , by while.

Later I will access github.

In error page, which file show the error?

Em segunda-feira, 11 de julho de 2016 09:31:04 UTC-3, Jeff Riley escreveu:

Marlysson Silva

unread,
Jul 11, 2016, 8:48:59 AM7/11/16
to web2py-users
You use just one model.py?
if yes, change this line:

IS_DATETIME(format=("%m-%d-%Y %H:%H), timezone = pytz.timezone(timezone_user) )

to 

IS_DATETIME(format=("%m-%d-%Y %H:%H), timezone = timezone(timezone_user) )

Jeff Riley

unread,
Jul 11, 2016, 9:21:53 AM7/11/16
to web...@googlegroups.com
Yes I do. I am not that advanced to be able to handle more than one lol. I will make that change.

Thank you,
Jeff Riley

Marlysson Silva

unread,
Jul 11, 2016, 9:24:49 AM7/11/16
to web2py-users
Any thing ,you talk with me by the Google Talk , for no to pollute here with conversations and attempts. 


Em segunda-feira, 11 de julho de 2016 09:40:44 UTC-3, Jeff Riley escreveu:
Reply all
Reply to author
Forward
0 new messages