github.com/wtfutil/wtf@v0.43.0/modules/digitalclock/clocks.go (about)

     1  package digitalclock
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"time"
     7  )
     8  
     9  // AM defines the AM string format
    10  const AM = "A"
    11  
    12  // PM defines the PM string format
    13  const PM = "P"
    14  const minRowsForBorder = 3
    15  
    16  // Converts integer to string along with makes sure the length of string is > 2
    17  func intStrConv(val int) string {
    18  	valStr := strconv.Itoa(val)
    19  
    20  	if len(valStr) < 2 {
    21  		valStr = "0" + valStr
    22  	}
    23  	return valStr
    24  }
    25  
    26  // Returns Hour + minute + AM/PM information based on the settings
    27  func getHourMinute(hourFormat string) string {
    28  	strHours := intStrConv(time.Now().Hour())
    29  	AMPM := " "
    30  
    31  	if hourFormat == "12" {
    32  		hour := time.Now().Hour()
    33  		strHours = intStrConv(hour % 12)
    34  		if (hour % 12) == hour {
    35  			AMPM = AM
    36  		} else {
    37  			AMPM = PM
    38  		}
    39  
    40  	}
    41  
    42  	strMinutes := intStrConv(time.Now().Minute())
    43  	strMinutes += AMPM
    44  	return strHours + getColon() + strMinutes
    45  }
    46  
    47  // Returns the : with blinking based on the seconds
    48  func getColon() string {
    49  	if time.Now().Second()%2 == 0 {
    50  		return ":"
    51  	}
    52  	return " "
    53  }
    54  
    55  func getDate(dateFormat string, withDatePrefix bool) string {
    56  	if withDatePrefix {
    57  		return fmt.Sprintf("Date: %s", time.Now().Format(dateFormat))
    58  	}
    59  	return time.Now().Format(dateFormat)
    60  }
    61  
    62  func getUTC() string {
    63  	return fmt.Sprintf("UTC: %s", time.Now().UTC().Format(time.RFC3339))
    64  }
    65  
    66  func getEpoch() string {
    67  	return fmt.Sprintf("Epoch: %d", time.Now().Unix())
    68  }
    69  
    70  // Renders the clock as string by accessing appropriate font from configured in settings
    71  func renderClock(widgetSettings Settings) (string, bool) {
    72  	var digFont ClockFont
    73  	clockTime := getHourMinute(widgetSettings.hourFormat)
    74  	digFont = getFont(widgetSettings)
    75  
    76  	chars := [][]string{}
    77  	for _, char := range clockTime {
    78  		chars = append(chars, digFont.get(string(char)))
    79  	}
    80  
    81  	needBorder := digFont.fontRows <= minRowsForBorder
    82  	return fontsJoin(chars, digFont.fontRows, widgetSettings.color), needBorder
    83  }