go - 允许从周日到周五运行吗?

标签 go time process weekday

以下 PHP 代码允许进程仅在特定时间之间运行。这在 Go 语言中如何完成?

$curdate = date('Y-m-d');
$mydate=getdate(strtotime($curdate));
if ( $mydate['wday'] === 0 ) {
  if ( date('H') < 15 ) { exit; }; // This is for 0 Sunday!!!
}
if ( $mydate['wday'] === 5 ) {
  if ( date('H') > 19 ) { exit; }; // This is for 5 Friday!!!
}
if ( $mydate['wday'] === 6 ) {
  exit;  // This is for 6 Saturday //
}

最佳答案

这应该做同样的事情:

now := time.Now()
day := now.Weekday()
hr  := now.Hour()

if day == 0 {
    if hr < 15 { os.Exit(0) }
} 
if day == 5 {
    if hr > 19 { os.Exit(0) }
}
if day == 6 {
    os.Exit(0)
}

类似地,每一天都可以用一个整数 (0 - 6) 表示。

请注意,要使用timeos,您需要调用

import "time"
import "os"

参见 the documentation有关 Golang 时间的更多信息。

关于go - 允许从周日到周五运行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43835216/

相关文章:

ruby - 如何将时间字符串解析为特定时区?

24 小时格式的 iOS 时间

python - 在类中实现微调器类型对象作为子进程

go - 如何创建交互式命令执行

go - 如何在 golang 中声明接受任何类型值的 map 参数?

go - 无法在 GoLand 中运行测试功能 : cannot find package "."

unit-testing - 无法在 golang : "unable to open database file [recovered]"-error 中打开 SQLite 数据库

c# - 使用 Noda Time 将奥尔森时区 ID 转换为 Windows 标准格式

python - 使用控制台在 python Windows 中创建进程

variables - VHDL:变量和过程