Sponsored By

Sign in to follow this  
davalend

Current Time

Recommended Posts

what programming language do you want to use? in python, this would display your time

import time
time.ctime().split()[3]

Share this post


Link to post
Share on other sites

More or less ISO 8601.

ISO C

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

void print_time(void)
{
struct tm * tm;
time_t t;
char s[100];

t = time(NULL);
tm = localtime(&t);
strftime(s, sizeof s, "%Y-%m-%dT%H:%M:%S%z", tm);
puts(s);
}

Win32

#include <stdio.h>
#include <windows.h>

void print_time(void)
{
SYSTEMTIME st;
TIME_ZONE_INFORMATION tz;

GetLocalTime(&st);
GetTimeZoneInformation(&tz);

printf("%.4hu-%.2hu-%.2huT%.2hu:%.2hu:%.2hu%+.2ld:%.2ld\n",
st.wYear, st.wMonth, st.wDay,
st.wHour, st.wMinute, st.wSecond,
tz.Bias / 60, +tz.Bias % 60);
}

Java

import java.util.*;
import java.text.*;

public class T
{
public static void printTime()
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
StringBuffer sb = new StringBuffer();

df.format(new Date(), sb, new FieldPosition(0));
System.out.println(sb.toString());
}
}

C#

using System;

public class T
{
public static void PrintTime()
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:sszzzz"));
}
}

SML

fun printTime () =
let
val d = Date.fromTimeLocal (Time.now ())
val f = Date.fmt "%Y-%m-%dT%H:%M:%S\n" d
in
print f
end

Common Lisp

(defun print-time ()
(multiple-value-bind (second minute hour day month year weekday ds tz)
(get-decoded-time)
(format t "~4,'0d-~2,'0d-~2,'0dT~2,'0d:~2,'0d:~2,'0d~2,'[email protected]"
year month day hour minute second tz)))

Edited by jcl

Share this post


Link to post
Share on other sites

my first code did not actually print the time, this does

import time
print time.ctime()

python sure seems simplest.

Edited by shanenin

Share this post


Link to post
Share on other sites

cmd.exe

> date /t

(*sh are shorter of course but AFAIK none of them have date as a built-in command.)

Edited by jcl

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this