2023-10-07 16:32:53 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-10-23 23:45:23 +02:00
|
|
|
"encoding/json"
|
2023-10-07 16:32:53 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-10-23 23:45:23 +02:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2023-10-10 21:47:08 +02:00
|
|
|
)
|
2023-10-07 16:32:53 +02:00
|
|
|
|
2023-10-23 23:45:23 +02:00
|
|
|
var portToListenOn int = 9100
|
|
|
|
var defaultTZ string = "Europe/Berlin"
|
2023-10-07 16:32:53 +02:00
|
|
|
|
2023-10-23 23:45:23 +02:00
|
|
|
type DateTimeResponse struct {
|
|
|
|
Year int `json:"year,omitempty"`
|
|
|
|
Month int `json:"month,omitempty"`
|
|
|
|
Day int `json:"day,omitempty"`
|
|
|
|
Hour int `json:"hour,omitempty"`
|
|
|
|
Minute int `json:"minute,omitempty"`
|
|
|
|
Seconds int `json:"seconds,omitempty"`
|
|
|
|
MilliSeconds int `json:"milliSeconds,omitempty"`
|
|
|
|
DateTime string `json:"dateTime,omitempty"`
|
|
|
|
Date string `json:"date,omitempty"`
|
|
|
|
Time string `json:"time,omitempty"`
|
|
|
|
TimeZone string `json:"timeZone,omitempty"`
|
|
|
|
DayOfWeek string `json:"dayOfWeek,omitempty"`
|
|
|
|
DstActive bool `json:"dstActive,omitempty"`
|
|
|
|
}
|
2023-10-07 16:32:53 +02:00
|
|
|
|
2023-10-23 23:45:23 +02:00
|
|
|
func main() {
|
|
|
|
fmt.Println("********************************************")
|
|
|
|
fmt.Println("* *")
|
|
|
|
fmt.Println("* git.0x0001f346.de/andreas/Datetime-API *")
|
|
|
|
fmt.Println("* *")
|
|
|
|
fmt.Println("********************************************")
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Printf("Listening: http://localhost:%d\n", portToListenOn)
|
|
|
|
fmt.Println("")
|
2023-10-10 21:47:08 +02:00
|
|
|
|
2023-10-23 23:45:23 +02:00
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
loc, _ := time.LoadLocation(defaultTZ)
|
|
|
|
if r.URL.String() != "/" {
|
|
|
|
newloc, err := time.LoadLocation(r.URL.String()[1:])
|
|
|
|
if err == nil {
|
|
|
|
loc = newloc
|
2023-10-10 21:47:08 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-07 16:32:53 +02:00
|
|
|
|
2023-10-23 23:45:23 +02:00
|
|
|
log.Printf(
|
|
|
|
"%q %q\n",
|
|
|
|
loc.String(),
|
|
|
|
r.RemoteAddr,
|
|
|
|
)
|
2023-10-07 16:32:53 +02:00
|
|
|
|
2023-10-23 23:45:23 +02:00
|
|
|
w.Header().Add("Content-Language", "en")
|
|
|
|
w.Header().Add("Content-Type", "application/json; charset=utf-8")
|
|
|
|
w.Header().Add("Server", "git.0x0001f346.de/andreas/Datetime-API")
|
|
|
|
fmt.Fprint(w, GetDateTimeResponseAsJSONString(loc))
|
|
|
|
})
|
2023-10-07 16:32:53 +02:00
|
|
|
|
2023-10-23 23:45:23 +02:00
|
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", portToListenOn), nil))
|
|
|
|
}
|
2023-10-10 21:47:08 +02:00
|
|
|
|
2023-10-23 23:45:23 +02:00
|
|
|
func GetDateTimeResponse(loc *time.Location) DateTimeResponse {
|
|
|
|
t := time.Now().In(loc)
|
|
|
|
return DateTimeResponse{
|
|
|
|
Year: t.Year(),
|
|
|
|
Month: int(t.Month()),
|
|
|
|
Day: t.Day(),
|
|
|
|
Hour: t.Hour(),
|
|
|
|
Minute: t.Minute(),
|
|
|
|
Seconds: t.Second(),
|
|
|
|
MilliSeconds: func(i int64, err error) int { return int(i) }(strconv.ParseInt(strconv.Itoa(t.Nanosecond())[:3], 10, 0)),
|
|
|
|
DateTime: t.Format(time.RFC3339),
|
|
|
|
Date: fmt.Sprintf("%02d/%02d/%04d", t.Month(), t.Day(), t.Year()),
|
|
|
|
Time: fmt.Sprintf("%02d:%02d", t.Hour(), t.Minute()),
|
|
|
|
TimeZone: loc.String(),
|
|
|
|
DayOfWeek: t.Weekday().String(),
|
|
|
|
DstActive: t.IsDST(),
|
2023-10-07 16:32:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 23:45:23 +02:00
|
|
|
func GetDateTimeResponseAsJSONString(loc *time.Location) string {
|
|
|
|
r, _ := json.Marshal(GetDateTimeResponse(loc))
|
|
|
|
return string(r)
|
2023-10-07 16:32:53 +02:00
|
|
|
}
|