JavaScript,Python 不同语言下的时区获取问题

JS web端时区相关问题

 1 // 获取当前UTC时间
 2 function get_utctime()
 3 {
 4     var d = new Date();
 5     console.log(d.getTimezoneOffset()) ; // 获取时区偏移,分钟
 6     console.log(d.getTime()); // 获取时间戳,毫秒为单位
 7     return (d.getTimezoneOffset()*60*1000+d.getTime()); // UTC时间 毫秒
 8 }
 9 
10 // 设置任意时区
11 function set_zone_time(zone)
12 {
13     var time = get_utctime()+zone*60*60*1000;
14     return new Date(time);  // 返回时间对象
15 }
16 
17 // 对Date的扩展,将 Date 转化为指定格式的String
18 // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
19 // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
20 // 例子: 
21 // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
22 // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
23 Date.prototype.Format = function (fmt) { //author: meizz 
24     var o = {
25         "M+": this.getMonth() + 1, //月份 
26         "d+": this.getDate(), //
27         "h+": this.getHours(), //小时 
28         "m+": this.getMinutes(), //
29         "s+": this.getSeconds(), //
30         "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
31         "S": this.getMilliseconds() //毫秒 
32     };
33     if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
34     for (var k in o)
35     if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
36     return fmt;
37 }
38 
39 console.log(set_zone_time(8).Format('MM-dd-yyyy hh:mm:ss S')); // 东八区的时间
40 console.log(set_zone_time(9).Format('MM-dd-yyyy hh:mm:ss S')); // 东九区的时间
41 // 获取当前UTC时间
42 function get_utctime()
43 {
44     var d = new Date();
45     console.log(d.getTimezoneOffset()) ; // 获取时区偏移,分钟
46     console.log(d.getTime()); // 获取时间戳,毫秒为单位
47     return (d.getTimezoneOffset()*60*1000+d.getTime()); // UTC时间 毫秒
48 }
49 
50 // 设置任意时区
51 function set_zone_time(zone)
52 {
53     var time = get_utctime()+zone*60*60*1000;
54     return new Date(time);  // 返回时间对象
55 }
56 
57 // 对Date的扩展,将 Date 转化为指定格式的String
58 // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
59 // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
60 // 例子: 
61 // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
62 // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
63 Date.prototype.Format = function (fmt) { //author: meizz 
64     var o = {
65         "M+": this.getMonth() + 1, //月份 
66         "d+": this.getDate(), //
67         "h+": this.getHours(), //小时 
68         "m+": this.getMinutes(), //
69         "s+": this.getSeconds(), //
70         "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
71         "S": this.getMilliseconds() //毫秒 
72     };
73     if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
74     for (var k in o)
75     if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
76     return fmt;
77 }
78 
79 console.log(set_zone_time(8).Format('MM-dd-yyyy hh:mm:ss S')); // 东八区的时间
80 console.log(set_zone_time(9).Format('MM-dd-yyyy hh:mm:ss S')); // 东九区的时间

实际上JavaScript中有专门的函数可以获取到UTC(世界协调时)时间(如下图),但是上述代码有助于理解时间戳的意思。即最初时间和现在时间的差值,无论时区怎么变化,差值总是不会变化的。也就是说,时间戳本身是不带有时区信息的。

Python时区相关问题

对于Python来说,有time库,专门用来处理时间相关的问题。

 

 

 其中,time.gmtime() 可以将时间戳转换成时间对象。time.timezone 属性是时区信息,秒为单位。time.time() 可以获取到当前的时间。

值得注意的是,time.time() 获取到的本身就是UTC时间。如果需要获取当前时区的时间对象,需要time.localtime([时间戳])可以将送入的时间戳转成时间对象。

但是,注意,这里的time.localtime() 返回的时间对象是带有时区信息的。即:送入的时间戳后,会自行转成笔者所在的东8区时间。(而且,经过在Win10系统中的测试,虽然电脑的时区改变了,但是仍然返回的是东8区的时间,不知道是什么机制?

 

1 # 转换时间为某时区的时间
2 # 单位粒度是秒
3 def set_zone_time(zone):
4     return return time.strftime('%m-%d-%Y %H',time.gmtime(time.time()+zone*60*60))

 参考文献

[1]. JS 时区时间转换 - 青天诀的专栏 - CSDN博客

posted @ 2019-11-05 11:28  缘起花渊  阅读(379)  评论(0编辑  收藏  举报