The secrets of the time formatting in AnQi CMS template:stampToDateDeciphering the Go language's time standards

As an experienced website operations expert, I know that the flexible display of timestamps in content management systems is crucial for the freshness of website information and user experience.AnQiCMS relies on the powerful genes of Go language and inherits the unique elegance and efficiency of Go language in template processing.stampToDateEspecially the unique 'format' parameter that follows the Go language time formatting standard.

stampToDateThe source of the format: the reference time in Go language.

To put it directly, in the Anqi CMS template,stampToDatethe 'format' parameter of the function follows the sametime formatting standard that comes with the Go language. As with the common practices in other programming languages,Y-m-d H:i:soryyyy-MM-dd HH:mm:ss[en]The symbol placeholders are different, Go language adopts a very unique and intuitive approach.[en]Reference time.[en]A method to define the date and time format.

This 'reference time' is a fixed date and time:2006-01-02 15:04:05.At first glance, this sequence of numbers may be confusing, why exactly at this time?

  • 2006Represents the year (Year)
  • 01Represents the month (Month), lowercasejanorJanAlso can be
  • 02Represents date (Day of month)
  • 15Represents hour (Hour, 24-hour format), i.e., 3 PM
  • 04Represents minute (Minute)
  • 05Represents seconds (Second)

In simple terms, when you want to format a timestamp into a specific format, you just need to 'write' the expected output format using this "reference time".Go language's internal mechanism will intelligently parse the date and time unit you want to express based on the "reference time" pattern you provide and apply it to the actual timestamp.

Specific practices of formatting

Understood the reference time of Go language,stampToDateThe usage becomes clear. In the Anqi CMS template, we usually get things like documents.CreatedTime(Creation time) orUpdatedTime(Updated time) such Unix timestamp. These timestamps are usually 10-digit or 13-digit integers.stampToDateThe function is expected to receive a 10-digit timestamp and format it.

For example, if we have a timestamp1609470335(corresponding)2021-01-01 15:05:35), we want to display it in a different format:

  • Only show the year, month and date (for example:)2021-01-01)You just need to use the reference time in2006-01-02Represented as:

    {{ stampToDate(publishStamp, "2006-01-02") }}
    {# 结果可能为: 2021-01-01 #}
    
  • Display the year, month, day, and minute accuracy (e.g.,)2021年01月01日 15:05)Combine the corresponding parts of the reference time:

    {{ stampToDate(publishStamp, "2006年01月02日 15:04") }}
    {# 结果可能为: 2021年01月01日 15:05 #}
    

    Please note that the "15:04" precisely matches the hour and minute in the reference time.

  • Show the full timestamp format (e.g.,)2021-01-01 15:05:35)Use the full reference time format:

    {{ stampToDate(publishStamp, "2006-01-02 15:04:05") }}
    {# 结果可能为: 2021-01-01 15:05:35 #}
    

The reason why this formatting style in Go language is considered 'magic' is that it is more flexible than the traditional placeholder system.You can almost arrange these numbers in any way you want, and Go will understand your intention.01/01/21You can use01/02/06as the format string.

The actual application in the AnQi CMS template

In the templates of AnQi CMS,stampToDatefunctions are often used with theCreatedTimeandUpdatedTimefields. These fields are usually stored in the form of Unix timestamps. For example, when looping to output a list of articles:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
    <li>
        <a href="{{item.Link}}">
            <h5>{{item.Title}}</h5>
            <div>发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</div>
            <div>更新时间:{{stampToDate(item.UpdatedTime, "2006/01/02 15:04")}}</div>
        </a>
    </li>
    {% endfor %}
{% endarchiveList %}

Here,item.CreatedTimeanditem.UpdatedTimeis passed instampToDateThe timestamp, followed by the string defines their display style on the page.This way allows template designers to have very fine control over the display of time, meeting different design needs and user preferences.

It is worth mentioning that the Anqi CMS also providesdatea filter, but it is usually used to process native Go languagetime.Timetype objects, whereasstampToDateThis is specifically used for handling Unix timestamps, which is an important distinction in template development.

You can handle various time display needs skillfully in the Anqi CMS template by mastering the reference time format of the Go language, ensuring that your website content is always presented in the most user-friendly and logical way.This unique and efficient mechanism is exactly what makes AnQi CMS, a Go language CMS, charming.


Common Questions and Answers (FAQ)

  1. Question: Why isn't the time formatting standard in the Go language more common?Y-m-d H:i:sIn this form?Answer: One of the design philosophies of the Go language is simplicity and clarity. Traditional symbol placeholders (such asYrepresenting a year,m[representing month) may be ambiguous or require additional lookup tables. Go language uses a "reference time"2006-01-02 15:04:05Each number has a unique and intuitive meaning, used directly as a template for the format string, making the definition of the format clearer and easier to remember (once you understand this set of 'magic numbers').It avoids the need to memorize a new set of symbols and directly simulates the format you wish to output.

  2. Question: If the timestamp I get is not a 10-digit number (for example, it contains milliseconds),stampToDateis it still usable?Answer:stampToDateThe function is expected to receive a 10-digit Unix timestamp (second-level timestamp).If your timestamp is 13 digits (millisecond level), you need to convert it to 10 digits, that is, divide it by 1000.{{ stampToDate(item.CreatedTime / 1000, "2006-01-02") }}But in actual development, it is more recommended to uniformly process the timestamp into 10 digits at the backend or business logic layer before passing it to the template to maintain the cleanliness of the template.

  3. Question: BesidesstampToDateAre there any other ways to format time in Anqi CMS template?Answer: Yes, Anqi CMS, developed in Go language, also supports native Go languagetime.Timetype valuesdateFilter. However,stampToDateis specifically designed to handle common Unix timestamps. If you operate directly on variables of thetime.Timetype in the template,{{ myTimeVar|date:"2006-01-02" }}This syntax. But for timestamp fields typically obtained from databases or APIs (such asCreatedTime)stampToDateis a more direct and recommended choice.