aboutsummaryrefslogtreecommitdiff
path: root/challenge-100/paulo-custodio/basic/ch-1.bas
blob: 35cc52653524939f822ffbebbab32e394c5b3f69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
' Challenge 100
'
' TASK #1 > Fun Time
' Submitted by: Mohammad S Anwar
' You are given a time (12 hour / 24 hour).
'
' Write a script to convert the given time from 12 hour format to 24 hour format
' and vice versa.
'
' Ideally we expect a one-liner.
'
' Example 1:
' Input: 05:15 pm or 05:15pm
' Output: 17:15
' Example 2:
' Input: 19:15
' Output: 07:15 pm or 07:15pm

function format_nn(n as integer) as string
    format_nn = right("00" & trim(str(n)), 2)
end function

function convert_time(text as string) as string
    dim hour as integer, minute as integer, am as boolean, pm as boolean
    dim ampm as string

    text = lcase(text)
    hour = val(text)
    minute = val(mid(text, instr(text, ":")+1))
    if instr(text, "am") > 0 then am = true
    if instr(text, "pm") > 0 then pm = true
    if am or pm then
        ' 12->24
        if pm then
            if hour < 12 then hour = hour + 12
        else
            if hour = 12 then hour = 0
        end if
        convert_time = format_nn(hour) & ":" & format_nn(minute)
    else
        ' 24->12

        ampm = "am"
        if hour = 0 then
            hour = 12
        elseif hour = 12 then
            ampm = "pm"
        elseif hour > 12 then
            hour = hour - 12
            ampm = "pm"
        end if
        convert_time = format_nn(hour) & ":" & format_nn(minute) & ampm
    end if
end function

print convert_time(command(1))