System time: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|J}}: Terminology correction that only J coders will care about.)
(wiki formatting)
Line 52: Line 52:
==See Also==
==See Also==
[[Date format]]
[[Date format]]

[http://en.wikipedia.org/wiki/Time_%28computing%29#Retrieving_system_time Retrieving system time (wiki)]
[http://en.wikipedia.org/wiki/Time_%28computing%29#Retrieving_system_time Retrieving system time (wiki)]

Revision as of 23:32, 23 January 2008

Task
System time
You are encouraged to solve this task according to the task description, using any language you may know.

Output the system time (any units will do as long as they are noted) either by a system command or one built into the language. The system time can be used for debugging, network information, random number seeds, or something as simple as program performance.

BASIC

Interpreter: QuickBasic 4.5

This shows the system time in POSIX time.

PRINT TIMER

Forth

Forth's only standard access to the system timers is via DATE&TIME ( -- s m h D M Y ) and MS ( n -- ) which pauses the program for at least n milliseconds. Particular Forth implementations give different access to millisecond and microsecond timers:

Interpreters: Win32Forth, GNU Forth, bigFORTH, iForth, PFE, SwiftForth, VFX Forth, MacForth

[UNDEFINED] MS@ [IF]                                   \ Win32Forth (rolls over daily)
 [DEFINED] ?MS [IF] ( -- ms )
   : ms@ ?MS ;                                         \ iForth
 [ELSE] [DEFINED] cputime [IF] ( -- Dusec )
   : ms@ cputime d+ 1000 um/mod nip ;                  \ gforth: Anton Ertl
 [ELSE] [DEFINED] timer@ [IF] ( -- Dusec )
   : ms@ timer@ >us 1000 um/mod nip ;                  \ bigForth
 [ELSE] [DEFINED] gettimeofday [IF] ( -- usec sec )
   : ms@ gettimeofday 1000 MOD 1000 * SWAP 1000 / + ;  \ PFE
 [ELSE] [DEFINED] counter [IF]
   : ms@ counter ;                                     \ SwiftForth
 [ELSE] [DEFINED] GetTickCount [IF]
   : ms@ GetTickCount ;                                \ VFX Forth
 [ELSE] [DEFINED] MICROSECS [IF]
   : ms@  microsecs 1000 UM/MOD nip ;                  \  MacForth
[THEN] [THEN] [THEN] [THEN] [THEN] [THEN] [THEN]

MS@ .   \ print millisecond counter

J

The external verb 6!:0 returns a six-element floating-point array in which the elements correspond to year, month, day, hour, minute, and second. Fractional portion of second is accurate to thousandths.

   6!:0 ''

Java

This shows the system time in POSIX time.

import java.util.Date;

public class SystemTime{
   public static void main(String[] args){
      Date now = new Date();
      System.out.println(now.getTime());
   }
}

Other methods are available in the Date object such as: getDay(), getHours(), getMinutes(), getSeconds(), getYear(), etc.

See Also

Date format

Retrieving system time (wiki)