In website operation, the display of content release time is often one of the details that users pay attention to. Ensuring that these dates and times are presented in a consistent format across different server environments (such as the familiar Linux and Windows) is an important aspect of improving user experience and maintaining website professionalism. As a senior operator of Anqi CMS, I am well aware of its powerful capabilities based on the Go language, especially its template tagsstampToDateUnique advantages in dealing with such issues.

Today, let's delve into how to make use of the Anqi CMS.stampToDateThe label ensures that the output date format remains consistent regardless of the operating system on which the website is deployed.

UnderstandingstampToDateand the philosophy of the language

The reason why AnQi CMS can perform well in terms of date format consistency is largely due to the powerful design of its underlying Go language. In Go, time formatting does not depend on the operating system or locale settings like some other languages.strftimeStyle strings. Conversely, the Go language adopts a uniqueReference timePattern.

This reference time is:Mon Jan 2 15:04:05 MST 2006. You didn't read wrong, the time formatting of the Go language is defined using this specific date as a 'template'.For example, if you want to display "Year-Month-Day Hour:Minute:Second", you are not writing%Y-%m-%d %H:%M:%SInstead, write2006-01-02 15:04:05. The greatest benefit of this design is:The format string itself is platform-independent.2006Always represents the year,01The month is always represented, regardless of whether your server is running on Linux or Windows, in Asia or Europe, this formatting rule will not change.

Of Security CMSstampToDateTemplate tags, which perfectly inherit this feature. Their usage is{{stampToDate(时间戳, "格式")}}Where "timestamp" is usually a 10-digit Unix timestamp (the number of seconds since January 1, 1970 UTC time), and "format" is the formatting string according to the Go language reference time.

EnsurestampToDateThe key strategy for consistency of output

Although the format strings in Go language have already solved the cross-platform consistency issues, in practice, it is still necessary to pay attention to several aspects to ensurestampToDateThe final outputcontentAlso consistent:

  1. Use the reference time format string of Go language统一This is the most basic and most important point. Anqi CMSstampToDateThe label explicitly requires the use of Go language format. This means that you need to be familiar with and apply2006-01-02 15:04:05this kind of pattern. For example:

    • Display “2023年06月07日”:{{stampToDate(item.CreatedTime, "2006年01月02日")}}
    • Display “2023-06-07 14:30”:{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}
    • Display “Wednesday, 07 June 2023 14:30:00 UTC”:{{stampToDate(item.CreatedTime, "Mon, 02 Jan 2006 15:04:05 -0700")}}If you strictly use these Go-style formatted strings in the template,stampToDateyou can ensure that the output date style is exactly the same on any operating system.
  2. Ensure the consistency of the timestamp source (UTC Unix timestamp): stampToDateA 10-digit timestamp is received. In most cases, this timestamp refers toUTC Unix timestampIt is a time standard that is unrelated to time zones. AnQi CMS saves the publishing time of articles in the background (CreatedTimeThis is typically stored in this standard format. This means that as long as your content management system (Anqi CMS itself) or any external integration system follows the standard of UTC Unix timestamp when generating and transmitting timestamps, thenstampToDateThe 'original time' received from different servers is consistent. This is the premise to ensure the consistency of the final date value.

  3. Application-level timezone configuration (determines the localization of date values):This is the place that is most likely to produce 'inconsistencies in appearance'. Even though the timestamp is in UTC, the Go language will default to converting it to human-readable dates based on the runtime environment'sTime ZonePerform localization display. For example, a UTC timestamp1672531200(Corresponding to UTC 2023-01-01 00:00:00), if configured asAsia/Shanghaitime zone,stampToDateIt may output2023-01-01 08:00:00; If the system time zone is set on the Windows serverAmerica/New_Yorkit may output2022-12-31 19:00:00. To solve this problem, you need to:

    • Check the global settings of Anq CMS:Although the current document does not explicitly mention the existence of a 'global time zone setting' option in the background, a mature CMS usually provides it. If it is available, be sure to configure it to a target time zone uniformly on all sites and servers (for exampleAsia/Shanghai)
    • Unified server operating system time zone:If AnQi CMS does not provide a global time zone configuration, then the Go program may inherit the time zone setting of the operating system at runtime.In this case, ensuring that all Linux and Windows servers deployed with Secure CMS are configured to the same time zone is the most effective method to ensure consistency in the output date and time.Can be used on Linuxtimedatectl set-timezone Asia/ShanghaiOn Windows, it is configured through system settings.
    • Explicitly output time zone information (optional but recommended):If your website is aimed at global users, or needs to display in different time zones, you may also need to consider attaching a time zone abbreviation after the date (such asCST/EST) or UTC offset (such as+0800),Let users understand the time base displayed.{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05 MST")}}or{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05 -0700")}}.

Actual operation example

Let's look at a simple example, assuming we have an article object.archiveof whichCreatedTimeIt is a 10-digit Unix timestamp.

<p>文章发布时间:{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04:05")}}</p>
<p>简化日期:{{stampToDate(archive.CreatedTime, "2006-01-02")}}</p>
<p>仅显示时间:{{stampToDate(archive.CreatedTime, "15:04:05")}}</p>
<p>带有时区缩写:{{stampToDate(archive.CreatedTime, "2006-01-02 15:04:05 MST")}}</p>

As long as you strictly follow the formatting rules of the Go language and properly manage the server