Category: 未分类

  • Chrome 独立安装文件(完整版)

    Firefox 越来越不给力了,我自己很久以前就转到了 Chrome 阵营,但是家里的 Windows 笔记本还是 Firefox 为默认浏览器。

    前几天老婆在淘宝上等着抢拍一件衣服,结果预定上架时间之后已经过了一个小时,页面上仍然显示“即将开始”。后来发现是 Firefox 的问题——不知道为什么,有的页面可以正常加载,但是有的页面就显示缓存的版本!我没有深究到底是什么原因造成了这个症状。

    每次打开 Firefox,开始在地址栏输入,总是要假死至少半分钟才能恢复。关掉 Firefox,进程仍然一直在后台无法退出,重新打开的时候就会提示,只有在进程管理器里杀掉。

    除了开发者,对普通用户而言,Firefox 给人的感觉变得甚至比 IE 还要臃肿、笨拙。该彻底摆脱它了。

    不过今天想安装 Chrome 的时候遇到问题了——每次安装的时候,总是卡在“正在等待下载 Chrome”这里,不能再继续。可是 Chrome 主页上并没有提供独立安装文件的下载。

    在网上找了一下,发现只需在 Chrome 主页的 URL 上加个参数 standalone=1,Google 就会将安装方式更改为下载完整版本。即此链接:

    http://www.google.com/chrome/?standalone=1

  • 天津护照办理流程(攻略)

    在我朝,跟ZF打交道就跟探险似的(可是很少能感受到乐趣),大家得多贡献攻略,造福后来人。我今天就说说天津办理护照的流程。

    一年多前,写了一篇日志记录在保定乡下老家办理护照的痛苦经历。最近,去天津办一些手续(也是跟ZF纠缠),顺路办护照,省得以后真要出去的时候麻烦。去之前就打电话咨询,问要不要“协查”,警察说户口迁入不满十年都要协查。我问要多久?他说不知道。问他根据经验呢?答曰——没经验,快的一两周就好了,慢的一两年都不行。你说得到这样的答复气人不气人,用血汗钱养着这帮孙子,让他办事的时候就尽给添麻烦!当时做计划就给办护照预留了不少时间。

    不过到了天津之后,办理护照出奇地顺利——在遭遇不下五次拒载之后,苦尽甘来,遇上一位性格开朗的老师傅,听说我们要去火车站附近的出入境管理局,就推荐我们去很近的一个地方,说是新开的,人少。开始我们还不信,觉得师傅年纪这么大了会不会弄错。后来师傅提了好几次,说他不确定能不能办护照,但他客人说可以,于是我们就听了师傅的,起步价距离就到了。停车一看,长江道出入境受理处,没错,很开心,零钱不用找了!(第一次在国内给小费……)。

    那时已经快十二点了,就怕人民公仆们累了饿了要吃饭休息。进去之后发现人真的很少,直接进去照相,领表,上二楼填表,让一个警察审核,然后在柜台办理,整个过程也就半个小时,大大出乎我的意料!下面详细说一下情况。

    地点 – 天津现在应该有四个地方可以办理护照:市公安局出入境管理局、塘沽分局出入境接待大厅、南开区长江道以及河西区利民道出入境受理点。之前只知道那个出入境管理局,在网上看好像是要排老长队,不知道现在是什么情况。但是似乎好多出租车司机一听火车站就不去。

    照相 – 电子照片的采集应该是包含在护照办理费用里的。他们照完给表的时候会问你带照片没有,没有的话交60大洋,买他们打印的照片贴在表格上,还有一张存照片的光盘!一般是稀里糊涂就上了套了——这些值60大洋?很明显这是中国特色经济,跟公权的关系造就私人的暴利。某公司可以拿这个出个面试题,问他们一年净利润是多少,偷税多少。不过大部分人也不缺这个钱,在这里花钱买省事,让少数人先富起来吧。

    协查 – 没有遇到协查这一说。我在老家办的时候还要派出所证明我清白,天津不需要。

    事由 – 我当时在乡下办理说因商务出国,人家还问我要了公司开的证明。反正在天津填的是去韩国旅游(照样本填的),无需“事由证明”。现在表格的名字也已经有原来的“因私出境申请”改成“护照申请”了,算是个小进步吧。

    取件 – 选择特快专递,他会给你说两周收到。我们一周多一点就拿到了,效率不错。

    总之护照办下来感觉天津还是个大地方,不像我老家那种落后地区。不过其他很多方面真有待改善。交通已经很差,下午三四点就开始堵。大街小巷太多,XX道,XX道,连出租车司机都不知道,地图上信息都不准,12580查到的信息也不对。

    末了来个友情提醒,看到这儿的朋友们如果还没护照赶紧去办一个,尤其户口在偏远落后地区的。我一个刚毕业的哥们,老乡,说想去缅甸玩,问他情况,竟然户口刚迁回老家,麻烦大了……这东西,等你想用的时候就后悔没早办了。

  • 骑行大运河森林公园未遂

    今天下午骑车往通州去,想看看“大运河森林公园”。沿着京通快速的辅路走,竟然有一段路边还有这么宽阔的水面:

    P1000729

    后来看辅路比较堵,我就在沿河的小路骑了一段。这段路是防汛路,车比较少,灰尘也少,不过路况比较差,有点越野的感觉。往前走,这小路和京通辅路交汇。后面在八里桥和北苑地铁站中间发生了悲剧,后胎扎了个铆钉!第二次骑远路就出这种事了。其实之前路上我就想了几次,万一轮胎扎了怎么办。用快没电的手机打开地图,搜到附近有个捷安特店,距离3.3公里。推车步行过去,打电话才知道人家搬了,又步行差不多1公里,到了九棵树地铁站,终于换了个新内胎。问了问,大运河森林公园还有点距离,天色不早了,灰溜溜回家。

    这就是罪魁祸首,长度差不多有手机那么宽了,不偏不倚从轮胎正中央扎了进去:

    P1000731

    我本以为带着这钉子还骑了几十米,肯定把内胎划得不成样子了。不过换下来之后发现也就一个眼,可以补补,以后出门带个备胎,哈哈:

    P1000730

    总起来说京通辅路还是比较好走的,自行车道很宽。偶尔有些车停着,有些车也从自行车道超车,但是情况比市里好多了。比较麻烦的是过五环的那一段,从西向东就绕圈,而且路况复杂,需要小心。从东向西似乎是根本没有正常的自行车道,立交桥下有一条黑咕隆咚的路,几个电三轮车主还在那儿乱停,坐着打牌。再往前,完全没有光让人看路了(汽车是进不来这个路的),只看到前面有亮光,知道方向。冷风吹过来——这是唯一的速度指示,这时就只能放慢速度,希望路上别有坑什么的,希望后面的车不要撞你。很恐怖,所以最好还是从从西向东的那条路逆行回去(路牌是这样指示非机动车的)。我现在也还是稀里糊涂的,有个示意图就好了。

  • 骑车从劲松到香山

    前阵子买了个入门级的山地车,捷安特欧野2.0,后来就张罗着和公司的同事们出去玩。最初定的是今天骑车去十三陵,查了一下地图,从国贸出发往返要150公里,对不常锻炼身体的我来说,强度太大了。于是后来改成了去香山。

    提前几天就开始关注天气,最初的预报是周六日都下雨,当时估计去不了了。周四的时候天气预报突然改成周六日都是好天气,开始让人兴奋了。到了周五,又变成周六日下雨了,我们商定下雨的话就取消。

    早上起来不想吃饭,就吃了一个小烧饼,出发了。带的东西有一个卡片相机,骑行手套,一瓶脉动,眼镜布,一件备用T恤。由于没有经验,车子的轮胎气都不够,虽然不会影响车子,但是阻力比较大,骑起来比较费力。

    在大望路地铁附近跟两位住东边的同事汇合,然后沿长安街一路向西,杀向XXX广场。两个同事都经常骑车,骑得比较快,我刚开始还觉得可以追赶,用力蹬,消耗了不少体力。到西三环附近跟另一位同事汇合(这位倒是轻松啊),然后往香山走。

    等到了香山脚下,腿已经基本没有力气了,稍微有个上坡,换到慢速档也坚持不了多远。最终有个同事沿防火道连骑带推上了山顶,我和另外两个大约只走了三分之一。

    等了很久看见那同事下来了,原来他的碟刹因为长时间使用,温度过高,他碰了一下把指头都烫伤了,恐怖。后来用水擦了一下,跟我们一起下山。我的车是V刹,下山之后摸轮圈也发现烫得厉害,如果是从最高处一次骑下来,估计刹车就冒烟了!下山还是很爽的,累了半天,就为了那一会快乐……

    之后骑到了西三环的香格里拉大酒店,附近的新疆餐馆用餐。吃完已经4点半了,我就靠一个烧饼坚持了一天。然后各奔东西。我还是沿长安街回来,一路不时有雨滴落到身上,我下意识地尽力提高速度,还好没有被雨淋。一整天也没心情(也没力气)掏出卡片机拍照。

    感觉不锻炼的话,骑平路还可以,稍微有个长一点的上坡,就很消耗体力,更别说香山那样的坡度了。北京的空气是够差的,大部分时间我罩着鼻子,还是吸了一鼻子黑。

    下面是今天的路线,除去上山大约往返70公里吧:

    查看大图

  • 警惕 Chrome 的查看源代码 (View Page Source) 功能

    前阵子解决一个问题的时候,差点以为是我们自己在 HTML 代码中输出的一段信息有问题,结果发现,Chrome 的 View Source Code 竟然会重新发送一个请求!

    有史以来,所有的浏览器从来没有过这样天才的设计。大家都是老老实实,既然你让我显示源代码,那我就直接给你把正在看的这个页面的源代码显示出来。没有人想过竟然可以重新发起一个请求,去拿“纯洁的”源代码。这是革命性的!Chrome 你做到了!

    早在2008年,就已经有人提出这个 bug – View source forces page reload. 中间有人将之标记过 Fixed,但是世界末日快来了,Chrome 的稳定版本已经飚到18了,市场份额已经远超 Firefox 了,实际上这个 bug 仍然存在。我的天啊,究竟是什么样的设计,导致解决这样一个问题这么难?

    甚至还曾有开发者认为 View Source 就应该是这样的行为 (链接):

    Yes, when you “view source”, you’re really opening a new tab that opens the page
    again and displays the source rather than renders the page. Many web pages are
    dynamic and modify their HTML content (eg. using JavaScript/XMLHttpRequest) so we
    cannot display the current HTML – this is why a new request is made.

    If you want to view the current state of a dynamic page, you should use the
    inspector. “File icon -> Developer -> JavaScript console”

    I don’t see how this is a security bug or a bug at all – the behavior is by design
    closing as such.

    注意里面加粗倾斜的那段话——因为很多网页是动态的,所以无法显示当前的 HTML,所以发送一个新的请求。这逻辑真够奇怪的!你是想“显示当前的 HTML”吗?你发送一个新的请求解决了这个问题吗?没有解决,那你何必要发一个新的请求?这就相当于脱了裤子,屁还是没有放出来。

  • Nothing to Envy

    Winner of the 2010 BBC Samuel Johnson prize

    What if the nightmare imagined by George Orwell in 1984 were real? 这本书真是太棒了,我读英文还不是很快,不过书中的真实故事越来越抓住人的心理,前几天晚上都看到很晚才睡。此书中有些许关联的不同人物穿插叙事的方式也没有觉得太乱,比较自然、有条理。与1984的绝望不同,这本书除了让人看到外人很难知道的一些真相,悲哀,但同时也给人一些希望,让人珍惜亲情。此处摘录一些印象深刻的段落(数字是 Kindle 的 location)。

    103 没有污染

    The night sky in North Korea is a sight to behold. It might be the most brilliant in Northeast Asia, the only place spared the coal dust, Gobi Desert and carbon monoxide choking the rest of the continent.

    209

    They learned that China’s diluted brand of c0mmun1sm was less successful than that brought by Kim Il-sung and that millions of Chinese were going hungry.

    341 轻率、残酷的三八线

    The United States was concerned that the Soviet Union might seize Korea as a staging ground on the way to the bigger prize of Japan… So the two army officers looked for a convenient way to divide the peninsula. They slapped a line across the map at the 38th parallel.
    The line bore little relationship to anything in Korean history or geography… The bifurcation between north and south was an entirely foreign creation, cooked up in Washington and stamped on the Koreans without any input from them. One story has it that the secretary of state at the time, Edward Stettinius, had to ask a subordinate where Korean was.

    373

    Some of the rice farmers were sympathetic to the North because they’d heard a rumor that the C0mmun1sts would give them free land.

    409 傻子往北跑,能人往南跑

    Fearing persecution by the C0mmun1sts, tens of thousands of Koreans from north of the 38th parallel had fled south – among them landlords, businessmen, Christian clergymen, and Japanese collaborators. A small number of C0mmun1st sympathizers fled north. (这部分人的命运很悲惨,因为是从“敌方”过来的)

    414 卸磨杀驴

    After the war, Kim Il-sung made it his first order of business to weed out foe from friend. He started at the top with potential rivals for the leadership. He disposed of many of his comrades in arms who had led the struggle from Manchuria to unseat the Japanese occupiers. He ordered the arrest of the founding members of the C0mmun1st Par4y in South Korea. They had been invaluable during the war; now that they’d served their purpose they could be discarded. Throughout the 1950s, many more were purged in what was increasingly coming to resemble an ancient Chinese empire with Kim Il-sung the unchallenged master of the realm.

    424 精细的人口分类学。他们为了防止人们篡改自己的档案,在北部一个山区省存有备份。上大学是需要政审的

    The classifications became more refined in subsequent phases, such as the “Understanding People Project,” between 1972 and 1974.

    439

    North Koreans of the lower ranks were banned from living in the showcase capital of Pyongyang or the nicer patches of countryside toward the south where the soil was more fertile and the weather warmer.

    471 创造历史,无可争议

    It was especially taboo to talk about the Korean War and who started it. In the official histories (and there was nothing but official history in North Korea), it was the South Korean Army that invaded, acting on orders from the Americans, not the North Korean Army storming across the 38th parallel.

    510

    Their textbooks at school were full of stories of people burned, crushed, stabbed, shot, and poisoned by the enemy.

    556 某些移居日本的人把后代送回朝鲜帮助建设“祖国”,然而他们被视为敌人(除了他们的财富),命运凄惨

    No matter that they were avowed C0mmun1sts who gave up comfortable lives in Japan, they were lumped in with the hostile class. The regime couldn’t trust anyone with money who wasn’t a member of the Worker’s Par4y… the strength of the regime came from its ability to isolate its own citizens completely.

    587 孔夫子教遍地开花

    Koreans have measured their success in life by their proximity to power – part of a long Asian tradition of striving to get off the farm and close to the imperial palace.

    643 喉舌

    “Those who write in accordance with the par4y’s intention are heroes,” Kim Jong-il proclaimed.

    646 无耻流氓穷光蛋也要面子,掩耳盗铃

    Because Pyongyang is the only North Korean city frequented by foreigners, the regime goes to great lengths to ensure that its inhabitants make a good impression with their appearance and are ideologically sound… The streets and squares in the city center were designed in the ostentatiously oversized style favored in Moscow and other C0mmun1st cities that conveys the power of the regime over the individual.

    691

    North Korea was perpetually short of men – an estimated 20 percent of working-age men were in the armed services, the largest per capita military in the world.

    704 改造人类

    Kim Il-sung’s goal wasn’t merely to build a new country; he wanted to build better people, to reshape human nature. To that end, he created his own philosophical system, juche, which is commonly translated as “self-reliance.”

    759 造神从娃娃抓起

    A few days before each birthday, the Workers’ Par4y would distribute to every child more than two pounds of sweets… When the time came, the children lined up in front of the portraits to express their gratitude… “Thank you, dear father Kim Il-sung”

    861 一切行动听指挥

    The mobile police often dropped in after midnight to see if there were any overnight guests who might have come to visit without travel permits…Since the country was too poor and the power supply too unreliable for electronic surveillance, state security relied on human intelligence – snitches.

    887 外国人民生活在水深火热中

    Anything positive that happend in capitalist countries or especially South Korea, which in 1998 hosted the Summer Olympics, was downplayed. Strikes, disasters, riots, murders – elsewhere – got plenty of coverage.

    916 社会主义需要坚强的意志

    The Eastern Europeans and the Chinese weren’t as strong by nature or as disciplined.

    985

    New clothes were dispensed by your work unit or school, often on Kim Il-sung’s birthday, reinforcing his image as the source of all good things.

    1009 泡菜是不能共产的

    The North Korean regime understood you couldn’t keep Koreans happy without kimchi. Each family got 70 kilograms per adult and 50 kilos per child… Kimchi thieves were common in Chongjin. Even in a society as collectivist as North Korea, no one wanted to share their kimchi with a stranger.

    1024

    In fact, thousands of ethnic Koreans in China fled the famine caused by Mao Zedong’s disastrous “Great Leap Forward” to return to North Korea.

    1032

    Lies were built upon lies, all the way to the top, so it is in fact conceivable that Kim Il-sung himself didn’t know when the economy crashed…

    1036

    Kim Il-sung skillfully played the Soviet Union and China against each other, using their rivalry to extract as much aid as possible. Like an old-style emperor, he commanded tribute from neighboring realms: Stalin personally sent an armored limousine, Mao sent a complete train carriage… One day he would decree that the country should switch from rice to potatoes for its staple food; the next he would decide that raising ostriches was the cure for North Korea’s food shortage. The country lurched from one harebrained scheme to another.

    1046 就像1984里

    The north Korean propaganda machine kept hysteria at a high level, ginning up incessant reports of imminent invasion by the imperialist warmongers.

    1103 拯救嗷嗷待哺的韩国同胞

    People were told that their government was stockpiling food to feed the starving South Korean masses on the blessed day of reunification. They were told that the United states had instituted a blockade against North Korea that was keepking out food… United States – North Korea’s favorite scapegoat.

    1114 忍受饥饿,升华精神

    Enduring hunger became part of one’s patriotic duty. Billboards went up in Pyongyang touting the new slogan, “Let’s Eat Two Meals a Day.” North Korean television ran a documentary about a man whose stomach burst, it was claimed, from eating too much rice.

    1301 只有皇上可以风流

    Pornographers were executed. Notwithstanding his own excesses and those of Kim Jong-il, a playboy in his youth, par4y officials caught in adulterous affairs lost their jobs.

    1744 神死了!

    “If a great man ilke Kim Il-sung can die, why should a good-for-nothing ilke me go on living and consume food?” he cried.

    2113

    North Koreans learned to swallow their pride and hold their noses. They picked kernels of undigested corn out of the excrement of farm animals.

    2304 不是被饿死的

    By 1998, an estimated 600,000 to 2 million North Koreans had died as a result of the famine, as much as 10 percent of the population. In Chongjin, where food supplies were cut off earlier than the rest of North Korea, the toll might have been as high as 20 percent. Exact figures would be nearly impossible to tally since North Korean hospitals could not report starvation as a cause of death.

    Between 1996 and 2005, North Korea would receive $2.4 billion worth of food aid, much of it from the United States. But as much as the North Korean regime was willing to accept foreign food, it rejected the foreigners who came along with it.

    2659 关于吃人

    It does not seem, though, that the practice was widespread or even occurred to the degree that was chronicled in China during the 1958-62 famine, which killed as many as 30 million people.

    2877 失败的政变

    In the end, the entire 6th Army was dissolved, replaced eventually by units from the 9th Army from Wonsan. the process dragged on for many months. To this day, the exact reasons remain a mystery… Supposedly, corruption was rampant within the 6th Army and its officers were skimming off the profits for themselves and, like capos in the Mafia, were punished by the big boss.

    2958 共产也要防盗

    Most of the families in his neighborhood had raised the walls around their houses, ignoring a regulation that restricted the height to 1.5 meters so that police could look in.

    3019

    Since North Korea couldn’t manufacture its own appliances anymore, imported sets had to be fixed to the government stations and then their tuners disabled – a North Korean version of crippleware that would prevent them from receiving any information from the outside world.

    3100 株连

    Any antiregime activity would have terrible consequences for the protester, his immediate family, and all other known relatives. Under a system that sought to stamp out tainted blood for three generations, the punishment would extend to parents, grandparents, brothers, sisters, nieces, nephews, cousins.

    3344 前50年只有不到1000人逃出!关键就是他们认为自己还是世界上最幸福的。

    In the nearly half a century that elapsed between the end of the Korean War and Mi-ran’s defection in October 1998, only 923 North Koreans had fled to South Korea. It was a minuscule number if you consider that while the Berlin Wall stood an average of 21,000 East Germans fled west every year.

    3352 叛逃的代价… 不过许多人还是渴望自由。

    Defectors had to be able to live with the knowledge that their freedom came at the expense of loved ones who would likely spend the rest of their lives in a labor camp… By 2001, it was estimated that 100,000 North Koreans had sneaked into China, a small percentage of whom eventually defected to South Korea.

    3375 传说中,边界那边的中国农民都很富有,一天吃三顿米饭

    (It was said) that ordinary Chinese peasants living across the border were so rich that they ate white rice three times a day.

    3388 我以前也是这么想的……

    In her wildest dreams Dr. Kim never imagined leaving North Korea. It was not that she was ignorant or lacking in curiosity about the world – she was an avid reader and loved tales of exotic faraway lands – but as far as she was concerned, North Korea was the very best country of all. Why go anywhere else?

    3578 中国会遣返叛逃者

    At least eight thousand women were arrested in one such roundup in March 2000. (As of 2008, the crackdown on North Korean defectors continued.)

    3863 韩国接受朝鲜人“叛逃”,但是

    The NIS was also screening for Korean-speaking Chinese posing as North Koreans to obtain South Korean citizenship and resettlement benefits that were worth more than $20,000.

    3963 叛逃头等舱

    Defections were arranged like package tours and Mrs. Song went first class. Her package included the private car that drove her from Chongjin to the border, the bribes to the North Korean border guards who carried her on their backs across the river, and the stolen South Korean passport. “I could have done it cheaper,” Oak-hee explained, “but I wanted her to tranvel like a VIP.”

    4533 精神食粮

    When North Korea runs short of food, the regime feeds its population with more propaganda.

  • 《史蒂夫·乔布斯传》精彩摘录

    37页

    它(迷幻药)让我更清楚什么是重要的——创造伟大的发明,而不是赚钱。应该尽我所能,将此生放回历史和人类思想的长河。

    168页

    “这个道理很简单,团队扩张时,如果吸收了几名二流队员,他们就会招来更多二流队员,很快,你的团队里甚至还会出现三流队员,”他回忆道,“麦金塔的经验告诉我,一流队员只喜欢同一流队员合作,这就意味着你不能容忍二流队员。”

    181页

    但他对产品的关注又是斯卡利永远达不到的,而且乔布斯会侮辱任何一个算不上一流队员的人,以避免苹果出现太多的笨蛋。

    205页 Paul Rand –

    “我解决你的问题,你付钱给我。我设计出来的东西你用也行,不用也罢,都得付钱给我,但是我不做备选。”

    332页

    人们总是说他们和别人合不来,他们不喜欢团队合作。但是我发现,一流选手喜欢和一流选手共事,他们只是不喜欢和三流选手在一起罢了。

    392页

    艺术的作用就是驱赶丑陋。

    399页(这是“风水”真正有意义的地方)

    Steve 坚信,设计对路的建筑物会对文化起到积极的作用。

    519页(书末乔布斯的原话太精彩了,大段摘录)

    像IBM或微软这样的公司为什么会衰落,我有我自己的见解。这样的公司干得很好,它们进行创新,成为或接近成为某个领域的垄断者,然后产品的质量就变得不那么重要了。这些公司开始重视优秀的销售人员,因为是他们在推动销售、改写了收入数字,而不是产品的工程师和设计师。因此销售人员最后成为公司的经营者。IBM的约翰·埃克斯是聪明、善辩、非常棒的销售人员,但是对产品一无所知。同样的事情也发生在施乐。做销售的人经营公司,做产品的人就不再那么重要,其中很多人就失去了创造的激情。斯卡利加入后,苹果就发生了这样的事情,那是我的失误;鲍尔默接管微软后也是这样。苹果很幸运,能够东山再起,但我认为只要鲍尔默还在掌舵,微软就不会有什么起色。

    521页

    我的动力是什么?我觉得,大多数创造者都想为我们能够得益于前人取得的成就而表达感激。我并没有发明我用的语言或数学。我的食物基本都不是我自己做的,衣服更是一件都没做过。我所做的每一件事都有赖于我们人类的其他成员,以及他们的贡献和成就。我们很多人都想回馈社会,在历史的长河中再添上一笔。我们只能用这种大多数人都掌握的方式去表达——因为我们不会写鲍勃· 迪伦的歌或汤姆· 斯托帕德(Tom Stoppard)的戏剧。我们试图用我们仅有的天分去表达我们深层的感受,去表达我们对前人所有贡献的感激,去为历史长河加上一点儿什么。那就是推动我的力量。

  • 运算符优先级

    一段计时的代码,把时间长度用“2小时37分钟28秒”这样的形式输出,但是偶然注意到结果很有问题。盯着代码看了半天,觉得逻辑判断都是正确的,后来用一个数字 debug 才找到真相。

    比如 4000 秒,程序先判断如果大于一小时,就输出小时数,然后算余数。就是算余数这一步除了问题,代码写成 secs % 60*60。写代码的人为了清晰,还故意在乘号两边去掉了空格,可是这更加容易地造成了错觉,让人觉得 60*60 是先计算的。可是 “%” 的优先级和乘除是同等的!

    运算符优先级是挺难记的。我觉得,迷惑的时候,加括号就行了,可读性也绝对好。

    不过这次通过 Oracle 这个文档我是记住了,”%” 和乘除都是 multiplicative operators, 所以是同等优先级。想想处理器原理,确实是这样,除法的结果不就顺便出来余数了吗。

  • Java SimpleDateFormat 与 locale (以及 Mac OS X 更改语言)

    遇到一个非常怪异的问题,Tomcat 里面有个 servlet 用 SimpleDateFormat 解析日期的,类似这样:

    DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    formatter.parse("05-Jan-2012")
    

    但是会抛出 ParseException – Unparseable date “05-Jan-2012”.

    我仔细看了看,似乎一切都是对的,不应该出错。于是写一个最简单的测试类,main 函数就这么两行,同一台机器上运行完全正常。更纳闷了。

    最后在 servlet 代码里打出 formatter.format(new Date()) 的结果,发现是 “29-二月-2012”!

    这才想起我最早拿到这台 MacBook Pro 时系统是中文,我改成英文但是登录界面等少数地方还是中文。不知道 Tomcat 是怎么设置的 locale,不过这里有人在 Windows 上遇到同样的问题,可以通过指定 java 参数解决。

    但是苹果恰好有一个文档:Mac OS X: How to change the language displayed in the login window!看起来,我在 System Preferences 里修改的只是我当前用户的 locale,而我启动 tomcat 的时候,使用了 sudo——root 的 locale 仍然是中文?

    不管怎么样,既然有了这么专业的文档,照做就是了。不过苹果给的第一种解决办法太让人无语了:

    Reinstall Mac OS X and select the desired language during installation

    正常做法是在 Terminal 里执行 sudo languagesetup,挑选想要的语言即可。

    P.S. SimpleDateFormat 是有个 constructor 可以指定 locale 的。

  • 无限递归导致 Segmentation fault

    某服务器上一个 cron job 是 shell 脚本调用 Java 程序,最近老是报 Segmentation fault, 每次看见此信息

    /bin/sh: line 1: 4285 Segmentation fault java …

    总觉得无处下手,bash 的问题?Java 版本不对?信息量太少了。其实遇到这种事情不能谎,表面上没有信息一定要挖出信息来。今天仔细一看,这个脚本把标准输出重定向到一个日志文件去了,于是去看日志。这个程序的主体是对一个信息列表做循环,恰好在每个循环的关键部分开始前、结束后都会写一条日志。其目的是为了计时,不过正是这两条日志让我很快找到了错误缘由,因为发现日志文件的末尾只有一个开始前的,说面在这个循环的处理过程里面发生了 Segmentation fault.

    找出程序的源代码,发现循环里面调用的方法有一个是递归的,情况就开始明朗了,猜测就是递归无法终止导致 stack overflow,segmentation fault. 果然,根据日志里最后一行日志中记录的真实数据,发现这条数据是有问题的,会导致此方法无限递归。

    Wikipedia 的 Segmentation fault 词条里有一节是 “Common causes”, 我这次遇到的就是最后一条。