在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
最满意答案
不可以。推理参见以下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.
相关问答
更多-
__unused标志行为/用法(GCC with Objective C)(__unused Flag Behavior/Usage (GCC with Objective-C))[2024-01-04]
__unused宏(实际上扩展为__attribute__((unused)) GCC属性)只告诉编译器“如果我不使用这个变量,不要警告我”。 unused :该属性附加到变量,意味着该变量可能未被使用。 GCC不会为这个变量产生警告。 ( 来源:gnu.gcc.org doc ) 所以这个GCC属性是为了避免在不使用变量时发出警告 ,而在使用你声称未使用的变量时不要触发 警告 。 至于在你的最后一个例子中,在变量名之前或之后放置属性,两者都被接受并且在你的情况下是等价的:为了兼容性目的,编译器对该位置只是 ... -
__unused属性旨在防止在对函数/方法或函数/方法的参数未使用时发生投诉,而不是强制其缺少使用。 GCC手册中使用的术语是: 该属性附加到函数,意味着该函数可能未被使用 对于变量 : 该属性附加到变量上,意味着该变量可能未被使用。 最常见的用途是针对界面进行开发 - 例如回调,您可能会被迫接受多个参数,但不能使用所有参数。 当我进行测试驱动开发时,我会使用它 - 我的初始例程需要一些参数并且什么也不做,所以所有参数都使用__attribute__((unused)) 。 在我开发它时,我使用了这些参数。 ...
-
刚找到的方式。 右键单击所需的包,然后单击Source-> Organize Imports。 I just found the way. Right click on the desired package then Source -> Organize Imports. Shortcut keys: Windows: Ctrl + Shift + O Mac: Cmd + Shift + O
-
任何理由清理Java中的未使用进口,除了减少混乱?(Any reason to clean up unused imports in Java, other than reducing clutter?)[2023-09-09]
如果您不删除导入,我认为不会出现性能问题或类似情况。 但是在很少的情况下可能会导致命名冲突,例如导入列表界面。 在Eclipse中,您可以随时使用快捷方式(取决于操作系统 - Win: Ctrl + SHIFT + O和Mac: COMMAND + SHIFT + O )来组织导入。 Eclipse然后清理导入部分删除所有陈旧的导入等。如果您需要再次导入的东西,eclipse将在您使用Ctrl + SPACE完成语句时自动添加它们。 所以没有必要在你的类中保留未使用的代码。 一如以往一直未使用的代码会分散您 ... -
使用Eclipse,选择Project-> Properties,然后选择Java Editor-> Save Actions,选中“Additional actions”,点击“Configure”,然后在“Unnecessary code”下面,一定要选择“Remove unused imports” Using Eclipse, select Project->Properties then select Java Editor->Save Actions, check "Additional act ...
-
有没有办法标记编译器忽略未使用的导入?(Is there a way to flag compiler to ignore unused imports? [duplicate])[2023-12-29]
不可以。推理参见以下FAQ: 常见问题:我可以停止对我未使用的变量/导入的投诉吗? 未使用的变量可能表示一个错误,而未使用的导入只会减慢编译速度,随着程序逐渐累积代码和程序员,这种效应可能会变得很大。 出于这些原因,Go拒绝使用未使用的变量或导入来编译程序,交换短期便利以实现长期的编译速度和程序清晰度。 尽管如此,在开发代码时,通常会暂时创建这些情况,并且在程序编译之前必须将它们编辑出来可能很烦人。 一些人已经要求编译器选项关闭这些检查或至少将它们减少到警告。 不过,这样的选项还没有被添加,因为编译器选项不 ... -
我倾向于认为这是编译器中的一个错误。 它仍然存在于g ++ 4.7.1中。 作为一种解决方法,您可以尝试定义一个什么都不做的构造函数。 它用g ++ 4.7.1抑制警告,我不知道用3.4.6。 I'd tend to think it is a bug in the compiler. It is still present in g++ 4.7.1. As a work around, you may try to define a constructor which does nothing. It s ...
-
java中的未使用的导入会影响性能并消耗内存吗?(Do unused imports in java effects performance and consume memory? [duplicate])[2021-12-17]
不,导入是编译时功能(它们在编译代码中没有意义)。 它们不会影响运行时行为。 No, imports are a compile-time feature (they have no meaning in the compiled code). They do not affect runtime behavior. -
未使用的基元数组:javac和JIT编译器对它做了什么?(Unused array of primitives: what do javac and the JIT compiler do with it?)[2022-01-23]
Java微基准测试很难。 但是,快速查看frobincate的字节码表明javac(OpenJDK 7)不会忽略数组... Compiled from "Frobincate.java" public class Frobincate { public Frobincate(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."... -
C编译器标志忽略符号(C compiler flag to ignore sign)[2023-08-07]
实际上char , signed char和unsigned char是三种不同的类型。 从标准(ISO / IEC 9899:1990): 6.1.2.5类型 ... char , signed char和unsigned char这三种类型统称为字符类型 。 (例如,在C ++中,如果你有一个char参数,你必须(或者至少应该)用它们的三个变体来编写覆盖函数) 普通字符可能被编译器处理为有符号或无符号,但标准说明(也在6.1.2.5中): 声明为char类型的对象足够大,可以存储基本执行字符集的任何成员 ...