doushifen4060 2019-01-04 08:26
浏览 894


I wrote a Golang program that runs on OpenWRT.

package main

import (

func main(){

When I run this program on my Macbook, I always get the correct local time.

However, when running this program on OpenWRT, I always get UTC time.

I have set the time zone and time of OpenWRT. When I execute uci show system, I can see the right time zone. When I execute date the right local time can be display correctly.

So my question is, how do I get the correct local time using Golang's time.Now() on OpenWRT?

  • 写回答

2条回答 默认 最新

  • dsvcqvp139098 2019-01-04 10:10

    The root of my problem is that my OpenWRT lacks the zoneinfo package. So I run opkg update && opkg install zoneinfo-xxx first.

    This is part of go/src/time/zoneinfo_unix.go:

    func initLocal() {
        // consult $TZ to find the time zone to use.
        // no $TZ means use the system default /etc/localtime.
        // $TZ="" means use UTC.
        // $TZ="foo" means use /usr/share/zoneinfo/foo.

    According to this file, if there is no "TZ" variable, Golang will use the time zone pointed to by /etc/localtime when calling time.Now().

    Then I set time zone at /etc/config/system (option zonename 'xxxxxx') and run /etc/init.d/system restart. Finally, I can get the correct time.Now().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥20 matlab有约束条件下的多元函数求最小值
  • ¥50 如何隐藏网页弹出框的url地址栏
  • ¥20 metropolis算法模拟二维ising模型来计算磁化强度,fortran
  • ¥15 uniapp-typescript-vue报错
  • ¥15 oracle强制关机以后报错01033
  • ¥15 给Chat with RTX添加语言模型时遇到问题
  • ¥15 oracle修复,怎么根据日志修复呀?
  • ¥15 使用Stable Diffusion时出现错误
  • ¥15 请教含粗糙度的点接触润滑问题
  • ¥100 需要3万组微信数据,昵称+头像+朋友圈背景