Created
December 22, 2016 22:30
-
-
Save lionelg3/2865e5388fa3f7b7f116990463280446 to your computer and use it in GitHub Desktop.
Tic tac
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Tic tac</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" | |
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" | |
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" | |
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" | |
crossorigin="anonymous"></script> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<script> | |
var INTERVAL = 100; // millisecondes | |
function loadjson(uri, callback) { | |
var xhr = new XMLHttpRequest(); | |
xhr.overrideMimeType("application/json"); | |
xhr.open('GET', uri, true); | |
xhr.onreadystatechange = function () { | |
if (4 == xhr.readyState && 200 == xhr.status) | |
callback(JSON.parse(xhr.responseText)); | |
}; | |
xhr.send(null); | |
} | |
function update() { | |
loadjson('datetime.json', function (datetime) { | |
document.getElementById('jour').innerText = datetime.jour; | |
document.getElementById('moi').innerText = datetime.moi; | |
document.getElementById('annee').innerText = datetime.annee; | |
document.getElementById('heure').innerText = datetime.heure; | |
document.getElementById('minute').innerText = datetime.minute; | |
document.getElementById('seconde').innerText = datetime.seconde; | |
document.getElementById('nanoseconde').innerText = datetime.nanoseconde; | |
}); | |
} | |
setInterval(update, INTERVAL); | |
</script> | |
<body> | |
<div class="container"> | |
<br/> | |
<h4>Tic Tac</h4> | |
<br/> | |
<div class="jumbotron"> | |
<div class="row"> | |
<div class="col-md-4"> | |
<kbd id="jour"></kbd> <kbd class="col-md-3" id="moi"></kbd> <kbd id="annee"></kbd> | |
</div> | |
<div class="col-md-4 col-md-offset-4"> | |
<kbd id="heure"></kbd> : <kbd id="minute"></kbd> : <kbd id="seconde"></kbd> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="col-md-3 col-md-offset-3"> | |
<kbd id="nanoseconde"></kbd> | |
</div> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains 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
package main | |
import ( | |
"net/http" | |
"log" | |
"html/template" | |
"time" | |
"encoding/json" | |
) | |
type DateTime struct { | |
Year int `json:"annee"` | |
Month string `json:"moi"` | |
Day int `json:"jour"` | |
Hour int `json:"heure"` | |
Minute int `json:"minute"` | |
Second int `json:"seconde"` | |
NanoSecond int `json:"nanoseconde"` | |
} | |
func main() { | |
http.HandleFunc("/", indexHandler) | |
http.HandleFunc("/datetime.json", timeHandler) | |
log.Println("Lancement serveur HTTP, port 3000") | |
err := http.ListenAndServe(":3000", nil) | |
if err != nil { | |
log.Fatal("Erreur dans le lancement du serveur HTTP ", err) | |
} | |
} | |
func indexHandler(w http.ResponseWriter, r *http.Request) { | |
t, _ := template.ParseFiles("home.html") | |
t.Execute(w, nil) | |
} | |
func timeHandler(w http.ResponseWriter, r *http.Request) { | |
year, month, day := time.Now().Date() | |
hour, minute, second := time.Now().Clock() | |
nanoSecond := time.Now().Nanosecond() | |
dateTime := DateTime{ | |
Year: year, | |
Month: month.String(), | |
Day: day, | |
Hour: hour, | |
Minute: minute, | |
Second: second, | |
NanoSecond: nanoSecond, | |
} | |
jsonTime, _ := json.Marshal(dateTime) | |
w.Write(jsonTime) | |
// <iframe frameborder="0" width="480" height="270" src="//www.dailymotion.com/embed/video/xikgxb" allowfullscreen></iframe> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment