[Class] midday.ahk

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Chunjee
Posts: 1421
Joined: 18 Apr 2014, 19:05
Contact:

[Class] midday.ahk

15 Jul 2021, 22:21

midday.ahk
Converts 24-hour (military) time string to 12-hour (meridiem) time string and vice versa.

Image Image Image

Installation
In a terminal or command line navigated to your project folder:

Code: Select all

npm install midday.ahk
In your code only export.ahk needs to be included:

Code: Select all

#Include %A_ScriptDir%\node_modules
#Include midday.ahk\export.ahk

midday := new midday()
result := midday.to24("12:30 AM")
; => "00:30"

result := midday.to12("00:30")
; => "12:30 AM"
You may also review or copy the library from ./export.ahk on GitHub; #Incude as you would normally when manually downloading.


API
Including the module provides a class midday with two methods: .to12 and .to24

to12(value)

Converts 24-hour (military) time string to 12-hour (meridiem) time string.

Arguments
value (String): the time string

Returns
(String): converted 24-hour time string to 12-hour time

Exceptions
throws an error if the time string is invalid

Example

Code: Select all

midday.to24("12:30 AM")
; => "00:30"


.to24(value)
Converts 12-hour (meridiem) time string to 24-hour (military) time string.

Arguments
value (String): the time string

Returns
(String): converted 24-hour time string to 12-hour time

Exceptions
throws an error if the time string is invalid

Examples

Code: Select all

midday.to12("00:30")
; => "12:30 AM"

midday.to12("0030")
; => "12:30 AM"

Meta Tags: meridiem, meridiem-time, military-time, midday-time, ante-meridiem, post-meridiem, am-time, pm-time
Last edited by Chunjee on 18 Apr 2024, 15:29, edited 8 times in total.
User avatar
Chunjee
Posts: 1421
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] midday.ahk

15 Jul 2021, 22:25

v0.1.0

Code: Select all

class midday
{
; --- Static Methods ---

	to24(param_time) {
		; prepare
		re := "^(([01][012])|(\d)):([012345]\d)\s?(a|p)m$"

		; check
		if (this._regexpTest(param_time, re)) {
			parts := this._regexpExec(param_time, re)

			switch (format(parts[5], "{:L}")) {
				case "p":
					if (parts[1] < 12) {
						hour := parts[1] + 12
					} else {
						hour := parts[1]
					}
				case "a":
					hour := parts[1]
					if (hour == 12) {
						hour := "00"
					}
			}
			if (strLen(hour) == 1) {
				hour := "0" hour
			}
			return hour ":" parts[4]
		}
		throw exception("Invalid time input: " param_time)
	}

	to12(param_time) {
		; prepare
		re := "^(([01]?\d)|(2[0123]))\D?([012345]\d)$"

		; create
		if (this._regexpTest(param_time, re)) {
			parts := this._regexpExec(param_time, re)
			hour := parts[1]
			min := parts[4]
			if (hour >= 12) {
				if (hour > 12) {
					hour := hour - 12
				}
				ampm := "PM"
			} else {
				ampm := "AM"
			}
			if (hour == "00") {
				hour := "12"
			}
			return hour ":" min " " ampm
		}

		throw exception("Invalid time input: " param_time)
	}

	_regexpTest(param_value, param_re) {
		try {
			; do something
			this._regExpExec(param_value, param_re)
		} catch error {
			return false
		}
		return true
	}

	_regexpExec(param_value, param_re) {
		RegExMatch(param_value, "i)" param_re, outputVar)
		if (outputVar == "") {
			throw exception("Regexp Error")
		} else {
			outputArray := []
			loop, 5 {
				outputArray.push(outputVar%A_Index%)
			}
			return outputArray
		}
	}
}

hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: [Class] midday.ahk

18 Jul 2021, 21:49

Thanks. This is great for some projects.
User avatar
Chunjee
Posts: 1421
Joined: 18 Apr 2014, 19:05
Contact:

Re: [Class] midday.ahk

01 Aug 2021, 13:32

To my knowledge this is the first package that officially credits Github Copilot as a contributor.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Drake and 182 guests