首页 \ 问答 \ 在postgres中获取具有时区偏移量的日期(Getting date with timezone offset)

在postgres中获取具有时区偏移量的日期(Getting date with timezone offset)

我试图从postgres中的查询中提取日期。 下面的例子显示了我想要做的事情。 时间戳存储为UTC,所以如果我有1/1/2014 02:00:00,我希望在太平洋时间的日期为2013年12月31日,而不是2014年1月1日。 我非常接近,但查询2和3仍然返回1/1/2014。

SELECT '1-1-2014 02:00:00'::timestamp at time zone 'America/Los_Angeles';

回报

2014-01-01 02:00:00-08

-

SELECT CAST('1-1-2014 02:00:00'::timestamp at time zone 'America/Los_Angeles' AS Date);

回报

2014-01-01

但我希望它能在2013-12-31回归

SELECT CAST('1-1-2014 00:02:00'::timestamp at time zone 'America/Los_Angeles' AS Date) at time zone 'America/Los_Angeles';

回报

2014-01-01 00:00:00

但我想要它返回2013-12-31 00:00:00

所以,我基本上想要返回它所在时区的日期,在这个例子中是太平洋时区。


I am trying to extract the date from a query in postgres. The timestamp is stored as UTC, so if I have 1/1/2014 02:00:00, I want the date in pacific time, to be 12/31/2013, not 1/1/2014. I am really close, but both query 2 and 3 still return 1/1/2014.

SELECT '1-1-2014 02:00:00'::timestamp at time zone 'America/Los_Angeles';

returns

2014-01-01 02:00:00-08

-

SELECT CAST('1-1-2014 02:00:00'::timestamp at time zone 'America/Los_Angeles' AS Date);

returns

2014-01-01

but I want it to return 2013-12-31.

SELECT CAST('1-1-2014 00:02:00'::timestamp at time zone 'America/Los_Angeles' AS Date) at time zone 'America/Los_Angeles';

returns

2014-01-01 00:00:00

but I want it to return 2013-12-31 00:00:00

I basically want to return the date in the timezone it is in, in this case the pacific timezone.


原文:https://stackoverflow.com/questions/22996959
更新时间:2023-03-18 08:03

最满意答案

不可以。推理参见以下FAQ:

常见问题:我可以停止对我未使用的变量/导入的投诉吗?

未使用的变量可能表示一个错误,而未使用的导入只会减慢编译速度,随着程序逐渐累积代码和程序员,这种效应可能会变得很大。 出于这些原因,Go拒绝使用未使用的变量或导入来编译程序,交换短期便利以实现长期的编译速度和程序清晰度。

尽管如此,在开发代码时,通常会暂时创建这些情况,并且在程序编译之前必须将它们编辑出来可能很烦人。

一些人已经要求编译器选项关闭这些检查或至少将它们减少到警告。 不过,这样的选项还没有被添加,因为编译器选项不应该影响语言的语义,因为Go编译器不会报告警告,而只会报告阻止编译的错误。

没有警告有两个原因。 首先,如果值得投诉,值得在代码中修复。 (如果不值得修复,则不值得一提)。其次,让编译器生成警告会鼓励实现警告有关可能导致编译噪声的弱情况,从而掩盖应该修复的实际错误。

但是,解决这种情况很容易。 在开发过程中使用空白标识符让未使用的东西持续存在。

你可能要做的是在临时想要排除某些东西时使用空白标识符,例如

import (
    "fmt"
    _ "time"  // This will make the compiler stop complaining
)

现在,大多数Go程序员使用工具goimports ,它会自动重写Go源文件以具有正确的导入,从而在实践中消除未使用的导入问题。 这个程序很容易连接到大多数编辑器,以便在编写Go源文件时自动运行。


No. For reasoning see the following FAQ:

FAQ: Can I stop these complaints about my unused variable/import?

The presence of an unused variable may indicate a bug, while unused imports just slow down compilation, an effect that can become substantial as a program accumulates code and programmers over time. For these reasons, Go refuses to compile programs with unused variables or imports, trading short-term convenience for long-term build speed and program clarity.

Still, when developing code, it's common to create these situations temporarily and it can be annoying to have to edit them out before the program will compile.

Some have asked for a compiler option to turn those checks off or at least reduce them to warnings. Such an option has not been added, though, because compiler options should not affect the semantics of the language and because the Go compiler does not report warnings, only errors that prevent compilation.

There are two reasons for having no warnings. First, if it's worth complaining about, it's worth fixing in the code. (And if it's not worth fixing, it's not worth mentioning.) Second, having the compiler generate warnings encourages the implementation to warn about weak cases that can make compilation noisy, masking real errors that should be fixed.

It's easy to address the situation, though. Use the blank identifier to let unused things persist while you're developing.

What you may do is use the blank identifier when temporarily want to exclude something, e.g.

import (
    "fmt"
    _ "time"  // This will make the compiler stop complaining
)

Nowadays, most Go programmers use a tool, goimports, which automatically rewrites a Go source file to have the correct imports, eliminating the unused imports issue in practice. This program is easily connected to most editors to run automatically when a Go source file is written.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)