How to get timestamp in Monkey 2 with libc and in C

This morning I got implemented a little code, that can be used to get timestamp based on ANSI C code using Monkey 2 programming language’s libc.

Let’s take first look at the Monkey 2 code:

Namespace myapp

#Import "<std>"
#Import "<libc>"

Using std..
Using libc..

Function Main()

		Local seconds:time_t
		Local p:tm_t Ptr
		Local date:String
		
		libc.time(Varptr seconds)
	
		p = localtime(Varptr seconds)
		
		date = String(p->tm_sec) + "-" + String(p->tm_min) + "-" + String(p->tm_hour) + "-" + String(p->tm_mday) + "-" + String((p->tm_mon)+1) + "-" + String(1900 + p->tm_year)
		
		Print date
	
End

At the moment the output of the program is as follows: 40-31-8-26-10-2018

Year 2018, month 10, day 26, hour 8, min 31, sec 40.

Reversed order might be more useful in practice…

Let’s take a look at the C version:

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

void Date (char date[10+1]);

void main (void) {

	char datum[10+1];
	
	Date(datum);
	printf("%s",datum);

}


void Date (char date[10+1]) {
	time_t seconds;
	struct tm *p;
	
	time (&seconds);
	p = localtime(&seconds);
	
	sprintf(date, "%02i.%02i.%02i",
		p->tm_mday,
		p->tm_mon + 1,
		p->tm_year+1900);
}

To the year is again added 1900. This gives following output: 26.10.2018

The tm_mon is between 0..11, tm_year between 1900 + 0..n.

That’s it for now, lots of things to do…