davalend Posted January 28, 2006 Report Share Posted January 28, 2006 how to write the code to display the current time ? Quote Link to post Share on other sites
shanenin Posted January 28, 2006 Report Share Posted January 28, 2006 what programming language do you want to use? in python, this would display your timeimport timetime.ctime().split()[3] Quote Link to post Share on other sites
jcl Posted January 28, 2006 Report Share Posted January 28, 2006 (edited) 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);}Javaimport 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")); }}SMLfun printTime () = let val d = Date.fromTimeLocal (Time.now ()) val f = Date.fmt "%Y-%m-%dT%H:%M:%S\n" d in print f endCommon 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,'0@d" year month day hour minute second tz))) Edited January 28, 2006 by jcl Quote Link to post Share on other sites
shanenin Posted January 28, 2006 Report Share Posted January 28, 2006 (edited) my first code did not actually print the time, this doesimport timeprint time.ctime()python sure seems simplest. Edited January 28, 2006 by shanenin Quote Link to post Share on other sites
Hai-Etlik Posted January 28, 2006 Report Share Posted January 28, 2006 Rubyputs Time.new Quote Link to post Share on other sites
jcl Posted January 29, 2006 Report Share Posted January 29, 2006 (edited) cmd.exe> date /t(*sh are shorter of course but AFAIK none of them have date as a built-in command.) Edited January 29, 2006 by jcl Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.