Tutorials on using date and time in the Python Flask framework

Source: Internet
Author: User
Tags day and date time zones iso 8601

Tutorials on using date and time in the Python Flask framework

This article mainly introduces how to use the date and time in the Python Flask framework, including processing the conversion between different time zones. For more information, see

Timestamp Problems

One of our Weibo applications that has been ignored for a long time is the display of day and date.

Until now, we have used Python in our User and Post objects to render time objects, but this is not a good solution.

Consider this example. I am writing this article. It is at on March 13, December 31. My time zone is PST (or you are more accustomed to: UTC-8 ). Run in the Python interpreter and I get the following output:

?

1

2

3

4

5

6

7

>>> From datetime import datetime

>>> Now = datetime. now ()

>>> Print now

15:54:42. 915204

>>> Now = datetime. utcnow ()

>>> Print now

23:55:13. 635874

In my place, the now () method returns the correct time, but the time returned by now () is UTC.

Which one is better?

If we use now (), the timestamps in all databases will be consistent with the local time when the server runs, which will cause some problems.

For example, if one day we need to place the server somewhere else (not in the same time zone), the time in the database needs to be updated to be consistent with the new location before restarting the server.

There will be more important issues. Users in different time zones will have difficulty in knowing when to send emails. If users see the PST time zone, they will have difficulty in knowing when to send emails, this requires the user to make corresponding adjustments based on the time.

Obviously, this is not a good choice. This is why we use the UTC time zone to save the timestamp when creating a database.

When the standardized timestamp is UTC, the mobile server problem is solved. However, he cannot solve the second problem. Data and time are presented to users in UTC in different parts of the world.

Assume that a user sends an email at three o'clock P.M. In the PST time zone. The email is immediately displayed in front of him, with the message, or a more specific point ).

The purpose of this article is to make our users no longer confused by the display of data and time.

Use a specific Timestamp

The common solution is that every user converts the time from UTC to the local time. This requires us to dynamically change to make the database UTC consistent with it.

But how do we know where the user is?

Many websites have a setting page to set their time zone. In this case, we need to add a new page and provide a drop-down box on the form to allow the user to select the time zone. the user needs to set the time zone for the first login and use it as part of registration.

This is a normal solution, but it is a little cumbersome for users. users need to enter a piece of information they have already configured in the operating system. Therefore, if we can capture the time zone set on the user's computer, the problem will be solved more efficiently.

For security reasons, the browser does not allow us to access the user's operating system to obtain information. Even if it permits, we also need to know where the time zone can be obtained in Windows, Linux, Mac, iOS, and Android, which does not include other non-mainstream operating systems.

Get the user's time zone in the browser, and then get it through the standard Javascript API. In the Web 2.0 world, users allow Javascript Execution (few websites do not use Javascript), so it is feasible to obtain the user time zone through Javascript.

You can use Javascript to configure available time zones in two ways:

Old school's practice: when a user logs on to the server for the first time, the browser sends the time zone information to us in some way. This can be implemented through Ajax or meta refresh tag. Once the server knows the time zone information, it can save it in the user session, and then adjust the time display of all pages.

New school's practice: it does not change anything on the server side, but still sends the UTC timestamp to the client browser. The conversion from UTC to local time is executed on the client through Javascript.

Both methods are effective, but the second method is more advantageous. The browser can complete the time conversion according to the local configuration of the system. Such as morning/afternoon vs 24-hour, day/month/year vs month/day/year, and other cultural formats, which are accessible by browsers, but the server is not necessarily the same.

If this is not enough, the new school's practice has a greater advantage, and others have already done this for us (moment. js is coming soon )!

Introduction to moment. js

Moment. js is a small, free, and open-source Javascript library that increases dates and events to another level. It provides all the time and date formats that can be imagined. Below are some.

To use moment. js in our applications, we need to write such a piece of Javascript code in our template file. We will first create a moment object through the ISO 8601 time. For example, you can use the UTC time in the Python example above to create a moment object, as shown in the following figure:

?

1

Moment ("2012-12-31T23: 55: 13 Z ")

Once an object is created, it can be converted into strings of various formats. For example, to convert a long time display to a local system:

?

1

Moment ("2012-12-31T23: 55: 13 Z"). format ('llll ');

The following figure shows the time after conversion:

?

1

Tuesday, January 1 2013 AM

Here are more examples to convert the same timestamp into different formats:

This class library supports more than these conversion options. In addition to format (), it also provides fromNow () and calendar () friendly timestamp conversion methods:

Note that in all the examples above, the server converts the same UTC time, and your local browser converts the time to a different one.

Finally, we add a little Javascript tips that we missed. On the page, it is obvious that the Code actually returns the string type by moment. The simplest way to do this is to use the document. write method of Javascript:

?

1

2

3

<Script>

Document. write (moment ("2012-12-31T23: 55: 13 Z"). format ('llll '));

</Script>

Using document. write in Javascript is a simple and straightforward way to generate part of HTML code. However, note that this method has some restrictions. The most imperative is that the document. write method can only be used when the document is loaded. After the document is loaded, it cannot modify the document. The result of this restriction is that this solution fails when data is loaded using Ajax.

Integrate moment. js

Here we need to add moment. js to our blog.

First, we need to download the moment. min. js Library to the/app/static/js folder so that it can serve as a static file as a client.

Then we add a reference to this library (moment. min. js) in our template file (fileapp/templates/base.html:

?

1

<Script src = "/static/js/moment. min. js"> </script>

Now we can add

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.