v1.0.0: refactored and hardened existing code, added python support, added man pages and shell completions, added makefile
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,33 @@
+# Compiled Binaries
+bin/
+
+# Python
+__pycache__/
+*.py[cod]
+*$py.class
+
+# Object files and archives
+*.o
+*.a
+*.so
+
+# OS generated files
+.DS_Store
+.directory
+*~
+*.swp
+
+# Arch build artifacts & metadata
+pkg/
+src/
+aika-*/
+*.tar.gz
+*.pkg.tar.zst
+*.pkg.tar.zst.sig
+
+# scdoc generated man pages
+man/arvelie.1
+man/neralie.1
+
+# Development tools
+.helix/
diff --git a/LICENSE b/LICENSE
@@ -1,9 +1,24 @@
This is free and unencumbered software released into the public domain.
-Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
-In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
-For more information, please refer to <http://unlicense.org/>
+For more information, please refer to <https://unlicense.org/>
diff --git a/Makefile b/Makefile
@@ -0,0 +1,74 @@
+CC = gcc
+VERSION = $(shell cat VERSION)
+PREFIX = /usr/local
+SCDOC = scdoc
+
+CFLAGS = -Wall -Wextra -O2 -pedantic \
+ -D_FORTIFY_SOURCE=2 -fstack-protector-strong \
+ -fPIE -Wl,-pie \
+ -DVERSION=\"$(VERSION)\" -DPREFIX=\"$(PREFIX)\"
+
+BIN_DIR = bin
+C_DIR = c
+SH_DIR = sh
+PY_DIR = py
+MAN_DIR = man
+COMP_DIR = completions
+
+MANDIR = $(PREFIX)/share/man/man1
+BASH_COMP_DIR = $(PREFIX)/share/bash-completion/completions
+ZSH_COMP_DIR = $(PREFIX)/share/zsh/site-functions
+
+TARGETS = $(BIN_DIR)/arvelie $(BIN_DIR)/neralie
+MAN_PAGES = $(MAN_DIR)/arvelie.1 $(MAN_DIR)/neralie.1
+
+.PHONY: all clean scripts install uninstall
+
+all: $(TARGETS) scripts $(MAN_PAGES)
+
+$(BIN_DIR):
+ mkdir -p $(BIN_DIR)
+
+$(BIN_DIR)/%: $(C_DIR)/%.c | $(BIN_DIR)
+ $(CC) $(CFLAGS) $< -o $@
+
+$(MAN_DIR)/%.1: $(MAN_DIR)/%.1.scd
+ $(SCDOC) < $< > $@
+
+scripts:
+ chmod +x $(SH_DIR)/*.sh
+ chmod +x $(PY_DIR)/*.py
+
+install: all
+ mkdir -p $(DESTDIR)$(PREFIX)/bin
+ mkdir -p $(DESTDIR)$(MANDIR)
+ mkdir -p $(DESTDIR)$(BASH_COMP_DIR)
+ mkdir -p $(DESTDIR)$(ZSH_COMP_DIR)
+
+ cp $(BIN_DIR)/arvelie $(DESTDIR)$(PREFIX)/bin/arvelie
+ cp $(BIN_DIR)/neralie $(DESTDIR)$(PREFIX)/bin/neralie
+ cp $(MAN_DIR)/arvelie.1 $(DESTDIR)$(MANDIR)/arvelie.1
+ cp $(MAN_DIR)/neralie.1 $(DESTDIR)$(MANDIR)/neralie.1
+
+ cp $(COMP_DIR)/aika.bash $(DESTDIR)$(BASH_COMP_DIR)/arvelie
+ ln -sf arvelie $(DESTDIR)$(BASH_COMP_DIR)/neralie
+
+ cp $(COMP_DIR)/_aika.zsh $(DESTDIR)$(ZSH_COMP_DIR)/_arvelie
+ ln -sf _arvelie $(DESTDIR)$(ZSH_COMP_DIR)/_neralie
+
+ @echo "Installed binaries, man pages, and completions ($(VERSION)) to $(PREFIX)"
+
+uninstall:
+ rm -f $(DESTDIR)$(PREFIX)/bin/arvelie
+ rm -f $(DESTDIR)$(PREFIX)/bin/neralie
+ rm -f $(DESTDIR)$(MANDIR)/arvelie.1
+ rm -f $(DESTDIR)$(MANDIR)/neralie.1
+ rm -f $(DESTDIR)$(BASH_COMP_DIR)/arvelie
+ rm -f $(DESTDIR)$(BASH_COMP_DIR)/neralie
+ rm -f $(DESTDIR)$(ZSH_COMP_DIR)/_arvelie
+ rm -f $(DESTDIR)$(ZSH_COMP_DIR)/_neralie
+ @echo "Uninstalled binaries, man pages, and completions"
+
+clean:
+ rm -rf $(BIN_DIR)
+ rm -f $(MAN_PAGES)
diff --git a/README.md b/README.md
@@ -2,20 +2,52 @@
## About
-Aika is a repository of mine dedicated to Arvelie and Neralie utilities, in an effort to make these time systems more accessible.
+Aika is a repository dedicated to Arvelie and Neralie utilities, aimed at making these time systems more accessible.
-I'm only comfortable with coding in C, and scripting in POSIX compliant shell, so that's likely all that will be offered here unless I learn other languages well enough to port to them.
+The repo is split into `c/`, `sh/`, and `py/` directories. I am currently focused on providing C, Python, and POSIX-compliant shell scripts here.
-I'd consider myself below average at programming in a general sense, so it's likely that some problems will be ran into every so often with these implementations, if something unexpected occurs. If that does happen, please feel free to reach out to me via email at git@faen.sh.
+I consider myself a beginner programmer. If you encounter unexpected behavior or have suggestions for optimization, please reach out via email at git@faen.sh.
-## Compiling
+## Dependencies
-To compile C source code, grab `gcc` from your package manager and run `gcc <source_file.c> -o <desired binary name>`
+### Build Dependencies
+`gcc` (for C binaries)
-## Conributions
+`make` (for build automation)
-Contributions are welcome, of course! I would love for people to fork this work and expand on it, and make Arvelie and Neralie more accessible! That's my goal here.
+`scdoc` (for generating man pages)
-## License
+### Runtime Dependencies
+`python3` (required for Python implementations)
+
+`sh` (any POSIX-compliant shell)
+
+## Installation
+
+### On Arch Linux
+This repo will soon include a `PKGBUILD`, and Arch Linux users can install it as a native package by running `makepkg -si`. It will soon be available in the AUR.
+
+### Manual Installation
+To compile manually and install to `/usr/local`, run `sudo make install`.
+
+This will install the binaries, man pages, and shell completions.
+
+## Usage
+The C and Python utilities support advanced features like live clocks and specific date/time parsing.
-This software is licensed under the Unlicense. For more information, please visit https://unlicense.org/.
+`arvelie --help` to display help
+
+`neralie --count` will run a continuous clock
+
+The shell scripts in `sh/` are strictly minimal, providing basic current time/date conversion only.
+
+## Examples
+`arvelie 2021-01-08` will convert 2021-01-08 to Arvelie format
+
+`neralie 14:30` will convert 14:30 to Neralie format
+
+## Contributions
+Contributions and forks are highly encouraged! My goal is to expand accessibility to these time systems through community collaboration.
+
+## License
+Licensed under the Unlicense. See https://unlicense.org/ for details.
diff --git a/VERSION b/VERSION
@@ -0,0 +1 @@
+1.0.0
diff --git a/arvelie.c b/arvelie.c
@@ -1,28 +0,0 @@
-#include <stdio.h>
-#include <time.h>
-
-void prArvelie() {
- time_t t = time(NULL);
- struct tm *tm = localtime(&t);
- int doy = tm->tm_yday + 1;
- int y = tm->tm_year % 100;
- int yFull = tm->tm_year + 1900;
- int ly = (yFull % 4 == 0 && yFull % 100 != 0) || (yFull % 400 == 0);
- char mList[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
-
- if (doy == 365) {
- printf("%02d+00\n", y);
- } else if (doy == 366 && ly) {
- printf("%02d+01\n", y);
- } else {
- if (ly && doy > 365) {
- doy--;
- }
- printf("%02d%c%02d\n", y, mList[(doy - 1) / 14], (doy - 1) % 14 + 1);
- }
-}
-
-int main() {
- prArvelie();
- return 0;
-}
diff --git a/arvelie.sh b/arvelie.sh
@@ -1,23 +0,0 @@
-#!/bin/sh
-
-doy=$(date +%j)
-y=$(date +%y)
-yFull=$(date +%Y)
-ly=$(( (yFull % 4 == 0 && yFull % 100 != 0) || yFull % 400 == 0 ))
-mList="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
-
-set -- $mList
-
-if [ "$doy" -eq 365 ]; then
- echo "$y+00"
-elif [ "$doy" -eq 366 ] && [ "$ly" -eq 1 ]; then
- echo "$y+01"
-else
- if [ "$ly" -eq 1 ] && [ "$doy" -gt 365 ]; then
- doy=$((doy - 1))
- fi
- mIndex=$(( (doy - 1) / 14 ))
- dom=$(( (doy - 1) % 14 + 1 ))
- eval mLetter=\$"{$((mIndex + 1))}"
- printf "%s%s%02d\n" "$y" "$mLetter" "$dom"
-fi
diff --git a/c/arvelie.c b/c/arvelie.c
@@ -0,0 +1,98 @@
+#include <stdio.h>
+#include <time.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sysexits.h>
+
+// Prints help/usage
+void printHelp(const char *progName) {
+ printf("Arvelie %s\n", VERSION);
+ printf("Usage: %s [OPTION]... [DATE]\n", progName);
+ printf("Convert the current date, or a specified date, to Arvelie format.\n\n");
+ printf("Options:\n");
+ printf(" -h, --help display this help and exit\n");
+ printf(" -v, --version display version information and exit\n\n");
+ printf("Arguments:\n");
+ printf(" DATE convert a specific date (formats: YYYY-MM-DD, YYYYMMDD)\n");
+ printf(" If omitted, prints the current local date.\n\n");
+ printf("Examples:\n");
+ printf(" %s # Print current Arvelie date\n", progName);
+ printf(" %s 2021-01-08 # Convert January 8, 2021 to Arvelie\n", progName);
+}
+
+// Core conversion logic
+void printArvelie(int yearFull, int doy) {
+ int y = yearFull % 100;
+ const char mList[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+ if (doy == 365) {
+ printf("%02d+00\n", y); // Year Day
+ } else if (doy == 366) {
+ printf("%02d+01\n", y); // Leap Day
+ } else {
+ int mIndex = (doy - 1) / 14;
+ int dom = (doy - 1) % 14 + 1;
+ printf("%02d%c%02d\n", y, mList[mIndex], dom);
+ }
+}
+
+void parseAndConvert(const char *input) {
+ int y = 0, m = 0, d = 0;
+ if (sscanf(input, "%4d-%2d-%2d", &y, &m, &d) == 3 ||
+ sscanf(input, "%4d%2d%2d", &y, &m, &d) == 3) {
+
+ if (m < 1 || m > 12 || d < 1 || d > 31) {
+ fprintf(stderr, "Error: Invalid date values. Expected MM 01-12, DD 01-31.\n");
+ exit(EX_DATAERR);
+ }
+
+ struct tm time_in = {0};
+ time_in.tm_year = y - 1900;
+ time_in.tm_mon = m - 1;
+ time_in.tm_mday = d;
+ time_in.tm_isdst = -1;
+
+ if (mktime(&time_in) == -1) {
+ fprintf(stderr, "Error: Could not process date.\n");
+ exit(EX_DATAERR);
+ }
+
+ printArvelie(y, time_in.tm_yday + 1);
+ } else {
+ fprintf(stderr, "Error: Invalid date format. Use YYYY-MM-DD.\n");
+ exit(EX_USAGE);
+ }
+}
+
+void prArvelie(void) {
+ time_t t = time(NULL);
+ struct tm *tm = localtime(&t);
+ printArvelie(tm->tm_year + 1900, tm->tm_yday + 1);
+}
+
+int main(int argc, char *argv[]) {
+ if (argc > 1) {
+ for (int i = 1; i < argc; i++) {
+ if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
+ printHelp(argv[0]);
+ return 0;
+ }
+ if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
+ printf("Arvelie %s\n", VERSION);
+ return 0;
+ }
+ }
+
+ if (argv[1][0] != '-') {
+ parseAndConvert(argv[1]);
+ return 0;
+ } else {
+ fprintf(stderr, "Error: Unknown argument '%s'\n", argv[1]);
+ fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
+ exit(EX_USAGE);
+ }
+ }
+
+ prArvelie();
+ return 0;
+}
diff --git a/c/neralie.c b/c/neralie.c
@@ -0,0 +1,153 @@
+#define _POSIX_C_SOURCE 199309L
+
+#include <stdio.h>
+#include <time.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <signal.h>
+#include <sysexits.h>
+
+static volatile sig_atomic_t keepRunning = 1;
+
+void restoreCursor(int sig) {
+ (void)sig;
+ printf("\033[?25h\n");
+ fflush(stdout);
+ exit(0);
+}
+
+// Prints help/usage
+void printHelp(const char *progName) {
+ printf("Neralie %s\n", VERSION);
+ printf("Usage: %s [OPTION]... [TIME]\n", progName);
+ printf("Convert standard time to Neralie, or run a continuous clock.\n\n");
+ printf("Options:\n");
+ printf(" -c, --count run in continuous count mode\n");
+ printf(" -h, --help display this help and exit\n");
+ printf(" -v, --version display version information and exit\n\n");
+ printf("Arguments:\n");
+ printf(" TIME convert a specific time (formats: HH:MM:SS, HHMMSS, HH:MM, HHMM, HH)\n");
+ printf(" If omitted, prints the current local time.\n\n");
+ printf("Examples:\n");
+ printf(" %s # Print current Neralie time\n", progName);
+ printf(" %s -c # Run live Neralie clock\n", progName);
+ printf(" %s 14:30 # Convert 14:30:00 to Neralie\n", progName);
+ printf(" %s 143000 # Convert 14:30:00 to Neralie\n", progName);
+}
+
+// Converts standard time to Neralie and prints it
+void printNeralie(int h, int m, int s, long ns) {
+ long ms = (h * 3600 + m * 60 + s) * 1000 + (ns / 1000000);
+ long nera = (ms * 5) / 432;
+ printf("%03ld:%03ld", nera / 1000, nera % 1000);
+}
+
+void prNera(void) {
+ struct timespec ts;
+ struct tm *tm;
+ time_t t;
+
+ clock_gettime(CLOCK_REALTIME, &ts);
+ t = ts.tv_sec;
+ tm = localtime(&t);
+
+ printNeralie(tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec);
+}
+
+void countMode(void) {
+ struct timespec req = {0, 86400000};
+ printf("\033[?25l");
+
+ signal(SIGINT, restoreCursor);
+ signal(SIGTERM, restoreCursor);
+
+ while (keepRunning) {
+ printf("\r");
+ prNera();
+ fflush(stdout);
+ nanosleep(&req, NULL);
+ }
+
+ restoreCursor(0);
+}
+
+void parseAndConvert(const char *input) {
+ int h = 0, m = 0, s = 0;
+ char clean[32];
+ int j = 0;
+
+ for (int i = 0; input[i] != '\0'; i++) {
+ if (!isspace((unsigned char)input[i])) {
+ if (j < (int)sizeof(clean) - 1) {
+ clean[j++] = input[i];
+ }
+ }
+ }
+ clean[j] = '\0';
+
+ if (strchr(clean, ':') != NULL) {
+ int count = sscanf(clean, "%d:%d:%d", &h, &m, &s);
+ if (count == 2) {
+ s = 0;
+ }
+ } else {
+ int len = strlen(clean);
+ switch (len) {
+ case 6: sscanf(clean, "%2d%2d%2d", &h, &m, &s); break;
+ case 5: sscanf(clean, "%1d%2d%2d", &h, &m, &s); break;
+ case 4: sscanf(clean, "%2d%2d", &h, &m); break;
+ case 3: sscanf(clean, "%1d%2d", &h, &m); break;
+ case 2: sscanf(clean, "%2d", &h); break;
+ case 1: sscanf(clean, "%1d", &h); break;
+ default:
+ fprintf(stderr, "Error: Invalid time format. Use HH:MM[:SS] or HHMM[SS].\n");
+ exit(EX_USAGE);
+ }
+ }
+
+ if (h < 0 || h > 23 || m < 0 || m > 59 || s < 0 || s > 59) {
+ fprintf(stderr, "Error: Invalid time values (Expected 00-23 for hours, 00-59 for min/sec).\n");
+ exit(EX_DATAERR);
+ }
+
+ printNeralie(h, m, s, 0);
+ printf("\n");
+}
+
+int main(int argc, char *argv[]) {
+ if (argc > 1) {
+ for (int i = 1; i < argc; i++) {
+ if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
+ printHelp(argv[0]);
+ return 0;
+ }
+ if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
+ printf("Neralie %s\n", VERSION);
+ return 0;
+ }
+ }
+
+ int countRequested = 0;
+ if (argv[1][0] == '-' && argv[1][1] != '-') {
+ for (int j = 1; argv[1][j] != '\0'; j++) {
+ if (argv[1][j] == 'c') countRequested = 1;
+ }
+ } else if (strcmp(argv[1], "--count") == 0) {
+ countRequested = 1;
+ }
+
+ if (countRequested) {
+ countMode();
+ } else if (argv[1][0] != '-') {
+ parseAndConvert(argv[1]);
+ } else {
+ fprintf(stderr, "Error: Unknown argument '%s'\n", argv[1]);
+ exit(EX_USAGE);
+ }
+ } else {
+ prNera();
+ printf("\n");
+ }
+ return 0;
+}
diff --git a/completions/_aika.zsh b/completions/_aika.zsh
@@ -0,0 +1,22 @@
+#compdef arvelie neralie
+
+_aika() {
+ local line
+ case $service in
+ arvelie)
+ _arguments -s -S \
+ '(-h --help)'{-h,--help}'[Display help message]' \
+ '(-v --version)'{-v,--version}'[Display version information]' \
+ ':date: '
+ ;;
+ neralie)
+ _arguments -s -S \
+ '(-c --count)'{-c,--count}'[Run in continuous count mode]' \
+ '(-h --help)'{-h,--help}'[Display help message]' \
+ '(-v --version)'{-v,--version}'[Display version information]' \
+ ':time: '
+ ;;
+ esac
+}
+
+_aika "$@"
diff --git a/completions/aika.bash b/completions/aika.bash
@@ -0,0 +1,21 @@
+# Bash completion for Aika
+_aika_completions() {
+ local cur prev opts
+ COMPREPLY=()
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ prev="${COMP_WORDS[COMP_CWORD-1]}"
+
+ case "${COMP_WORDS[0]}" in
+ arvelie)
+ opts="-h --help -v --version"
+ ;;
+ neralie)
+ opts="-h --help -v --version -c --count"
+ ;;
+ esac
+
+ COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
+ return 0
+}
+
+complete -F _aika_completions arvelie neralie
diff --git a/man/arvelie.1.scd b/man/arvelie.1.scd
@@ -0,0 +1,46 @@
+arvelie(1) "Arvelie v1.0.0" "Arvelie Manual"
+
+# NAME
+
+arvelie - convert dates to the Arvelie calendar format
+
+# SYNOPSIS
+
+*arvelie* [OPTION]... [DATE]
+
+# DESCRIPTION
+
+*arvelie* is a utility for converting standard Gregorian dates into the Arvelie
+calendar format (YYMDD). If no date is provided, it defaults to the current
+local date.
+
+# OPTIONS
+
+*-h, --help*
+ Display a help message and exit.
+
+*-v, --version*
+ Display version information and exit.
+
+# ARGUMENTS
+
+*DATE*
+ A specific date to convert. Supported formats include YYYY-MM-DD
+ and YYYYMMDD.
+
+# EXAMPLES
+
+*arvelie*
+ Print the current Arvelie date.
+
+*arvelie 2021-01-08*
+ Convert January 8, 2021 (Uxn release) to Arvelie.
+
+# AUTHOR
+
+Written by faen <git@faen.sh>.
+
+# COPYRIGHT
+
+This is free and unencumbered software released into the public domain.
+For more information, please refer to <https://unlicense.org/>
diff --git a/man/neralie.1.scd b/man/neralie.1.scd
@@ -0,0 +1,47 @@
+neralie(1) "Neralie v1.0.0" "Neralie Manual"
+
+# NAME
+
+neralie - convert time to the Neralie decimal format
+
+# SYNOPSIS
+
+*neralie* [OPTION]... [TIME]
+
+# DESCRIPTION
+
+*neralie* converts standard 24-hour time into the Neralie decimal format (000:000).
+If no time is provided, it defaults to the current local time.
+
+# OPTIONS
+
+*-c, --count*
+ Run in continuous count mode, updating the time every Neralie
+ millisecond (approx. 86.4ms).
+
+*-h, --help*
+ Display a help message and exit.
+
+*-v, --version*
+ Display version information and exit.
+
+# ARGUMENTS
+
+*TIME*
+ A specific time to convert. Supported formats: HH:MM:SS, HHMMSS,
+ HH:MM, HHMM, or HH.
+
+# EXAMPLES
+
+*neralie*
+ Print the current Neralie time.
+
+*neralie -c*
+ Run a live Neralie clock in the terminal.
+
+*neralie 14:30*
+ Convert 14:30 to Neralie.
+
+# AUTHOR
+
+Written by faen <git@faen.sh>.
diff --git a/neralie.c b/neralie.c
@@ -1,23 +0,0 @@
-#include <stdio.h>
-#include <time.h>
-
-void prNera() {
- struct timespec ts;
- struct tm *tm;
- time_t t;
-
- clock_gettime(CLOCK_REALTIME, &ts);
- t = ts.tv_sec;
- tm = localtime(&t);
-
- long ms = tm->tm_hour * 3600000 + tm->tm_min * 60000 + tm->tm_sec * 1000 + ts.tv_nsec / 1000000;
- int beat = ms / 86400;
- int frac = ms % 86400;
-
- printf("%03d:%03d\n", beat, frac * 1000 / 86400);
-}
-
-int main() {
- prNera();
- return 0;
-}
diff --git a/neralie.sh b/neralie.sh
@@ -1,9 +0,0 @@
-#!/bin/sh
-
-h=$(date +%k)
-m=$(date +%-M)
-s=$(date +%S)
-ms=$(( (h*3600 + m*60 + s) * 1000 ))
-nera=$((ms * 1000 / 86400))
-
-printf "%03d:%03d\n" $((nera / 1000)) $((nera % 1000))
diff --git a/py/arvelie.py b/py/arvelie.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python3
+
+import sys
+import argparse
+from datetime import datetime
+
+def to_arvelie(year_full, doy):
+ y = year_full % 100
+ letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+
+ if doy == 365:
+ return f"{y:02d}+00"
+ elif doy == 366:
+ return f"{y:02d}+01"
+ else:
+ m_index = (doy - 1) // 14
+ dom = (doy - 1) % 14 + 1
+ return f"{y:02d}{letters[m_index]}{dom:02d}"
+
+def main():
+ parser = argparse.ArgumentParser(
+ description="Convert the current date, or a specified date, to Arvelie format.",
+ epilog="Examples:\n"
+ " %(prog)s # Print current Arvelie date\n"
+ " %(prog)s 2021-01-08 # Convert January 8, 2021 to Arvelie",
+ formatter_class=argparse.RawDescriptionHelpFormatter
+ )
+
+ parser.add_argument("-v", "--version", action="version", version="Arvelie 1.0.0")
+
+ parser.add_argument(
+ "date",
+ metavar="DATE",
+ nargs="?",
+ help="convert a specific date (formats: YYYY-MM-DD, YYYYMMDD)\n"
+ "If omitted, prints the current local date."
+ )
+
+ args = parser.parse_args()
+
+ if args.date:
+ try:
+ if "-" in args.date:
+ dt = datetime.strptime(args.date, "%Y-%m-%d")
+ else:
+ dt = datetime.strptime(args.date, "%Y%m%d")
+ except ValueError:
+ sys.stderr.write("Error: Invalid date format. Use YYYY-MM-DD.\n")
+ sys.exit(1)
+ else:
+ dt = datetime.now()
+
+ print(to_arvelie(dt.year, dt.timetuple().tm_yday))
+
+if __name__ == "__main__":
+ main()
diff --git a/py/neralie.py b/py/neralie.py
@@ -0,0 +1,97 @@
+#!/usr/bin/env python3
+
+import sys
+import time
+import argparse
+import re
+from datetime import datetime
+
+def to_neralie(h, m, s, ms=0):
+ total_ms = (h * 3600 + m * 60 + s) * 1000 + ms
+ nera = (total_ms * 5) // 432
+ return f"{nera // 1000:03d}:{nera % 1000:03d}"
+
+def count_mode():
+ sys.stdout.write("\033[?25l")
+ try:
+ while True:
+ now = datetime.now()
+ ms = now.microsecond // 1000
+ sys.stdout.write(f"\r{to_neralie(now.hour, now.minute, now.second, ms)}")
+ sys.stdout.flush()
+ time.sleep(0.0864)
+ except KeyboardInterrupt:
+ pass
+ finally:
+ sys.stdout.write("\033[?25h\n")
+ sys.stdout.flush()
+
+def parse_time(time_str):
+ clean = re.sub(r'\s+', '', time_str)
+ h, m, s = 0, 0, 0
+
+ if ':' in clean:
+ parts = clean.split(':')
+ try:
+ h = int(parts[0])
+ if len(parts) > 1: m = int(parts[1])
+ if len(parts) > 2: s = int(parts[2])
+ except (ValueError, IndexError):
+ sys.stderr.write("Error: Invalid time format.\n")
+ sys.exit(1)
+ else:
+ try:
+ length = len(clean)
+ if length == 6: h, m, s = int(clean[0:2]), int(clean[2:4]), int(clean[4:6])
+ elif length == 5: h, m, s = int(clean[0:1]), int(clean[1:3]), int(clean[3:5])
+ elif length == 4: h, m = int(clean[0:2]), int(clean[2:4])
+ elif length == 3: h, m = int(clean[0:1]), int(clean[1:3])
+ elif length == 2: h = int(clean)
+ elif length == 1: h = int(clean)
+ else: raise ValueError
+ except ValueError:
+ sys.stderr.write("Error: Invalid time format. Use HH:MM[:SS] or HHMM[SS].\n")
+ sys.exit(1)
+
+ if not (0 <= h <= 23 and 0 <= m <= 59 and 0 <= s <= 59):
+ sys.stderr.write("Error: Invalid time values (Expected 00-23 for hours, 00-59 for min/sec).\n")
+ sys.exit(1)
+
+ return h, m, s
+
+def main():
+ parser = argparse.ArgumentParser(
+ description="Convert standard time to Neralie, or run a continuous clock.",
+ epilog="Examples:\n"
+ " %(prog)s # Print current Neralie time\n"
+ " %(prog)s -c # Run live Neralie clock\n"
+ " %(prog)s 14:30 # Convert 14:30:00 to Neralie\n"
+ " %(prog)s 143000 # Convert 14:30:00 to Neralie",
+ formatter_class=argparse.RawDescriptionHelpFormatter
+ )
+
+ parser.add_argument("-v", "--version", action="version", version="Neralie 1.0.0")
+
+ parser.add_argument("-c", "--count", action="store_true", help="run in continuous count mode")
+ parser.add_argument(
+ "time",
+ metavar="TIME",
+ nargs="?",
+ help="convert a specific time (formats: HH:MM:SS, HHMMSS, HH:MM, HHMM, HH)\n"
+ "If omitted, prints the current local time."
+ )
+
+ args = parser.parse_args()
+
+ if args.count:
+ count_mode()
+ elif args.time:
+ h, m, s = parse_time(args.time)
+ print(to_neralie(h, m, s))
+ else:
+ now = datetime.now()
+ ms = now.microsecond // 1000
+ print(to_neralie(now.hour, now.minute, now.second, ms))
+
+if __name__ == "__main__":
+ main()
diff --git a/sh/arvelie.sh b/sh/arvelie.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+doy=$(date +%j | sed 's/^0*//')
+y=$(date +%y)
+
+letters="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+
+if [ "$doy" -eq 365 ]; then
+ printf "%s+00\n" "$y"
+elif [ "$doy" -eq 366 ]; then
+ printf "%s+01\n" "$y"
+else
+ mIndex=$(( (doy - 1) / 14 ))
+ dom=$(( (doy - 1) % 14 + 1 ))
+ mChar=$(echo "$letters" | cut -c $((mIndex + 1)))
+ printf "%s%s%02d\n" "$y" "$mChar" "$dom"
+fi
diff --git a/sh/neralie.sh b/sh/neralie.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+h=$(date +%H | sed 's/^0*//'); h=${h:-0}
+m=$(date +%M | sed 's/^0*//'); m=${m:-0}
+s=$(date +%S | sed 's/^0*//'); s=${s:-0}
+
+ms=$(( (h*3600 + m*60 + s) * 1000 ))
+
+nera=$((ms * 5 / 432))
+
+printf "%03d:%03d\n" $((nera / 1000)) $((nera % 1000))