In the template world of AnQi CMS, we often encounter situations where we need to format and display time data.Whether it is the publication time of the article, the update date of the product, or the submission moment of user comments, a clear and easy-to-read time format is crucial for the website user experience.AnQi CMS is a powerful content management system developed based on the Go language, which is SEO-friendly and also provides an efficient and clear strategy for handling time data.

When you browse the template tag document of Anq CMS, you will find that there are two filters (or template functions) that are related to time formatting: one is a generaldateFilter, another is designed specifically for handling timestampsstampToDateFilter. At first glance, both seem to be able to complete the task of time formatting, but why does the CMS recommend usingstampToDateProcess timestamps instead of using them directly.dateWhat about filters? This involves careful consideration of data types, Go language features, and development experience.

Deep understandingdateFilters: Type is the key.

Firstly, let's understanddateFilter.It is very common in many class Django template engines, providing a concise way to format dates and times.time.TimeType variable, thendateThe filter is very intuitive and convenient to use. You just need to pass in the format string as simply as this:

{{ someTimeObject|date:"2006-01-02 15:04:05" }}

However, the key issue is,dateFilter has strict requirements for the input data type. As explicitly stated in the documents of Anqi CMS:Note that this value must betime.TimeType, not a timestamp, if it is a timestamp, it will report an error.This sentence reveals the core limitation. Go is a statically typed language, and a timestamp of an integer type (such as Unix timestamp, usually represented by 10 digits indicating seconds) and atime.TimeThe object is a completely different concept in the Go type system. Passing an integer timestamp directly to the expectedtime.TimeObjects'dateFilter, it will cause type mismatch errors, leading to the failure of page rendering.

stampToDate: Elegant answer to timestamp by AnQi CMS

It is precisely to solve this universally existing problem of data type differences that AnQi CMS introducesstampToDateSuch a specialized template function.Its design goal is very clear: safely and efficiently convert Unix timestamps (usually integer values obtained from databases) into a readable date-time format.

When you usearchiveList/archiveDetail/commentListWhen getting document and comment data with tags likeitem.CreatedTimeanditem.UpdatedTimeSuch fields, after being retrieved from the database, are often in the form of Unix timestamps.This is a common and efficient way to store time information in a database.stampToDateit becomes a choice:

{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}

This function accepts two parameters: the first is a 10-digit timestamp (for example1609470335), and the second is a formatted string following the Go language standard layout. For example,"2006-01-02 15:04:05"It will format the timestamp into the form of "Year-Month-Day Hour:Minute:Second".

Why is this distinction so important?

ThisdateandstampToDateclear distinction between, and recommend the use ofstampToDateThe strategy, brought multiple benefits:

  1. Suitable for data source characteristics:The CMS design, document creation and update time information, for the efficiency of database storage and query, is often stored as Unix timestamp (integer).stampToDateDirectly caters to this data storage method, avoiding additional type conversions in the backend controller.
  2. Follows the strong typing principle of the Go language: dateThe filter needs a complextime.Timestruct, whereas the original timestamp is just a simpleint64type. If notstampToDate, developers would have to perform explicit type conversions outside of the template layer (such as in the controller), which increases the complexity of the code.stampToDateEncapsulate this conversion logic in the template layer so that front-end developers can focus more on content presentation without delving into the type details of Go language.
  3. Improve development efficiency and avoid errors:By providing a dedicated timestamp handling tool, AnQi CMS significantly reduces errors caused by time type mismatches during template development. Developers can pass timestamp data directly from the database with greater confidence.stampToDateAnd don't worry about type issues.
  4. Clear responsibility division: stampToDateThe presence, making the intention of time formatting more clear. When we see it, we immediately know we are dealing with a timestamp; while if we usedateThis means that we are operating on an object that has been fully converted to Gotime.TimeThe variable of the object. This clarity helps with the understanding and maintenance of the code.
  5. The inheritance of Go language formatting conventions: stampToDate沿用了Go语言独特的时间格式化方式(based on)2006-01-02 15:04:05This reference time point), which is more natural and unified for developers familiar with the Go programming language.It ensures consistency of time formatting in the AnQi CMS template with the Go language ecosystem.

In summary, it is recommended that the Anqi CMS usestampToDateA filter to process timestamps, which is a practical and elegant manifestation of Go language CMS design.It provides a dedicated tool to effectively bridge the gap between the original timestamp stored in the database and the required format for template display, greatly optimizing the development experience and avoiding common issues that may arise due to data type mismatch.


Common Questions (FAQ)

1. Why is the time formatting string of Go language2006-01-02 15:04:05, rather than likeY-m-d H:i:sSuch format?

The time formatting of the Go language adopts a unique "reference time" pattern. It does not use something likeY-m-dThis placeholder, but uses a specific date and time value2006-01-02 15:04:05.999999999 -0700 MSTCome as a formatted template. All you need to do is selectively combine the various numbers and parts of this reference time according to the output format you want.年-月-日,then write2006-01-02. If you want to output时:分, write15:04. This way may seem unusual at first glance, but once you get the hang of it, you will find it very flexible and precise.

2. What if my timestamp is not 10 digits (second level) but 13 digits (millisecond level)?

Anqi CMS'sstampToDateThe filter is designed to handle standard 10-digit Unix timestamps (i.e., time in seconds). If your timestamp is a 13-digit millisecond timestamp, pass it directlystampToDateIt may result in incorrect formatting due to its interpretation as a very large number of seconds.In this case, you need to divide it by 1000 before passing it to convert it into a 10-digit timestamp at the data source or controller layer, or confirm whether the future version of Safe CMS will provide a filter that supports millisecond timestamps.In the current version, it is recommended to convert to a 10-digit second timestamp before use.

3. Can I convert an object to a timestamp in the template?time.TimeDoes the document in the Anqi CMS template system clearly state?

that it is converting a timestamp to a date format, whilestampToDateis converting a date format to a timestamp.dateFilter is to formattime.Timethe object. The document does not directly provide a built-in filter to converttime.TimeConvert an object to an original Unix timestamp (integer). If there is indeed such a need, the usual practice is to perform this conversion in the controller or backend logic, and then pass the converted