Current Time


Recommended Posts

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,'0@d"
year month day hour minute second tz)))

Edited by jcl
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...