AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go programming language, with powerful template engine features that make content display flexible and diverse. In daily content operations, the formatting of time display is a common requirement, andstampToDateLabels are born for this. It can convert Unix timestamps into the date and time format we are familiar with. However, many operators may encounter a confusion: why usestampToDateThe formatted time sometimes displays as UTC time instead of the local time of my website, is that right?Today, let's delve into this issue and provide practical solutions.

KnowstampToDateLabel and its working principle

In the template design of AnQi CMS,stampToDateA tag 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 concise and clear:{{stampToDate(时间戳, "格式")}}The "timestamp" usually originates from the document's creation time (item.CreatedTime) or update time (item.UpdatedTime)fields, while the “format” follows the special time formatting rules of the Go language (for example"2006-01-02 15:04:05").

As we can see from the document description,stampToDate

why does it show UTC time?

We need to review the essence of timestamps to understand this question.Unix timestamp (also known as POSIX time) is the number of seconds elapsed since 00:00:00 UTC on January 1, 1970.It does not contain time zone information.It is just a number representing a moment.

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 as it avoids data confusion in multi-timezone scenarios, ensuring consistency and accuracy of time data.

WhenstampToDateThe 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 default UTC, it will directly format the UTC timestamp into a string of 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 misconception of 'time is wrong'.

EnsurestampToDateA solution to display local time.

SincestampToDateThe label itself does not have a direct timezone parameter, so in order to display local time, our focus needs to be placed onThe server environment where the Anqi CMS is runningUp.The Go language application defaults to reading the system's timezone settings and uses this to handle time localization.Therefore, the most direct and effective method is to ensure that the timezone configuration of your security CMS server is correct.

1. Configure the server time zone

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

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

    timedatectl status
    

    Then, list all available timezones:

    timedatectl list-timezones
    

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

    sudo timedatectl set-timezone Asia/Shanghai
    

    Set after completion, run againtimedatectl statusConfirm that the time zone has been changed.

    For some older systems or those withouttimedatectlyou may need to manually create or link/etc/localtimethe file and setTZEnvironment variable.

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

2. Restart the Anqi CMS service

When changing the server time zone, merely saving the settings is not enough.The AnQi CMS as a Go application loads the system environment's timezone configuration on startup.Restart the Anqi CMS serviceto have it reread and apply the new timezone settings.

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

3. Verify time display

Refresh your website page after restarting the service, and check againstampToDateFormatted time. They should now display correctly as your local time.

To verify more accurately, you can try using formats that include timezone offsets in addition to the conventional date format, such as{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05 -0700 MST")}}.-0700 MSTThis will display the time zone offset and the time zone name, which can help you confirm if the Go application is correctly identifying the local time zone. For example, if it shows as+0800 CSTIt indicates that the time has been correctly identified as China Standard Time (CST, UTC+8).

Advanced Consideration: Front-end JavaScript Assistance for Display

Although the server-side configuration is to solvestampToDateThe fundamental solution to time zone problems, but in certain specific scenarios, you may also want to handle time display on the front end through JavaScript, for example, if your website users are spread across the globe and you want the time to be displayed based onThe time zone of the user's browserto dynamically display.

In this case, you can continue to letstampToDateOutput a UTC timestamp (or a UTC string in Go standard format), then pass this value to the frontend JavaScript. In JavaScript, you can useDateObjects'toLocaleString()ortoLocaleDateString()Methods, which will automatically display the time as local time according to the user's browser settings.

For example, in the template:

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

In JavaScript: "`javascript"