英国脱欧何时发生?


27

好吧,英国退欧发生了。天才新闻(Sky News)是他们的天才,他们决定在公交车旁创建倒计时。

enter image description here

您的工作是做类似的事情。英国于2019年3月31日离开欧盟,您必须为此创建一个数字倒计时,该倒计时每秒更改一次(允许0.05秒偏差)。

输入项

您的程序绝对不应输入。它被禁止!

输出量

它应以格式输出英国脱欧之前的时间ddd:hh:mm:ss。允许使用换行符开头和结尾,但每次显示都应留在同一位置。看起来好像它实际上已经到位了。正如@AnthonyPham所指出的,这并不意味着要打印足够的换行符来“清除”屏幕,这意味着您实际上必须清除屏幕。

不允许这样的输出:

100:20:10:05
100:20:10:04

这也不是

100:20:10:05
*A thousand newlines*
100:20:10:04

因为他们不在一条线上。

英国退欧后,您不必担心。您的程序最多只能运行31/3/2019

规则

  • 不允许出现标准漏洞
  • 这是因此以字节为单位的最短代码获胜。
  • 错误消息(尽管我不认为如何)是不允许的
  • 该代码应能够在2年内运行(英国退出欧盟时),并且应该显示实际时间,而不是再次从730开始(请参见下文)

倒数规则

倒计时不应硬编码,并且应该能够在英国退欧结束之前的任何时间运行,并且仍会产生正确的结果。新的一天开始时,小时应遵循以下格式

712:00:00:01
712:00:00:00
711:23:59:59

我再说一遍,为方便起见,英国退欧的结束日期是午夜31/3/2019(31:3:19 00:00:00或31:3:2019 00:00:00或您想要的任何其他格式)

NB:我想我拥有所有东西,但是我没有在沙盒中发布,否则可能已经不合时宜了。随意发布任何改进建议,因为它并不完美。


我可以每毫秒运行一次,但仍然可以每秒更新一次吗?这只是为了使我的代码更短。
— David Archibald

@DavidArchibald仅表示显示必须每秒更改一次。只是大多数答案会发现每秒运行起来会更容易。
— caird coinheringaahing

啊 好吧,1000如果我想每秒更改一次,我必须放一下。谢谢
— David Archibald

是否需要前导零?
— 毛茸茸的

1
午夜是什么时区:UCT或GMT?我们是否必须考虑到这一点?
— 泰特斯

Answers:


12

JavaScript中,134个 129 113字节

setInterval("d=-new Date;document.body.innerHTML=`<pre>${d/864e5+17986|0}:`+new Date(d).toJSON().slice(11,19)",1)

编辑:由于@Shaggy,节省了2个字节。@ l4m2节省了11个字节。


设法击败了你@Neil ...个字节。
— 大卫·阿奇博尔德

为什么不使用<a id=0>?或类似的东西?
— 所罗门·乌科

3
甚至document.body.innerHTML代替o.innerHTML
— cloudfeet

1
我希望它是等宽的,以确保显示保持在同一位置,但是我想我可以节省一个字节并使用<tt>。
— 尼尔

3
您可以通过每毫秒(或2或3 ...)运行代码来节省2个字节。
— 毛茸茸的

11

PowerShell,70 63 55 53字节

排除双引号,使用@Joey轻松实现-2

for(){cls;(date 31Mar19)-(date)|% T* ddd\:hh\:mm\:ss}

使用sleep运行此命令会增加8个字节,但是如果不使用输入运行(带有睡眠的版本(63字节)),则输入几乎是不可见的:

for(){cls;(date 31/3/19)-(date)|% T* ddd\:hh\:mm\:ss;sleep 1}

for() 是一个无限循环,并且在该循环内。

cls 清除屏幕,

将其31/3/19作为DateTime对象,并从中获取-当前日期,以给出剩余时间,然后.ToString()(|% T*)以正确的格式给出时间。

脱欧后这将显示负时间。


这对本地日期格式也很敏感。尽管英国dd / m / yy格式合适,但imo不会在美国机器上按原样运行。我很好奇T*工作原理。我对此不熟悉。
— Joel

@JoelCoehoorn这是一个很好的技巧,%foreach实际上选择了ToString对象,并将接受下一个字符串作为其参数。关于日期时间格式,为什么它不能在美国计算机上运行?我以为dd等对文化不敏感。
— colsw

它正在尝试在我的计算机上查找第31个月。如果将其更改为,效果很好3/31/19。也将工作2019-03-31的任何地方,但你的成本几个字节。
— 乔尔·科恩荷恩

@JoelCoehoorn啊对-抱歉,我想输出的,更新到31Mar19,而不是31/3/19应该希望解决它?
— colsw

1
在应用命令参数解析时,您可能会在格式字符串周围引起双引号。
— 乔伊

7

Excel VBA,91 84 82字节

由于JoeMalpass指出Excel将日期视为数字,因此节省了7个字节。
由于JoeMalpass,节省了2个字节

Sub b()
Do
t=CDec(43555-Now)
Cells(1,1)=Int(t) &Format(t,":hh:mm:ss")
Loop
End Sub

输出到A1活动Excel工作表中的单元格。


通过("2019-3-31")与交换-6个字节(43555)。但是,当我尝试在Excel中运行它时,它会在约5-6秒后冻结……
— CactusCake

1
@JoeMalpass谢谢,这很重要。对我来说,它也会在几秒钟后查找,因为它的计算速度快于1 /秒。不过,增加延迟会增加字节,而且OP并没有说它必须能够从现在开始一直递减计数直到退欧,而不会引起任何麻烦。
— Engineer Toast

显然Now不需要()太多的工作...
— CactusCake

6

Python 3.6,146个字节

from datetime import*
x=datetime
while 1:d=x(2019,3,31)-x.now();s=d.seconds;a=s%3600;print(end=f"\r{d.days:03}:{s//3600:02}:{a//60:02}:{s%60:02}")

5

C#,173 172 156 150 127字节

using System;class P{static void Main(){for(;;)Console.Write($"\r{new DateTime(2019,3,31)-DateTime.Now:d\\:hh\\:mm\\:ss}  ");}}

@Bob节省了16个 字节@ SørenD.Ptæus 节省了6个字节

格式化版本:

using System;

class P
{
    static void Main()
    {
        for (;;)
            Console.Write($"\r{new DateTime(2019, 3, 31) - DateTime.Now:d\\:hh\\:mm\\:ss}  ");
    }
}

你可以(TimeSpan)0吗?在电话上,目前无法测试。您仍然可以放下透明线,因为回车会处理它:它将光标放回到行的开头。
— 鲍勃

2
想法:如果长度更改(少于100天),则依赖CR可能会失败。修复:在末尾添加两个额外的空格。
— 鲍勃(Bob)”

@Bob我忘记删除了Clear!傻我 而且不能转换int为TimeSpan我已经尝试过的那个。
— TheLethalCoder

您可以节省6个字节的写入(t = new DateTime(2019, 3, 31) - DateTime.Now).Ticks > 0。
— 索伦D.Ptæus

@SørenD.Ptæus好主意甚至都没有想到这个
— TheLethalCoder

4

的JavaScript ES5,320 319 316 305 295 284字节

setInterval(function(){a=Math,b=a.floor,c=console,d=Date,e="00",f=new d(2019,2,31),g=a.abs(f-new d)/1e3,h=b(g/86400);g-=86400*h;var i=b(g/3600)%24;g-=3600*i;var j=b(g/60)%60;g-=60*j,c.clear(),c.log((e+h).slice(-3)+":"+(e+i).slice(-2)+":"+(e+j).slice(-2)+":"+(e+a.ceil(g)).slice(-2))})

感谢@Fels进行引用Math,@dgrcode进行引用console

未打高尔夫球

setInterval(function() {

  var math = Math, floor = math.floor, c = console, d = Date;

  var leadings = "00";

  // set our brexit date
  var brexit = new d(2019, 2, 31);

  // get total seconds between brexit and now
  var diff = math.abs(brexit - new d()) / 1000;

  // calculate (and subtract) whole days
  var days = floor(diff / 86400);
  diff -= days * 86400;

  // calculate (and subtract) whole hours
  var hours = floor(diff / 3600) % 24;
  diff -= hours * 3600;

  // calculate (and subtract) whole minutes
  var minutes = floor(diff / 60) % 60;
  diff -= minutes * 60;

  // what's left is seconds

  // clear the console (because OP said it must print in the same place)
  c.clear();

  // log the countdown, add the leadings and slice to get the correct leadings 0's
  c.log((leadings + days).slice(-3) + ":" + (leadings + hours).slice(-2) + ":" + (leadings + minutes).slice(-2) + ":" + (leadings + math.ceil(diff)).slice(-2));

});


2
您可以节省一些重命名数学的方法,例如g=Math;
— Fels

看起来别名Math仍然可以保存2个字节,对吗?
— Marie

通过使用箭头功能并摆脱,您可以节省更多var。此外,挑选较短的名称为变量,如h代替hours或m代替minutes。大概c=console可以节省更多的字节。同样1e3代替1000
— Daniel Reina

4

PHP,84字节

for(;$c=DateTime;)echo(new$c('@1553990400'))->diff(new$c)->format("\r%a:%H:%I:%S ");

非常坦率的。1553990400是UTC的31-3-2019 00:00:00的时间戳。使用DateTime-> diff()-> format()输出剩余时间,它可以无限循环。英国退欧发生后,它将从0开始计数。

已评论/更具可读性的版本:

// Infinite loop, assign DateTime (as a string) to $class
for (; $class = DateTime;) {
    echo (new $class('@1553990400')) // Create a new DateTime object for the brexit date/time.
            ->diff(new $class) // Caulculate the difference to the current date/time.
            ->format("\r%a:%H:%I:%S "); // Format it according to the specification, starting with a \r character to move to the start of the line and overwrite the previous output.
}

新功能在这里,即将发布我自己的答案。一些注意事项:1.我们可以在<?此处省略标签吗?2.我认为您的括号是错误的,现在您正在调用->diffecho而不是DateTime对象。3. echo无论如何都没有括号。4.即使解决了这个问题,这对我也不起作用,但是我是打高尔夫球的新手,所以我可能只是个白痴。
— Sworrub Wehttam

1
只需运行它,即可看到它按要求运行良好。从命令行运行PHP时无需刷新。字符串的开头有一个\ r,将光标移动到行的开头,它会不断覆盖以前输出的时间。这是许多其他答案使用的相同方法。
— chocochaos

1
抱歉,我错过了您的第一条评论。是的,除非需要提供完整的程序,否则我们可以省略开头的标签。括号就很好了。是的,没有它,echo可以工作,但是没有它们,在DateTime对象上调用diff不能工作。它在这里运行良好,您使用的是哪个PHP版本以及如何运行该程序?:)
— chocochaos

1
您可能想在这里查看一些帖子:codegolf.meta.stackexchange.com/questions/tagged/php
— chocochaos

1
— 差不多

4

CJam,69 62 59 57字节

通过不同地转换为时间格式节省了7个字节

感谢Martin Ender的建议,节省了3个字节

通过使用回车符而不是退格键节省了2个字节

{15539904e5esm1e3/{60md\}2*24md\]W%{sYTe[}%':*CTe[oDco1}g

由于明显的原因,不能在TIO上运行。

尽管它实际上仅每秒更新一次时间,但它会无限循环不断地重写显示内容,因此文本种类会闪烁进出(至少在我的控制台中)。

此70字节版本仅每秒打印一次:

{15539904e5es:Xm1e3/{60md\}2*24md\]W%{sYTe[}%':*CTe[oDco{esXm1e3<}g1}g

说明

{                           e# Begin a while loop
  15539904e5                e#  The timestamp on which Brexit will occur
  es                        e#  The current timestamp
  m                         e#  Subtract
  1e3/                      e#  Integer divide by 1000, converting to seconds from ms
  {                         e#  Run this block twice
   60md                     e#   Divmod by 60
   \                        e#   Swap top elements
  }2*                       e#  (end of block) 
                            e#    This block divmods the timestamp by 60, resulting in 
                            e#    the remaining minutes and seconds. Then the minutes get 
                            e#    divmod-ed by 60, to get hours and minutes remaining
  24md\                     e#  Divmod hours remaining by 24 and swap top elements, to get
                            e#    the hours left and days left.
  ]                         e#  Wrap the entire stack in an array
  W%                        e#  Reverse it since it's currently in the wrong order
  {                         e#  Apply this block to each element of the array
   s                        e#   Cast to string (array of digit characters)
   YTe[                     e#   Pad to length 2 by adding 0s to the left
  }%                        e#  (end of map block)
  ':*                       e#  Join with colons
  CTe[                      e#  Pad to length 12 by adding 0s to the left, dealing with the
                            e#    special case of the day being 3 digits. 
  o                         e#  Pop and print the resulting string, which is the time
  Dco                       e#  Print a carriage return, moving the cursor back to the start
  1                         e#  Push 1
}g                          e# Pop 1, if it's true, repeat (infinite loop)

目前尚无法测试,但您可以将替换Ab为s,并1e3/先执行一个字节,然后以相反的顺序进行划分来保存一个字节60md\60md\24md\]W%。
— Martin Ender'4

@MartinEnder是的,他们都工作。谢谢
— 商业猫

哦,然后{60md\}2*保存另一个。
— Martin Ender'4

3

Python 3.5(118字节)

import datetime as d,os
d=d.datetime
while 1:os.system("cls");e=str(d(2019,3,31)-d.today());print(e[:3]+':'+e[-15:-7])

3

C#6,149个字节

感谢Bob节省了57个字节!

using System;class P{static void Main(){DateTime a,x=new DateTime(2019,3,31);while((a=DateTime.Now)<x)Console.Write($"\r{x-a:ddd\\:hh\\:mm\\:ss}");}}

非高尔夫节目:

using System;

class P
{
    static void Main()
    {
        DateTime a,
                x = new DateTime(2019, 3, 31);
        while ( (a = DateTime.Now) < x)
            Console.Write($"\r{x-a:ddd\\:hh\\:mm\\:ss}");
    }
}

C#,210 206 159字节

谢谢 Bob节省了47个字节!

谢谢 Martin Smith节省了4个字节!

using System;class P{static void Main(){DateTime a,x=new DateTime(2019,3,31);while((a=DateTime.Now)<x)Console.Write("\r"+(x-a).ToString(@"ddd\:hh\:mm\:ss"));}}

非高尔夫节目:

using System;

class P
{
    static void Main()
    {
        DateTime a,
                x = new DateTime(2019, 3, 31);
        while ( (a = DateTime.Now) < x)
            Console.Write("\r" + (x - a).ToString(@"ddd\:hh\:mm\:ss"));
    }
}

1
new DateTime(2019,3,31)更短。
— 马丁·史密斯

1
您还可以删除该字符串,Console.Clear而是将其替换"\r"为该字符串的前缀,例如"\r"+(x-a)...
— Bob

1
更多改进:(x-a).ToString(@"d\:hh\:mm\:ss")等同于`String.Format(“ \ r {0:d \\:hh \\:mm \\:ss}”,xa)`等同于$@"{x-a:d\:hh\:mm\:ss}"(C#6.0中较短的插值字符串)。如果这样做,则可以将完整长度进一步缩短"\r"+$@"{x-a:d\:hh\:mm\:ss}"为$"\r{x-a:d\\:hh\\:mm\\:ss}"。
— 鲍勃

1
@TheLethalCoder我认为这根本就是不允许“假”多换行方法。回车仍然会覆盖屏幕上的唯一内容。至少另一个答案也使用这种方法。还可以消除睡眠,因为每秒更改一次显示屏是唯一重要的,并且允许不更改更新(请参阅问题注释)。编辑:实际上,问题注释也明确允许使用CR ...
— Bob

1
@鲍勃对不起,我读了你的建议,换了一行,这是我的错。但是,可以消除睡眠
— TheLethalCoder

3

C,104字节

main(x){for(;x=1553990400-time(0);)printf("\n%03d:%02d:%02d:%02d\e[1A",x/86400,x/3600%24,x/60%60,x%60);}

分解

main(x){
    for(;x=1553990400-time(0);)             // Seconds remaining
        printf("\n%03d:%02d:%02d:%02d\e[1A",// Move cursor and print:
            x/86400,                        // Days
            x/3600%24,                      // Hours
            x/60%60,                        // Minutes
            x%60);                          // Seconds
    puts("Now we can stop talking about it forever."); // Wishful thinking
}

依靠 time自1970年1月1日以来返回的秒数,这对我来说就是这种情况(在macOS上使用Clang / GCC),大多数UNIX情况应该如此。

使用bash终端转义序列来移动光标(<esc>[1A将光标向上移动1行)。能够简单地使用会更好\r,但是printf直到看到换行符时才刷新,并且手动刷新需要更多时间。

可能是我见过的最耗CPU的倒数计时。在热循环中运行以确保其始终尽可能准确。如果在截止日期之后运行,它将产生一些非常奇怪的东西(到处都是负数!)


@TobySpeight当然,已更正。我认为它们是bash,因为我总是使用以下参考页:tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
— Dave

C语言中的main形式是否有任何规则?在C90中:如果主叫方(OS)使用返回值,则从主要原因UB中省略返回。在标准C:main(x)中不是main的有效形式,隐式int已从语言中删除。我相信最小,安全的版本是int main(){}。
— 隆丁

@Lundin在代码高尔夫中,语言是由其实现定义的(即,如果您找到一个可公开使用的编译器,可以按自己的意愿编译代码,则可以这样做,但是如果它需要非标准的标志设置,则这些标志将计入您的位元组数)。您是正确的,这远不符合标准(以及您已经注意到的事情,由于没有导入,并且依赖时间返回自1970年1月1日起的秒数,所以隐式定义了函数)。看一下人们遵循的(而不是分散的)规则的元数据。
— 戴夫

高尔夫挑战代码带来的乐趣之一就是发现奇怪的语言漏洞和过时的功能!
— 戴夫

3

AHK,145字节

我认为这不是最短的答案,但是结果给人以厄运的感觉。我本来想送按键Ctrl+ A之后DEL,然后无论时间,但是刷新速度太慢,它会破坏你在任何环境。相反,那么,我与GUI去了。事实证明,与一遍又一遍地更新控件相比,完全销毁窗口并重新创建窗口所需的字节数更少,因此我同意了。这是一个很好的效果。

Loop{
s=20190331000000
s-=A_Now,S
d:=t:=20000101000000
t+=s,S
d-=t,D
d*=-1
FormatTime f,%t%,:HH:mm:ss
GUI,Destroy
GUI,Add,Text,,%d%%f%
GUI,Show
}

I'm gonna sing the doom song!


3

C#,128个 127字节

using System;class P{static void Main(){for(;;)Console.Write($"\r{new DateTime(2019,3,31)-DateTime.Now:ddd\\:hh\\:mm\\:ss}");}}

取消程式码:

using System;
class P
{
    static void Main()
    {
        for(;;)
            Console.Write($"\r{new DateTime(2019,3,31)-DateTime.Now:ddd\\:hh\\:mm\\:ss}"); 
    }
}

如果没有其他C#答案的帮助,我不会想出\ r技巧。

对于任何需要进一步改进的人,您还可以将Write()表达式放入for循环中。似乎我应该可以在这里保存一个字节,因为这样可以为我节省该语句的分号,但由于您不能有一个完全空的主体,因此可以使用相同的数字:

using System;class P{static void Main(){for(;;Console.Write($"\r{new DateTime(2019,3,31)-DateTime.Now:ddd\\:hh\\:mm\\:ss}"));}}

欢迎来到PPCG!
— Martin Ender'4

您仍然可以使用插值字符串来保存单个字符;)Console.Write($"\r{new DateTime(2019,3,31)-DateTime.Now:ddd\\:hh\\:mm\\:ss}");
— Bob的

玩得好,我试图找到一种在最短的代码中进行while(true)循环的方法!我会记住那个把戏。您可能需要像我一样在您的写入中添加几个空格,否则当日期从3位变为2位时,它不会覆盖时间字符串的最后一个字符,您将得到一个奇怪的输出
— NibblyPig

您可以像在我的答案中那样使用插值字符串来保存一个字节。您的格式ddd为零,看起来比两个空格更好。
— TheLethalCoder

更新为内插字符串。
— Joel Coehoorn

3

Ruby(83字节)

loop{h=431664-Time.now.to_r/3600;$><<['%02d']*4*?:%[h/24,h%24,h%1*60,h*3600%60]+?\r}

不打高尔夫球

loop do
  seconds = 1553990400 - Time.now.to_r

  print (["%02d"] * 4).join(':') % [
    seconds / 24 / 60 / 60     ,
    seconds      / 60 / 60 % 24,
    seconds           / 60 % 60,
    seconds                % 60,
  ] + "\r"
end

基本上是Python提交之一,但有所改进。在重新渲染之前,我们只是发出一个“ \ r”以字符串的开头。对于的字符串格式"%03d:%02d:%02d:%02d",我们真的不在乎第一个说明符的宽度……所以我们可以这样做"%02d"*4,并发出退格键和空格以清除多余的冒号。

另外,我发现一个两个字符的短print:$><<。$>是一个速记全球对$defout,这对于输出流print和printf,缺省值为STDOUT。IO#<<将其右侧写入流中。那两个字符怎么短?好了,我现在可以省略在括号内加上格式字符串的前导空格。

在这一点上,我真的认为没有办法在Ruby中进一步缩短此程序。

编辑:我错了。代替第一时间Time.new(2019,3,31),我们可以使用原始UNIX时间:1553990400。

编辑2:我尝试过处理分钟,然后将UNIX时间戳除以该常数,但是实际上并没有节省任何字节。:(

编辑3:结果缓存 h=3600实际上使我受了两个字节的伤害。哎呀

编辑4:感谢@EricDuminill,节省了3个字节。他使用了浮点数,但是在不损失精度的情况下进行了理性操作!

编辑5:Array#*作为的别名Array#join,?对每个字符使用Ruby -syntax!


使用小时而不是秒似乎可以节省3个字节:loop{h=431664-Time.now.to_f/3600;$><<('%02d:'*4+"\b \r")%[h/24,h%24,h%1*60,3600*h%60]}由于四舍五入的原因,秒数可能会减少1。
— 埃里克·杜米尼尔

谢谢!现在与PHP捆绑在一起。:)我用来to_r代替to_f保持精度。
— Stephen Touset

别客气。即使.to_r不是,此时钟也可能仍早1 .to_f
— 秒

@StephenTouset您可以移至+"\r"数组的右侧。这使您可以使用['%02d']*4*?:而不是,'%02d:'*4+"\b "这样您就可以丢失括号,从而使您净得一个字节。通过写入+?\r而不是保存另一个字节+"\r"。
— Synoli

很棒的发现!
— Stephen Touset

2

JavaScript + HTML,136 + 7 = 143字节

setInterval("d=1553990400-new Date/1e3|0;w.innerText=[60,60,24,999].map(z=>(q='00'+d%z,d=d/z|0,q.slice(z<61?-2:-3))).reverse().join`:`")
<a id=w

分数使它成为... 爱的字节吗?
— Engineer Toast

2

C#,142个字节

using System;class P{static void Main(){a:Console.Write($"\r{(new DateTime(2019,3,31)-DateTime.Now).ToString("d\\:h\\:mm\\:ss  ")}");goto a;}}

非高尔夫节目:

using System;
class P
{
    static void Main()
    {
        a: Console.Write($"\r{(new DateTime(2019, 3, 31) - DateTime.Now).ToString(@"d\:h\:mm\:ss  ")}"); goto a;
    }
}

遇到英国脱欧时,这不会停止打印,不确定是否有问题
— TheLethalCoder

@TheLethalCoder合法。说明特别指出代码仅在2019年3月3日之前有效,此后发生的情况无关紧要。如果您可以通过在该日期之后进行怪异的输出甚至是异常来节省字节,则可以为您提供更多的功能。
— Joel Coehoorn

另外:我怀疑通过跳过ToString()字节并把格式构建到Write()方法或插值字符串中,甚至可能会导致短路
— Joel Coehoorn

好吧...这绝对有可能。我有127个字节。我将其附加在您的原始图片下方,因此您仍然可以从基本goto想法中获得赞誉。
— 乔尔·科恩荷恩

我的编辑去消灭:(想我得后我自己的答案。
— 乔尔Coehoorn

2

Bash + GNU日期,128个字节

感谢@muru减少了2个字节,而感谢@This Guy减少了2个字节。

C=:%02d;while sleep 1;do D=$[B=3600,A=24*B,1553990400-`date +%s`];printf "%03d$C$C$C\r" $[D/A] $[D%A/B] $[D%A%B/60] $[D%60];done

不打高尔夫球

DAY=86400
HOUR=3600
while sleep 1 ; do
 DIFF=$[1553990400-`date +%s`]
 printf "%03d:%02d:%02d:%02d\r" \
         $[DIFF/DAY] \
         $[DIFF%DAY/HOUR] \
         $[DIFF%DAY%HOUR/60] \
         $[DIFF%60]
done

1
Never golfed in Bash before but can you remove the spaces at the while: while[1]?
— caird coinheringaahing

Fair point, have now given that a try but it yields line 1: [1]: command not found error. Spaces are mandatory it seems :-(
— steve

2
Assign 3600 to a variable to save 2 bytes? You might also be able to change B=3600;A=86400 to B=3600;A=24*B for another byte?
— caird coinheringaahing

3600 variable now done, thx. 24*B would need A=$[24*B] so not possible there though
— steve

1
C=:%02d;printf "%03d$C$C$C\r" to save a byte? And move the assignments to A and B in the arithmetic context: D=$[B=3600,A=24*B,1553990400-`date +%s`] to save another?
— muru

2

MATL, 45 bytes

737515`tZ'-tkwy-':HH:MM:SS'XOw'%03d'YDwYcDXxT

TIO apparently doesn't support clearing the output, but thankfully MATL Online does!

At 2 bytes more, a bit more CPU-friendly version that does a pause (sleep) every second:

737515`tZ'-tkwy-':HH:MM:SS'XOw'%03d'YDwYcDT&XxT

Try this on MATL Online

737515 is "31st of March 2019" represented MATLAB's default epoch format - number of days from January 0, 0000, optionally including a fractional part to represent the time of day. (I tried to shorten this by calculating it somehow, but its only factors are 5 and another six digit number (147503), and I couldn't figure out a way to do it in less than 6 bytes.)

`    % start do-while loop
tZ`- % duplicate Brexit date, get current date (& time), subtract
tk   % duplicate the difference, get the floor of it (this gives number of days left)
w    % switch stack to bring unfloored difference to top
y    % duplicate the floored value on top of that
-    % subtract to get fractional part representing time
':HH:MM:SS'XO % get a datestr (date string) formatted this way
w    % switch to bring number of days back on top
'%03d'YD      % format that to take 3 places, with 0 padding if needed
wYc  % switch to bring time string back on top, concatenate date and time
D    % display the concatenated result!
T&Xx % clear screen after a 1 second pause (the pause is optional, without it the call is `Xx`)
T    % push True on stack to continue loop

1

PHP, 64 bytes

while($d=1553990401-time())echo--$d/86400|0,date(":H:i:s\r",$d);

This will count exactly until 0:00:00:00 and then break/exit. Run with -r.

-2 bytes if I wouldn´t have to print the 0.


1

RPL, 83 78 bytes

Assuming your HP48, or similar, is setup with correct (UK) time and date, mm/dd date format, and 24h time format:

WHILE 1REPEAT DATE 3.302019DDAYS":"1.1 24TIME HMS- TSTR 15 23SUB + + 1DISP END

I was surprised to be able to save 2 bytes by removing spaces around ":". 1.1 is the shortest valid date, later dumped by SUB. Be careful with emulators, the time may run faster or slower (or not at all) than your wall clock. With a real HP, you can stop this program by pressing the ON key … or wait for empty batteries.


0

PHP, 102 95 90 bytes

Saved 7 bytes thanks to @TheLethalCoder & by not factoring

Saved another 5 bytes by concatenating

<? $b=1553990400-time();echo floor($b/$d=86400).date(':H:i:s', $b%$d);header('Refresh:1');

This is my first golf, so I'm probably missing quite a few tricks, but here you are regardless.

As for @chocochaos' PHP answer that would otherwise trump this, I believe it is flawed for reasons I have explained in my comments, but as I'm new here I might be wrong. Or I'm just a newbie :)


You may need a closing tag? (Not sure on that). You only use $a once so might as well use the number in place. I'm not too familiar with php but looks like there could be more improvements
— TheLethalCoder

What do you mean by flawed because of reasons stated in the comments? If you mean the comments on their answer, you're the only one who's commented.
— caird coinheringaahing

@TheLethalCoder No closing tag needed. And nice one!
— Sworrub Wehttam

1
You can save ~7 bytes by using a <?= opening tag and dropping the echo, assigning $b in the place you first use it. Additionally you can save 3 bytes using ^0 instead of floor. It's bitwise or and involves a cast to int, it's the shortest way to cast to int I've seen.
— user59178

1
My answer runs just fine :) Also, you might want to read up on some posts here: codegolf.meta.stackexchange.com/questions/tagged/php There's no need for opening tags either. I would really recommend to write your solution in such a way that it would run from the command line, then you would not have to mess around with headers to refresh. That refresh will also cause your solution to "miss" a second once in a while, because it does not take into account the time lost on the request itself.
— chocochaos

0

AWK, 78 bytes

BEGIN{for(;v=1552953600-systime();printf"\r%d:%s",v/86400,strftime("%T",v)){}}

Try it online!

Was longer before I realized I could precalculate the end time. Sometimes it pays to be a bit late to the game and get ideas from others.

FYI, the TIO link doesn't work very well, since it doesn't implement \r properly.


0

F#, 142 bytes

open System
let f=
 let t=DateTime(2019,3,31)
 while DateTime.Now<=t do Console.Clear();t-DateTime.Now|>printf"%O";Threading.Thread.Sleep 1000

I grew up in Ireland about half a kilometre from the border. Apart from a "Welcome to Fermanagh" sign and the road markings changing you wouldn't know you'd entered another country. Used to cross it twice on the way to school.


0

c, gcc 114 bytes

main(s){system("clear");if(s=1553900399-time(0)){printf("%d:%d:%d:%d\n",s/86400,s/3600%24,s/60%60,s%60);main(s);}}

Nothing omitted, full program. The program compiles in gcc on Ubuntu. The countdown will not show a long trail of print statements because of the system call to clear and halts when the countdown reaches 0 seconds.

UnGolfed

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>


//Brexit countdown timer
int main(){
  int sec = 1553900400 - time(0);//seconds remaining until Brexit
  if(sec){
    sleep(1);
    system("clear");
    printf("Brexit Countdown\n");
    printf("days Hours Mins Secs\n");
    printf("%d:  %d:    %d:  %d\n",
            sec/86400, sec/3600%24,
            sec/60%60, sec%60);
    main();
  }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.