<commit_before>/* This file is part of magic-sdk, an sdk for the open source programming language magic.
 *
 * Copyright (C) 2016 magic-lang
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

import os/win32

version(linux) {
	include unistd | (__USE_BSD), sys/time | (__USE_BSD), time | (__USE_BSD)
	//include unistd, sys/time, time
} else {
	include unistd, sys/time
}
version(windows) {
	include windows
}

version(windows) {
	SystemTime: cover from SYSTEMTIME {
		wYear, wMonth, wDayOfWeek, wDay, wHour, wMinute, wSecond, wMilliseconds : extern UShort
	}

	GetLocalTime: extern func (SystemTime*)
	QueryPerformanceCounter: extern func (LargeInteger*)
	QueryPerformanceFrequency: extern func (LargeInteger*)
	Sleep: extern func (UInt)

	LocaleId: cover from LCID
	LOCALE_USER_DEFAULT: extern LocaleId
	GetTimeFormat: extern func (LocaleId, Long, SystemTime*, CString, CString, Int) -> Int
	GetDateFormat: extern func (LocaleId, Long, SystemTime*, CString, CString, Int) -> Int
} else {
	TimeT: cover from time_t
	TimeZone: cover from struct timezone
	TMStruct: cover from struct tm {
		tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst : extern Int
	}
	TimeVal: cover from struct timeval {
		tv_sec: extern TimeT
		tv_usec: extern Int
	}

	time: extern proto func (TimeT*) -> TimeT
	localtime: extern func (TimeT*) -> TMStruct*
	gettimeofday: extern func (TimeVal*, TimeZone*) -> Int
	usleep: extern func (UInt)
	_asctime: extern (asctime) func (TMStruct*) -> CString

	// An `asctime` wrapper that copies the result to a new string. Otherwise, it would be overwritten in later calls.
	asctime: func (timePtr: TMStruct*) -> String {
		cStr := _asctime(timePtr)
		String new(cStr, cStr length() - 1)
	}
}

Time: class {
	__time_microsec_base := static This runTime()
	__time_millisec_base := static This __time_microsec_base / 1000

	dateTime: static func -> String {
		result: String
		version (windows) {
			dateLength := GetDateFormat(LOCALE_USER_DEFAULT, 0, null, null, null, 0)
			dateBuffer := calloc(1, dateLength) as Char*
			GetDateFormat(LOCALE_USER_DEFAULT, 0, null, null, dateBuffer, dateLength)
			date := String new(dateBuffer, dateLength - 1)

			timeLength := GetTimeFormat(LOCALE_USER_DEFAULT, 0, null, null, null, 0)
			timeBuffer := calloc(1, timeLength) as Char*
			GetTimeFormat(LOCALE_USER_DEFAULT, 0, null, null, timeBuffer, timeLength)
			time := String new(timeBuffer, timeLength - 1)

			result = "%s %s" format(date, time)
		} else {
			tm: TimeT
			time(tm&)
			result = asctime(localtime(tm&))
		}
		result
	}

	// The microseconds that have elapsed in the current minute.
	microtime: static func -> LLong {
		microsec() as LLong + (sec() as LLong) * 1_000_000
	}

	// The microseconds that have elapsed in the current second.
	microsec: static func -> UInt {
		result: UInt
		version(windows) {
			st: SystemTime
			GetLocalTime(st&)
			result = st wMilliseconds * 1000
		} else {
			tv : TimeVal
			gettimeofday(tv&, null)
			result = tv tv_usec
		}
		result
	}
	runTime: static func -> UInt {
		result: UInt
		version(windows) {
			counter, frequency: LargeInteger
			QueryPerformanceCounter(counter&)
			QueryPerformanceFrequency(frequency&)
			result = ((counter quadPart * 1000) / frequency quadPart) - __time_millisec_base
		} else {
			result = This runTimeMicro() / (1000 as UInt)
		}
		result
	}
	runTimeMicro: static func -> UInt {
		result: UInt
		version(!windows) {
			tv : TimeVal
			gettimeofday(tv&, null)
			result = ((tv tv_usec + tv tv_sec * 1_000_000) - __time_millisec_base) as UInt
		} else {
			result = This runTime() * (1000 as UInt)
		}
		result
	}
	measure: static func (action: Func) -> UInt {
		t1 := runTime()
		action()
		t2 := runTime()
		t2 - t1
	}
	// The seconds that have elapsed in the current minute
	sec: static func -> UInt {
		result: UInt
		version(windows) {
			st: SystemTime
			GetLocalTime(st&)
			result = st wSecond
		} else {
			tt := time(null)
			val := localtime(tt&)
			result = val@ tm_sec
		}<commit_msg>Further style fixes<commit_after>/* This file is part of magic-sdk, an sdk for the open source programming language magic.
 *
 * Copyright (C) 2016 magic-lang
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

import os/win32

version(linux) {
	include unistd | (__USE_BSD), sys/time | (__USE_BSD), time | (__USE_BSD)
	//include unistd, sys/time, time
} else {
	include unistd, sys/time
}
version(windows) {
	include windows
}

version(windows) {
	SystemTime: cover from SYSTEMTIME {
		wYear, wMonth, wDayOfWeek, wDay, wHour, wMinute, wSecond, wMilliseconds : extern UShort
	}

	GetLocalTime: extern func (SystemTime*)
	QueryPerformanceCounter: extern func (LargeInteger*)
	QueryPerformanceFrequency: extern func (LargeInteger*)
	Sleep: extern func (UInt)

	LocaleId: cover from LCID
	LOCALE_USER_DEFAULT: extern LocaleId
	GetTimeFormat: extern func (LocaleId, Long, SystemTime*, CString, CString, Int) -> Int
	GetDateFormat: extern func (LocaleId, Long, SystemTime*, CString, CString, Int) -> Int
} else {
	TimeT: cover from time_t
	TimeZone: cover from struct timezone
	TMStruct: cover from struct tm {
		tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst : extern Int
	}
	TimeVal: cover from struct timeval {
		tv_sec: extern TimeT
		tv_usec: extern Int
	}

	time: extern proto func (TimeT*) -> TimeT
	localtime: extern func (TimeT*) -> TMStruct*
	gettimeofday: extern func (TimeVal*, TimeZone*) -> Int
	usleep: extern func (UInt)
	_asctime: extern (asctime) func (TMStruct*) -> CString

	// An `asctime` wrapper that copies the result to a new string. Otherwise, it would be overwritten in later calls.
	asctime: func (timePtr: TMStruct*) -> String {
		cStr := _asctime(timePtr)
		String new(cStr, cStr length() - 1)
	}
}

Time: class {
	__time_microsec_base := static This runTime()
	__time_millisec_base := static This __time_microsec_base / 1000

	dateTime: static func -> String {
		result: String
		version (windows) {
			dateLength := GetDateFormat(LOCALE_USER_DEFAULT, 0, null, null, null, 0)
			dateBuffer := calloc(1, dateLength) as Char*
			GetDateFormat(LOCALE_USER_DEFAULT, 0, null, null, dateBuffer, dateLength)
			date := String new(dateBuffer, dateLength - 1)

			timeLength := GetTimeFormat(LOCALE_USER_DEFAULT, 0, null, null, null, 0)
			timeBuffer := calloc(1, timeLength) as Char*
			GetTimeFormat(LOCALE_USER_DEFAULT, 0, null, null, timeBuffer, timeLength)
			time := String new(timeBuffer, timeLength - 1)

			result = "%s %s" format(date, time)
		} else {
			tm: TimeT
			time(tm&)
			result = asctime(localtime(tm&))
		}
		result
	}

	// The microseconds that have elapsed in the current minute.
	microtime: static func -> LLong {
		This microsec() + This sec() * 1_000_000LL
	}

	// The microseconds that have elapsed in the current second.
	microsec: static func -> UInt {
		result: UInt
		version(windows) {
			st: SystemTime
			GetLocalTime(st&)
			result = st wMilliseconds * 1000
		} else {
			tv : TimeVal
			gettimeofday(tv&, null)
			result = tv tv_usec
		}
		result
	}
	runTime: static func -> UInt {
		result: UInt
		version(windows) {
			counter, frequency: LargeInteger
			QueryPerformanceCounter(counter&)
			QueryPerformanceFrequency(frequency&)
			result = ((counter quadPart * 1000) / frequency quadPart) - __time_millisec_base
		} else {
			result = This runTimeMicro() / 1000U
		}
		result
	}
	runTimeMicro: static func -> UInt {
		result: UInt
		version(!windows) {
			tv : TimeVal
			gettimeofday(tv&, null)
			result = ((tv tv_usec + tv tv_sec * 1_000_000) - __time_millisec_base) as UInt
		} else {
			result = This runTime() * 1000U
		}
		result
	}
	measure: static func (action: Func) -> UInt {
		t1 := runTime()
		action()
		t2 := runTime()
		t2 - t1
	}
	// The seconds that have elapsed in the current minute
	sec: static func -> UInt {
		result: UInt
		version(windows) {
			st: SystemTime
			GetLocalTime(st&)
			result = st wSecond
		} else {
			tt := time(null)
			val := localtime(tt&)
			result = val@ tm_sec
		}
{'fraction_non_alphanumeric': 0.10564930300807043, 'fraction_numerical': 0.013450721447786746, 'mean_word_length': 3.8482513337285122, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5036253', 'n_tokens_mistral': 3138, 'n_tokens_neox': 2790, 'n_words': 878}