ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

时间和时区问题 – 译学馆
未登陆,请登陆后再发表信息
最新评论 (0)
播放视频

时间和时区问题

The Problem with Time & Timezones - Computerphile

迟早每个程序员都得解决时区问题
Sooner or later, every programmer has to deal with time zones.
我在此实在不能提供很多建议
And I can’t really offer much advice here;
但我可以提供一个警示故事
I can offer a cautionary tale.
我可以告诉你 如果可能的话
I can tell you why you really should never,
你真的应该永远避免遇到时区问题
ever deal with time zones if you can help it.
假如有人写了一个程序
Let’s imagine that someone has built an application
用来计算过了多少秒
that lets you calculate how many seconds something is in the past.
你输入日期和具体时间
You type in a date and a time,
它就会计算出秒数
it gives you the number of seconds.
大家就会看着那个程序心里想:
And they look at that and think
好吧 这个程序还挺有用
“OK, that kind of works for me…”
让咱们来设计一个改变时区的小程序
“But let’s add a little box that lets you change the time zone”
你就能将纽约的现在时间和5天前作比较
So if you’re comparing between now in New York and five days ago
将纽约换成伦敦也行
in London, you can work that out.
都没问题
And that’s fine –
下拉选项能让你根据格林威治时间
the little drop down lets you change
来定位你所在地区的时间
which hour forward or backward of Greenwich you are.
太棒了
Brilliant.
早晚这个程序会小小地火一把
Sooner or later, after it gets a bit popular,
这时相关人员就会收到澳洲来电
they’ll get a call from Australia.
澳大利亚说 我就不模仿澳洲口音了
And Australia will say”G’day ma–“
我不准备模仿澳洲口音
— I’m not gonna try and do accents.
这不是重点
I just shouldn’t do accents.
澳大利亚会说:你好
Australia will say “Hello!”
我想说的是
“By the way,
我们的时间比格林威治时间提前9.5个小时
we’re 9½ hours ahead of Greenwich.”
然后程序员说:是这样啊?
And the programmer will go “really?”
是啊 快了9.5个小时呢
“Yeah, 9½ hours.”
好吧 我把这个个例记下来 交给我吧
“OK, I’ll add a special case for you. That’s fine.”
没多久 尼泊尔又有人打来了
A little bit later, someone will call from Nepal.
他们说:你好
And they’ll say “Hello.
我们的时间比格林威治时间快5.25个小时
We’re 5¼ hours ahead of Greenwich.”
程序员问:真的吗?
And they’ll say “really?”
是啊 好几年都这样了 快了5.25个小时
Yeah we’ve been that for ages! Yeah 5¼ hours!”
好吧 尼泊尔也被加入了特例
Great. Okay. And they’ll put in a special case.
然后 程序员去查了时区表
And maybe they’ll look up the list of time zones.
这个权威的表格告诉他们到底发生了什么
The Canonical list that tells you what everything is,
然后他们会确认一下
and they’ll make sure
该程序是否覆盖了世界上的所有时区
they’ve covered every time zone in the world.
然后秋天来了
And then Autumn will come along.
程序员收到了来自英国的电话
And they’ll get a call from England.
他说:你好
And England will say “Excuse me.
请问我们现在的时间为什么慢了1个小时?
Um, we’re an hour out at the minute, what’s going on?”
是这样的 时间改变了
“Hold on the clock’s just changed!”
不要紧 我们马上处理
“That’s fine – no, we dealt with that.”
我们会记下夏令时的变化 然后录入
“We made a note of when daylight savings changes for us, and we’ve put that in!”
英国人就回答:不
And England will say “No,
我们的夏令时一周前就实施了
see daylight savings changes a week earlier for us.”
具体时间每个地区都不一样
“It’s different depending on where you live.”
一周前我们就自己把时钟调了
“We shift our clocks back a week before you do.”
一般这时候
And at that point
程序员就该
generally the programmer will start to
抱头叹息了
hold their head in their hands
认识到自作孽不可活
and realise what they’ve gotten themselves into.
不过也有解决的办法
And that’s fine!
他们会录入那些个例
They’ll put that in and they will deal
并挨个将这些国家的夏今时调整成不同的日子
with each country shifting to daylight savings time on a different day.
他们会看一下档案找找解决的办法
They’ll look at the file that tells them how
然后比照着这个方法解决问题
to do that and they’ll copy all that in.
然后程序员会再次收到
Then they’ll get a call from someone
南半球的来电 来电说:
in the southern hemisphere again who will say
是啊 我们的秋天没有推迟 我们前移了
“Yeah we’re not shifting back in the Autumn we shift forward.”
我们的春天11月就开始了
“Our spring… is in November.”
那时候程序员通常会
At that point they’ll generally
开始醉心于各种美酒
start looking longingly at their intoxicant of choice
他们觉得应该在一切变得更糟之前快点喝醉
and wondering whether they should have a quick drink before keeping going…
最后又默默编起码来
…and they’ll code that in as well.
后来 程序员又收到萨摩亚的来电
Then they’ll get a call from Samoa.
该地位于太平洋中的国际日界线上
Out in the Pacific on the international date line.
萨摩亚说:你好
And Samoa will say “Hello!”
我们“丢掉”一天
“Yeah we skipped a day the other year.”
程序员说:啥?
And the programmer will say “what?”
对 我们略过了一天
“Yeah we skipped a day.
直接从12月29日跳到12月31日
We went from December 29th, 2011 to December 31st.”
我们直接挪到了国际日界线的另一边
“We shifted from one side of the international date line
从落后于格林威治时间很久
from being hours and hours behind Greenwich…
到超出格林威治时间很久
to being hours and hours ahead of Greenwich.
这倒有利于我们和澳大利亚的贸易往来
Helps us with trading with Australia.”
你能算一下吗?
“Can you take account of that when you work out
算一下过了多少天
how many days things are,
过了多少秒
and how many seconds things are in the past?”
好了
That’s fine,
这些只是国家改变时区的一些例子
there’s a file that tells you when any country has changed its time zone.
但实际上这种事情经常发生
It turns out that that happens fairly often.
不过他们提前公布了时间表
But they’re announced ahead of schedule so as long
所以只要你保持更新文件
as you keep that file updated
并将之编码进你的程序逻辑
and code that into your program’s logic as well,
就不会出现问题
it’ll be fine.
然后我们往回看
Then you look back and you notice
你会发现二战期间
that during World War II,
英国有双重夏令时
England had double British Summer Time.
它完全纳入了英国夏令时
It went completely onto BST
然后就多出了一个小时
and then just added an extra hour.
所以英国比格林威治时间提前了2个小时
So it was two hours ahead of Greenwich despite… having Greenwich.
好吧 你任性你有理
That’s fine you deal with that.
你注意到我说话时的代入感很强了吗?
Have you noticed I’ve started talking as if it was you or me?
因为我之前干过这事
Because I’ve done this before
真的真的很让人沮丧
and it’s really, really frustrating.
当一些国家准备改变时区时
And you make sure you subscribe to the list
你得确保要将其提交到列表里
of when countries are going to change their time zones –
很显然这种事发生了许多次了
which happens, apparently, many times!
比如 一些政府在一年内改了好几次时间
Like, sometimes, several times in a year because governments change over.
然后我们悲催的程序员收到利比亚的来电
And then this mythical programmer gets a call from Libya;
因为在2013年 经过几天的商议
who in 2013, with only a couple of
利比亚决定
days’ notice, decided that they weren’t
不把时钟往回调
going to put the clocks back.
所以如果没有足够的关注
With enough notice that it wasn’t possible
想要大家都准时更新时间是不可能的
for anyone to get the update out in time.
我是说 每台利比亚电脑
Meaning that every Libyan computer,
不管它用的是什么操作系统
no matter what operating system it ran… …
都会滞后1个小时
was an hour out.
好吧 你读了相关新闻文章
That’s OK, you read the news article
然后匆忙将其编入代码
about that and you hurriedly code that in as well.
然后你收到了来自约旦河西岸的来电
And then, then you get a call from the West Bank.
那里的以色列人和巴勒斯坦人
Where the Israeli population is on a different
用的时区不一样
time zone to the Palestinian population.
一个用的是以色列时间 一个不是
Because one is following Israel and one isn’t.
现在的情况是
Now you have two populations
同一地点的不同人口使用不同的时区
of people in the same location who are following different time zones.
当地人也很懵逼:
And now they’re all having to ask themselves whether
我们是在这个时区呢 还是那个
they’re on this time zone or this one,
应该是根据他们是谁 他们所处的位置判断
depending on who they are and where they are
可是这些根本没法编进程序啊
And there’s no way to code that into your program!
然后程序员收到历史学家的来电
And then… Then you get a call from the historian.
他说: 现在
Who says “Right,
我试着从18世纪开始来推算时间
I’m trying to calculate some times back in the 18th century.”
当时我们从罗马儒略历改成了格里高利历
“And we changed from the Julian calendar to the Gregorian calendar.”
关键不是我们丢失了3个星期
“It’s not that we lost about three weeks… …
而是我们直接从一天跳到了另一天
it’s just that we skipped right from this date to this date,
省略了中间的日子
and missed the others.”
你能编一下码 帮我找找逻辑吗?
“Can you code it so that it just kind of works that out for me?”
这个问题可以解决
It’s fine
因为你知道他们的日期
because someone has already told you when those dates are,
你可以将之编进你的程序
and you can code that into your program’s logic as well
但是代码将很长很复杂
but now it’s looking really, long and really complicated
还要用到杂乱的套管程序
and it’s a tangled mess of spaghetti code that somehow works
然后俄罗斯历史学家又给你打电话了
and then you get a call from the Russian historian!
他说: 我们20世纪才开始用格里高利历
Who says”Yeah, we only changed to the Gregorian calendar in the 20th century… …
而中间省略多少日子
and it turns out the dates
要视具体的地理位置而定
you’ve skipped change depending on your location… …
你能解决这个问题吗?
and can you deal with that as well?”
然后你收到英国的历史学家的来电
And then you get a call from the British historian!
他说:我觉得在16世纪时
Who says that until, I think it was the 16th century…
新年那一天是3月25日
the year started on the March 25th!
然后程序员就开始头脑风暴了
Just to blow your mind there.
924年3月24日之后
From March 24th, 924
紧接着就是925年3月25日
and then it would be March 25th, 925.
一个接着一个
And that is the next day.
那么按照这个逻辑顺序 你从924年12月31日
Because you have gone from December 31st, 924, to January 1st,
进入了924年1月1日
924 because it goes in that order!
简直太复杂了 但是…
It’s massively complicated, AND THEN…
天体物理学家又找来了
You get the call from the astrophysicist…
他说:提醒你一下 别忘了“闰秒”问题
Who says”By the way, we just had a leap second!”
这时候你就震惊了:什嘛?!
And at this point, you just kind of go”… what?!”
闰秒 因为地球不是恒速转动
Leap seconds; because the Earth does not rotate at a constant speed.
一会儿慢 一会儿快
It slows down, it speeds up,
就像地壳构造板块和磁场的移动之类的
as tectonic plates move about and magnetic fields shift, or something like that.
所以时不时地
And so occasionally
国际天文联合会将决定
the International Astronomical Union
我们到底要不要来个闰秒调整
will work out whether we need a leap second.
如果需要的话 假如现在是23:59:58
If you do, the clocks go 23:59:58
下一秒就是23:59:59
and then it’s 23:59:59
但是时区混乱了 下一个不是00:00:00
and then instead of going [00:00:00] like any sensible time zone would
而是23:59:60
it goes 23:59:60!
一切都乱了 因为1分钟突然变成了61秒
And everything breaks because suddenly you have 61 seconds in a minute!
所以我们要用到一个工具
So depending on your implementation, either… …
当你的时钟慢或快了一秒钟
your clock gets one second
和世界上的其它地方不同步
out of sync with the rest of the world
比如说它重复了1秒
or it repeats a second.
你能解决这件事的方式就是
The way you’re meant to deal with this is
用一个叫做“Unix时间戳”的工具
with something called the Unix Timestamp.
我觉得“数字狂”这个软件也有此类问题
And Numberphile, I think, has talked about this before.
你有一个开始数字
That you have this number that started
就是1970年的第1秒
on the first exact second of 1970
然后每秒增加一秒 从不间断
and increments one second, per second. Constantly.
嘀嗒 嘀嗒 嘀嗒
Tick. Tick. Tick.
这很好啊 因为你要做的就是
And that’s great because what you’re meant to
拿着这个已知日期
do is take whatever date has been given to you,
把它输入“Unix时间戳”中计算一下
and you calculate that as a Unix Timestamp,
然后把计算结果放进你的数据库里
and you put that into your database.
这样就解决了闰秒问题
And that’ll just deal with leap seconds
不过你不必 多此一举
EXCEPT IT DOESN’T! Of course it doesn’t!
因为有一种东西叫“世界标准时间”
Because you have Universal Coordinated Time
它负责实施“闰秒”
which includes leap seconds!
闰秒时不时会来一次
It repeats occasionally,
就包括23:59:60这种形式的时间
and it just includes 23:59:60
还有“天文时间”
and then you have astronomical time!
它不包括闰秒
Which does not include leap seconds
它非常准确
and has steadily been getting out
和世界上的地区都不同步
of sync with the rest of the world
因为我们需要根据天文时间看天象
because we need to look at the stars
设计望远镜
and design telescopes around it!

[deep sigh]
你知道…
And what you learn…
你知道解决完时区问题
What you learn after dealing with time zones,
你就…
is that what you do… …
你可能永远都不想写代码了
is you put away your code… …
你不再试图编写关于时间的程序
you don’t try and write anything to deal with this.
你看着这个领域的先贤
You look at the people who have been there before you.
你看着领头人
You look at the first people,
他之前解决过这个问题
the people who have dealt with this before,
他建立了套管程序
the people who have built the spaghetti code,
你非常感谢他公开了自己的成果
and you thank them very much for making it open source,
你给他们钱
and you give them credit,
拿走他们的成果 放进你的程序里
and you take what they have made and you put it in your program,
然后你就再也不用纠结了
and you never ever look at it again.
因为这世界上的时间都疯了
Because that way lies madness.
谷歌确实有一个
Google actually has a really,
真正的解决闰秒问题的好方法
really good approach to leap seconds that
这是谷歌独创的
they invented themselves.
网络博客中就有一篇这方面的文章
There’s an article about it on their blog,
我觉得 说得有道理
I think, that explains it.
他们采取“闰秒弥补”技术
They do something called a”leap smear.”
因为1分钟有60秒
Because having 61 seconds in a minute,
如果钟表的秒针慢了1秒
or because having a clock tick back a second,
将给大机构带来很坏的影响
can be really, really bad for massive agencies that have to
因为他们得非常准确地同步所有数据
synchronise everything really precisely
就得相信那1秒内储存的资料
and have to trust that one bit of data was stored before another,
实际上 谷歌将这1秒钟分解了
they essentially smear the second out over the whole day.
他们每次都弥补1微秒的时间
They increase their clock by a microsecond at a time.
滴答滴嗒嘀嗒 一直弥补到1秒钟
Tick tick tick, all the way through the day.
也就是说
So that it’s,
有时候可能现实中半秒钟的时间
sometimes maybe even half a second out from reality,
同样的半秒钟 在服务器上就是一切
but as long as everything on their servers is half a second out.
“闰秒弥补”技术建立在世界时间的不同步上
It’s built to be out of sync with the world
它让你知道此刻时间确实在流逝着
as long as it knows that one thing happened before another.
因为时间的持续性比精确性更重要
Having continuity is more important than actually having accurate time.

发表评论

译制信息
视频概述

程序员在编程中不可避免要遇到的时间和时区问题。

听录译者

收集自网络

翻译译者

ABC

审核员

审核员 LD

视频来源

https://www.youtube.com/watch?v=-5wpm-gesOY

相关推荐