Question

As you may know, tonight, at exactly 23:31:30 UTC, Epoch Time will reach 1234567890! Hurray!

One way of watching epoch time is by using Perl:

perl -le 'while(true){print time();sleep 1;}'

Can you do the same in another programming language?

Was it helpful?

Solution

this site is in my favorites and has many answers for it

OTHER TIPS

python one-line:

python -c "while True: import time;print time.time();time.sleep(1)"

shell script:

while :; do printf "%s\r" $(date +%s); sleep 1; done

python:

import time
import sys

while True:
    sys.stdout.write("%d\r" % time.time())
    sys.stdout.flush()
    time.sleep(1)

php one-liner

php -r 'while(true) { echo time(), "\n"; sleep(1);}'

Zsh - Advanced Unix Shell:

zmodload zsh/datetime && while true; do print $EPOCHSECONDS ; sleep 1; done

This would be the same code in c#:

    DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0);
    while (true)
    {
        Console.WriteLine((int)(DateTime.UtcNow - epoch).TotalSeconds);
        Thread.Sleep(1000);
    }

And like tehvan said, it's the current time, not "Epoch" time

Java

import java.util.Date;

public class EpochTime {
    public static void main(String[] args) {
        while (true) {
            System.out.println(new Date().getTime() / 1000);
            try {
                Thread.sleep(1000);
            }
            catch (InterruptedException ignore) {
            }
        }
    }
}

java:

System.out.println((new java.util.Date(0)).toString());

That's the epoch :) ... the current time would be:

System.out.println((new java.util.Date()).toString());

For getting the amount of milliseconds passed since the epoch, do:

System.out.println("" + (new java.util.Date()).getTime());

More perl:

perl -MAnyEvent -MDateTime -E 'my $cv = AE::cv; my $w = AE::timer 0, 1, sub { say DateTime->now->epoch }; $cv->wait'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top