System time: Difference between revisions

From Rosetta Code
Content added Content deleted
(added ruby)
Line 176: Line 176:
<python>import time
<python>import time
print time.ctime()</python>
print time.ctime()</python>

=={{header|Ruby}}==

<ruby>puts Time.now</ruby>


==See Also==
==See Also==

Revision as of 07:19, 27 November 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.

Ada

The following example displays a date-time stamp. The time zone value is the number of minutes offset from the prime meridian.

with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
with Ada.Calendar.Time_Zones; use Ada.Calendar.Time_Zones;
with Ada.Text_Io; use Ada.Text_Io;

procedure System_Time is
   Now : Time := Clock;
begin
   Put_line(Image(Date => Now, Time_Zone => -7*60));
end System_Time;

Output:

2008-01-23 19:14:19

BASIC

Works with: QuickBasic version 4.5

This shows the system time in seconds since midnight.

PRINT TIMER

C

This probably isn't the best way to do this, but it works. It shows system time as "Www Mmm dd hh:mm:ss yyyy", where Www is the weekday, Mmm the month in letters, dd the day of the month, hh:mm:ss the time, and yyyy the year.

#include<time.h>
#include<stdio.h>
#include<stdlib.h>
int main(){
  time_t my_time = time(NULL);
  printf("%s", ctime(&my_time));
  return 0;
}

Common Lisp

(multiple-value-bind (second minute hour day month year) (get-decoded-time)
	   (format t "~4,'0D-~2,'0D-~2,'0D ~2,'0D:~2,'0D:~2,'0D" year month day hour minute second))

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:

Works with: Win32Forth
Works with: GNU Forth
Works with: bigFORTH
Works with: iForth
Works with: PFE
Works with: SwiftForth
Works with: VFX Forth
Works with: 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

Fortran

In ISO Fortran 90 or later, use the SYSTEM_CLOCK intrinsic subroutine:

     integer :: start, stop, rate
     real :: result
     
     ! optional 1st integer argument (COUNT) is current raw system clock counter value (not UNIX epoch millis!!)
     ! optional 2nd integer argument (COUNT_RATE) is clock cycles per second
     ! optional 3rd integer argument (COUNT_MAX) is maximum clock counter value
     call system_clock( start, rate )
     
     result = do_timed_work()
     
     call system_clock( stop )
     
     print *, "elapsed time: ", real(stop - start) / real(rate)

In ISO Fortran 95 or later, use the CPU_TIME intrinsic subroutine:

     real :: start, stop
     real :: result
     
     ! System clock value interpreted as floating point seconds
     call cpu_time( start )
     
     result = do_timed_work()
     
     call cpu_time( stop )
     
     print *, "elapsed time: ", stop - start

Haskell

import System.Time

do
  ct <- getClockTime
  cal <- toCalendarTime ct
  putStrLn $ calendarTimeToString cal -- default format
  putStrLn $ formatCalendarTime System.Locale.defaultTimeLocale "%a %b %e %H:%M:%S %Y" cal

Io

Date now println

Example output:

2008-08-26 00:15:52 EDT

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 given to thousandths.

   6!:0 ''
2008 1 23 12 52 10.341

Java

This shows the system time in POSIX time. <java>import java.util.Date;

public class SystemTime{
   public static void main(String[] args){
      Date now = new Date();
      System.out.println(now.getTime());
      //System.currentTimeMillis() returns the same value
   }
}</java>

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

OCaml

#load "unix.cma";;
open Unix;;
let {tm_sec = sec;
     tm_min = min;
     tm_hour = hour;
     tm_mday = mday;
     tm_mon = mon;
     tm_year = year;
     tm_wday = wday;
     tm_yday = yday;
     tm_isdst = isdst} = localtime (time ());;
Printf.printf "%04d-%02d-%02d %02d:%02d:%02d\n" (year + 1900) (mon + 1) mday hour min sec;;

Perl

Simple localtime use in scalar context.

print scalar localtime, "\n";

Output:

Thu Jan 24 11:23:30 2008

localtime use in array context.

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;
printf("%04d-%02d-%02d %02d:%02d:%02d\n", $year + 1900, $mon + 1, $mday, $hour, $min, $sec);

Output:

2008-01-24 11:23:30

localtime use in array context with POSIX strftime.

use POSIX qw(strftime);

$now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
print "$now_string\n";

Output (with cs_CZ.UTF-8 locale):

Čt led 24 11:23:30 2008

Python

<python>import time print time.ctime()</python>

Ruby

puts Time.now

See Also

Date format

Retrieving system time (wiki)