AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, whose powerful template engine makes content display flexible and diverse. In daily content operations, the formatted display of time is a common requirement, andstampToDateThe label is just for this. It can convert Unix timestamps into the familiar date and time format. However, many operators may encounter a confusion: why usestampToDateFormatted time, sometimes it is displayed as UTC time instead of the local time of my website?Today, let's delve deeply into this issue and provide practical solutions.

Get to knowstampToDateLabel and its working principle

In the template design of AnQi CMS,stampToDateA label is a very practical tool that allows us to easily convert 10-digit Unix timestamps stored in databases into various readable date and time formats. Its basic usage is simple and straightforward:{{stampToDate(时间戳, "格式")}}. The 'timestamp' usually comes from the creation time of the document (item.CreatedTime) or update time(item.UpdatedTime) etc. fields, and the 'format' follows the time formatting rules specific to the Go language (for example"2006-01-02 15:04:05")

We can see from the document description that,stampToDateThe core function of the label is to "format timestamps". It receives a digital timestamp and then outputs a string in the specified format.The key is that this tag does not have a direct parameter to explicitly specify 'Please display time in a specific time zone'.This means that its output behavior largely depends on how it internally interprets this timestamp, as well as how the time zone is configured on the server environment where AnQi CMS is running.

Why does it show UTC time?

Understand this question, we need to review the essence of the timestamp.Unix timestamp (also known as POSIX time) is usually the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).This means, the timestamp itself isIt does not contain timezone informationIt is just a number representing a moment in time.

AnQi CMS, like many modern content management systems, usually uses UTC timestamps when storing time in the database.This is a common and recommended practice because it avoids data confusion in multi-time zone scenarios, ensuring the consistency and accuracy of time data.

WhenstampToDateWhen a label receives a UTC timestamp and formats it, if the underlying Go application does not explicitly perform timezone conversion, or if the server environment is set to UTC by default, it will directly format the UTC timestamp as a string representing UTC time.For users in the East Eighth Zone (such as Beijing time), this will cause the displayed time to be 8 hours earlier than the local time, thus creating a "time is wrong" illusion.

EnsurestampToDateSolution to display local time

SincestampToDateThe tag itself does not have a direct timezone parameter, so in order to make it display local time, our focus needs to be onThe server environment running AnQi CMSOn. The Go language application reads the system's timezone settings by default and processes time localization accordingly.Therefore, the most direct and effective method is to ensure that the time zone configuration of your secure CMS server is correct.

1. Configure the server time zone

This is usually the first step in solving time display issues. Whether it is a Linux or Windows server, you need to set its time zone to the target local time zone of your website.

  • For Linux servers (such as CentOS, Ubuntu):You can usetimedatectlCommand to manage system time zones. First, check the current time zone:

    timedatectl status
    

    Then, list all available time zones:

    timedatectl list-timezones
    

    Find the time zone you need (for exampleAsia/ShanghaiorAsia/Hong_Kong), then set it:

    sudo timedatectl set-timezone Asia/Shanghai
    

    Run again after setting uptimedatectl statusConfirm that the time zone has changed.

    For some older systems or nonetimedatectlcases, you may need to create or link manually/etc/localtimethe file and set upTZEnvironment variable.

  • For Windows server:In Windows system, you can change the time zone by the "Date and Time" settings.Right-click on the time in the taskbar, select "Adjust date/time", and then select the correct time zone in the settings window.

2. Restart the Anqicms service

After changing the server time zone, simply saving the settings is not enough.AnQi CMS as a Go application, it will load the system environment's time zone configuration when starting.Therefore, you mustRestart the Anqicms service, let it reread and apply the new timezone settings.

If you are using panel tools like Baota or 1Panel, you can find the restart button in the corresponding Go project management or container management interface. If you start manually through a script, you need to stop the service first (such as runningstop.sh), then start the service (run)start.sh)

3. Verify time display

Refresh your website page and check again after restarting the servicestampToDateFormatted time. They should now be displayed correctly as your local time.

To verify more accurately, you can also try using formats that include timezone offsets in the template, such as{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05 -0700 MST")}}.-0700 MSTThe timezone offset and timezone name will be displayed, which can help you confirm whether the Go application has correctly identified the local timezone. For example, if it shows as+0800 CSTIt indicates that it has been correctly identified as China Standard Time (CST, UTC+8).

Advanced considerations: Front-end JavaScript auxiliary display.

Although the server-side configuration is to resolvestampToDateThe fundamental method of time zone problems, but in some specific scenarios, you may also want to handle the display of time on the front end through JavaScript, for example, if your website users are spread all over the world and you want the time to be displayed according toThe time zone of the user's browserTo dynamically display.

In this case, you can continue to letstampToDateOutput a UTC timestamp (or a Go standard UTC formatted string), then pass this value to the front-end JavaScript. In JavaScript, you can useDatethe object'stoLocaleString()ortoLocaleDateString()Methods, they will automatically display the time according to the user's browser settings in the local time zone.

For example, in the template:

<span data-timestamp="{{ archive.CreatedTime }}"></span>

In JavaScript: