Created
September 27, 2024 05:31
-
-
Save boseji/fef7578760b0c3d1a8985e7b4d3c99a7 to your computer and use it in GitHub Desktop.
Boseji's JSON Time Server - Provides both GMT (Internet Time) and Local time in IST (Bharat Standard Time)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ॐ भूर्भुवः स्वः | |
// तत्स॑वि॒तुर्वरे॑ण्यं॒ | |
// भर्गो॑ दे॒वस्य॑ धीमहि। | |
// धियो॒ यो नः॑ प्रचो॒दया॑त्॥ | |
// | |
// | |
// बोसजी के द्वारा रचित जेसोन समय सहायक तन्त्राक्ष्। | |
// =================================== | |
// | |
// एक सरल संचार सहायक और संलग्न तंत्र। | |
// | |
// ~~~~~~~~~~~~~~~~~~~~~~~ | |
// एक रचनात्मक भारतीय उत्पाद। | |
// ~~~~~~~~~~~~~~~~~~~~~~~ | |
// | |
// ___________________________ | |
// | |
// Boseji's JSON Time Server | |
// ___________________________ | |
// | |
// Provides both GMT (Internet Time) and Local time in IST (Bharat Standard Time). | |
// | |
// Sources | |
// -------- | |
// https://gist.github.com/boseji/ | |
// | |
// | |
// License | |
// -------- | |
// | |
// SPDX: Apache-2.0 | |
// | |
// Copyright (C) 2024 Abhijit Bose (aka. Boseji). All rights reserved. | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// | |
// SPDX short identifier: Apache-2.0 | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"html" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
_ "time/tzdata" // Important! for Timezone Information | |
) | |
func main() { | |
port := os.Getenv("PORT") | |
if port == "" { | |
port = "8080" | |
} | |
log.Println("Starting Server on Port: ", port) | |
http.HandleFunc("/", hello) | |
http.HandleFunc("/time", timeHandler) | |
log.Fatalln(http.ListenAndServe(":"+port, nil)) | |
} | |
func hello(w http.ResponseWriter, r *http.Request) { | |
json.NewEncoder(w).Encode( | |
map[string]string{ | |
"Greeting": fmt.Sprintf("Hari Aum from %q", | |
html.EscapeString(r.URL.Path)), | |
}, | |
) | |
} | |
func timeHandler(w http.ResponseWriter, _ *http.Request) { | |
type tm struct { | |
CurrentTime string `json:"current_time"` | |
LocalTime string `json:"local_time"` | |
} | |
location, _ := time.LoadLocation("Asia/Kolkata") | |
ct := []tm{ | |
{ | |
CurrentTime: time.Now().Format(http.TimeFormat), | |
// Compatible to Internet Time | |
LocalTime: time.Now().In(location).Format(time.RFC1123), | |
}, | |
} | |
json.NewEncoder(w).Encode(ct) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment