我觉得在任何地方我都看到过编程中与时间相关的算法,GMT是基准时间.例如,我被告知总是将时间存储在GMT + 00的数据库中,以便时区更改不会中断任何事情.
我是对的,GMT似乎是软件开发的基准时区吗?
如果是这样,为什么不UTC?考虑到甚至Unix时间戳都是从UTC定义的(http://en.wikipedia.org/wiki/Unix_time),为什么说"UTC + 01"而不是"GMT + 01"并不常见
我正在尝试将Javascript API调用转换为Python.工作的JavaScript代码工作正常,生成这样的时间戳:
var curdate = new Date();
var gmtstring = curdate.toGMTString();
var utc = Date.parse(gmtstring) / 1000;
Run Code Online (Sandbox Code Playgroud)
随后在API调用中对此结果(自纪元以来的这个秒数)进行散列并使用,但这是相关部分.如果有人能让我知道转换它的正确方法,那将非常感激.
以下是不同方法的不同结果的一些细节:
var curdate = new Date(2013, 7, 10);
var gmtstring = curdate.toGMTString();
var utc = Date.parse(gmtstring) / 1000;
Run Code Online (Sandbox Code Playgroud)
结果:1376089200
from datetime import datetime
import calendar
d = datetime(2013, 8, 10)
calendar.timegm(d.utctimetuple())
Run Code Online (Sandbox Code Playgroud)
结果:1376092800
我显然遗漏了一些东西,有人可以启发我吗?
我最初在我的例子中犯了一个错误,因为Javascript使用0基于日期而Python的日期是从1开始的.
Jonathon亲切地解释了由于Python违约到UTC而在值中的差异是不同的,因为Javascript默认为本地时区.就我而言,这是GMT,这是API所要求的.我现在只需要在Python中获得此结果.
解决方案是提供的时区不匹配.虽然我仍然遇到第三方api的问题,但我至少现在正在接受正确的时间.
这可能是清理过来的:
from datetime import datetime
import calendar
import time
import pytz
def GenerateTimeStamp(d):
europe = pytz.timezone('Europe/London')
d = europe.localize(d)
tuple = d.utctimetuple() …Run Code Online (Sandbox Code Playgroud) 我的日期时间值为2016-12-21T07:48:36,偏移量为UTC + 14.如何将日期时间转换为等效的标准GMT时间.
我尝试使用sampleDateFormat.parse()method.But,我无法获得UTC偏移量的TimeZone对象.
sampleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC+14:00"))
Run Code Online (Sandbox Code Playgroud)
请帮我在Java 7中将UTC日期时间转换为标准GMT时间.
我有一个包含这样的数据的SQL表:
| theDate (datetime) | theValue (int) |
----------------------------------------
| 2010-05-17 02:21:10 | 5 |
| 2009-03-12 04:11:35 | 23 |
| 2010-02-19 18:16:53 | 52 |
| 2008-07-07 22:54:11 | 30 |
Run Code Online (Sandbox Code Playgroud)
日期以UTC格式存储在日期时间列中,如何将它们转换为当地时间(挪威)?请记住,由于冬季/夏季,UTC全年的UTC偏移量并不相同.
更新:我正在使用Microsoft SQL Server 2005,我需要从SQL执行此操作,因为数据将在稍后显示给报告.
我是Java的新手,我对使用SimpleDateFormat和使用时有点困惑Calendar.我有一个Date-Object,想要提取GMT日期字符串yyyy-MM-dd HH:mm:ss.我住在德国,目前我们是GMT +0200.我的Date-Object的时间就是例如2011-07-18 13:00:00.我现在需要的是2011-07-18 11:00:00.我的时区的偏移量应自动计算.
我试过这样的事情,但我想某处有一个错误:
private String toGmtString(Date date){
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone timeZone = TimeZone.getDefault();
Calendar cal = Calendar.getInstance(new SimpleTimeZone(timeZone.getOffset(date.getTime()), "GMT"));
sd.setCalendar(cal);
return sd.format(date);
}
Run Code Online (Sandbox Code Playgroud)
在某些设备上,datetring会像我想要的那样返回.在其他设备上,偏移量未正确计算,我从输入日期 - 对象接收日期和时间.你能给我一些提示或建议吗?我想我的方式获得默认时区不起作用?
我的数据库出了问题.我从未习惯在PostgreSQL中工作,我想从UTC日期时间字段中选择并获得GMT日期时间作为结果.
其实我的要求是:从位置选择dateheure应该是Postgresql请求做我想做的事情???
非常感谢Gwenael
我有一个Grails应用程序,代码如下:
Date now = Calendar.getInstance().getTime() //Also tried new Date()
println "now: " + now
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我明白了now: Thu Aug 18 12:47:09 CDT 2011.我需要将日期设置为GMT,而不是当地时间,因为我需要将GMT时间存储在数据库中.我可以使用simpleDateFormat对象在GMT中打印出时间,但我需要将其实际存储为GMT.
问题:如何使用GMT将Date对象转换为Date对象?
Emacs与系统时间相比减少了两个小时。我试图用谷歌搜索问题,但没有运气。我需要配置什么来纠正此问题?我怀疑这与GMT到我居住的地方不同(我位于GMT + 2区域,也就是说,如果我从系统时间2中减去,我将获得Emacs中的时间)。所以...也许是某些语言环境设置?
因此,我只是弄乱了一个git存储库:通过magit用过的Emacs时间进行的提交,并将它们放在其他人进行的提交之前:(

在这里,我添加了一个截图,显示了区别。的输出date是正确的时间,但是在Modeline条纹上的时间是错误的。
编辑0:
似乎Stefan是正确的,并且Git中的时间未与Emacs中的时间相关(下面的屏幕快照来自Cygwin终端)。
这个问题与Git以及Emacs都息息相关-某种程度上,他们使用的系统API在我的PC上不同步-这是我需要进行设置以使其与它们对齐的问题。问题是两者都使用什么设置?

编辑1:
这是Emacs用来获取时间afaik的代码:
/* Emulate gettimeofday (Ulrich Leodolter, 1/11/95). */
int
gettimeofday (struct timeval *__restrict tv, struct timezone *__restrict tz)
{
struct _timeb tb;
_ftime (&tb);
tv->tv_sec = tb.time;
tv->tv_usec = tb.millitm * 1000L;
/* Implementation note: _ftime sometimes doesn't update the dstflag
according to the new timezone when the system timezone is
changed. We could fix that by using GetSystemTime and
GetTimeZoneInformation, but that doesn't seem necessary, since …Run Code Online (Sandbox Code Playgroud) 我开发了一个应用程序,它记录了某些记录何时被修改和创建,所以基本上我们使用该time()函数记录保存更改的时间.
我在英国,所以我的时区必须是GMT.然而在英国,我们使用夏令时,所以在夏天我们不再使用GMT而是使用BST.
如何更改使用BST的时区(即GMT +1).我想在我的php文件中声明它,以便它很容易更改.这就是我现在所拥有的:
date_default_timezone_set("UTC");
Run Code Online (Sandbox Code Playgroud)
当我将其更改为:
date_default_timezone_set("BST");
Run Code Online (Sandbox Code Playgroud)
我得到一个PHP错误Timezone ID 'BST' is invalid,当我将其更改为欧洲/伦敦时,它仍然保持GMT而不是BST
这里我打印 UTC 时区的当前日期时间。我想要通过这种方法当前 GMT 时区的日期时间。我怎么能够?
import datetime
dt_utcnow = datetime.datetime.utcnow()
print(dt_utcnow)
Run Code Online (Sandbox Code Playgroud)
输出
2020-08-31 09:06:26.661323
Run Code Online (Sandbox Code Playgroud)