In website operation, the display of content publishing 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 an experienced operator of the Anqi CMS, I am well aware of its powerful capabilities based on the Go language, especially its template tagsstampToDateOn the unique advantage in dealing with such issues.

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

UnderstandingstampToDatethe philosophy of the [English]

The reason why AnQi CMS can excel in date format consistency is largely due to the powerful design of its underlying Go language. In Go, time formatting is not dependent on the operating system or locale like some other languages.strftimeStyle string. Instead, 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 in Go language uses this specific date as a 'template' to define.%Y-%m-%d %H:%M:%SInstead,2006-01-02 15:04:05.The format string itself is platform-independent.2006Always represents the year,01Always represents the month, regardless of whether your server is Linux or Windows, whether it runs in Asia or Europe, this formatting rule will not change.

Anqi CMS'sstampToDateTemplate tags, which perfectly inherit this feature. Their usage is{{stampToDate(时间戳, "格式")}}Where the "timestamp

EnsurestampToDateThe key strategy for output consistency

Although the format strings of the Go language itself have already solved the consistency problem across platforms, in practical applications, it is still necessary to pay attention to several aspects to ensurestampToDateThe final outputContentis also completely consistent:

  1. Unified use of Go language reference time format string:This is the most fundamental and most important point. The security CMS ofstampToDateLabel explicitly requires the format using Go language. This means, you need to be familiar with and apply.2006-01-02 15:04:05This kind of pattern. For example:.

    • Displaying “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")}}As long as you strictly use these Go-style formatted strings in the template,stampToDatethe output date "style" will be completely the same on any operating system.
  2. Ensure the consistency of timestamp sources (UTC Unix timestamp): stampToDateThe timestamp received is a 10-digit timestamp. In most cases, this timestamp refers toEnglish UTC Unix timestampIt is a time standard that is independent of time zones. The Safe CMS saves the publication time of articles in the background (CreatedTime)et cetera is usually stored in this standard form. 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 on different servers is consistent. This is the premise to ensure that the final date value is consistent.

  3. Application-level time zone configuration (determines the localization of date values):This is the easiest place to produce a 'inconsistent appearance'. Even though the timestamp is in UTC, the Go language will default to converting it to a human-readable date according to the runtime environment.time zonePerform localization display. For example, a UTC timestamp1672531200(Corresponding to UTC 2023-01-01 00:00:00),if configured as the timezone on a Linux serverAsia/Shanghaitime zone,stampToDateit may output2023-01-01 08:00:00;While on a Windows server if its system time zone isAmerica/New_York, it may output2022-12-31 19:00:00. To solve this problem, you need to:

    • Check the global settings of Security CMS:Although the current document does not explicitly mention the option of 'global timezone setting' in the background, a mature CMS should usually provide it. If available, please make sure to configure it as a target timezone consistently across all sites and servers (for exampleAsia/Shanghai).
    • Unified server operating system time zone:If the Safe CMS does not provide a global time zone configuration, the Go program may inherit the time zone settings of the operating system at runtime.In this case, ensuring that all Linux and Windows servers deployed with CMS are configured to the same time zone is the most effective way to ensure the consistency of the output date and time.timedatectl set-timezone Asia/ShanghaiIn 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 the user understand the basis of the displayed time.{{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.archivewhere,CreatedTimeIt 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>

Provided that you strictly adhere to the formatting rules of the Go language and properly manage the server