In AnQiCMS templatestampToDateThe mystery of time formatting: Unveiling the Go language time standard
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 (AnQiCMS) benefits from the powerful genetics of the Go language, inheriting the unique elegance and efficiency of Go language in template processing.Today, let's delve deeply into a commonly used and powerful time processing function in the Anqi CMS template——stampToDateEspecially the unique 'format' parameter followed by the Go language time formatting standard.
stampToDateThe format source: Reference time in Go language
To put it bluntly, in Anqi CMS templatestampToDateThe format parameter of the function followsThe time formatting standard built into Go languagePeriod, unlike that commonly used in other programming languagesY-m-d H:i:soryyyy-MM-dd HH:mm:ssThis symbol placeholder is different, Go language adopts a very unique and intuitive“Reference time”method to define date and time formats.
This "reference time" is a fixed date and time:2006-01-02 15:04:05.
At first glance, this string of numbers may be perplexing, why exactly this time?In fact, this is not randomly selected, but carefully chosen by the Go language designer, each number corresponds to a date and time unit, and has uniqueness, which is convenient for developers to remember and use:
2006Represents the year (Year)01Represents the month (Month), lowercasejanorJanAlso02Represents the day of the month (Day of month)15Represents an hour (Hour, 24-hour format), i.e., 3 PM04Represents a minute (Minute)05Represents a second (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".The internal mechanism of Go language will intelligently parse the date and time unit you want to express based on the "reference time" pattern and apply it to the actual timestamp.
Specific practice of formatting
Understood the reference time of Go language,stampToDateThe usage becomes clear. In Anqi CMS template, we usually get such as documents.CreatedTime(Creation time) orUpdatedTime(Update time) Such a Unix timestamp. These timestamps are usually 10 or 13-digit integers.stampToDateThe function expects a 10-digit timestamp and formats it.
For example, if we have a timestamp1609470335Corresponding to2021-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 the2006-01-02to represent:{{ stampToDate(publishStamp, "2006-01-02") }} {# 结果可能为: 2021-01-01 #}Display the year, month, day, and minute accuracy (for example:
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' exactly matches the hour and minute in the reference time.
Display the full timestamp format (for example:)
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 formatting style of Go language is considered 'magic' because it is more flexible than the traditional placeholder system.You can almost arrange these numbers in any way you want, Go will understand your intention.For example, if you want to display as01/01/21, you can use01/02/06as the format string.
In the actual application of the Anqi CMS template
In the AnQi CMS template,stampToDateFunctions are often combined with the document'sCreatedTimeandUpdatedTimefields. These fields are usually stored in the form of Unix timestamps. For example, when outputting an article list in a loop:
{% 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 very finely control the display of time, meeting different design needs and user preferences.
It is worth mentioning that Anqi CMS also providesdateA filter, but it is usually used to process Go language primitivestime.Timetype objects, butstampToDateis specifically used to handle Unix timestamps, which is an important distinction in template development.
Mastered the reference time format of Go language, you can handle various time display needs effortlessly in the AnQi CMS template, so that the content of your website is always presented in the most user-friendly and business logic manner.This unique and efficient mechanism is exactly the charm of Anqi CMS as a Go language CMS.
Frequently Asked Questions (FAQ)
Ask: Why is the time formatting standard of the Go language not 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 asYRepresents the year,mRepresenting the month may be ambiguous or may require additional reference tables. Go language adopts the 'reference time'2006-01-02 15:04:05Each number has a unique and intuitive meaning, directly used as a template for format strings, making the definition of the format clearer and easier to remember (once you understand this set of "magic numbers").It avoids memorizing a set of new symbols but directly simulates the format you want to output.Ask: If I get a timestamp that is not 10 digits long (for example, 13 digits containing milliseconds),
stampToDateCan I still use it?Answer:stampToDateThe function expects a 10-digit Unix timestamp (second-level timestamp).If your timestamp is 13 digits (millisecond level), you need to convert it to 10 digits first, that is, divide it by 1000.For example, simple mathematical operations can be performed in the template{{ stampToDate(item.CreatedTime / 1000, "2006-01-02") }}In actual development, it is more recommended to uniformly process the timestamp to 10 digits in the backend or business logic layer before passing it to the template to keep the template clean.Question: Besides,
stampToDateIs there another way to format time in AnQi CMS template?Yes, AnQi CMS, which is developed in Go language, also supports native Go language types.time.TimeValuesdateFilter. However,stampToDateis specifically designed to handle common Unix timestamps. If you operate directly ontime.Timetype variables, you can use{{ myTimeVar|date:"2006-01-02" }}This syntax. But for fields like timestamps typically obtained from databases or APIs (such asCreatedTime)stampToDateIt is a more direct and recommended choice.