Public Service

Fast Time Service VN

Dịch vụ cung cấp thời gian (timestamp epoch milliseconds) cho thiết bị IoT, đặc biệt tối ưu cho ESP32 và các thiết bị nhúng. Miễn phí, không cần đăng ký, không cần API key.

Miễn phí - Không cần Auth Tối ưu cho ESP32⚡ Rate Limit: 60 req/s🕐 Múi giờ: Asia/Ho_Chi_Minh

API Endpoints

GEThttps://time.iotlabs.vn/

Timestamp dạng Text (mặc định)

Trả về timestamp epoch milliseconds dạng text thuần. Tối ưu cho ESP32 và thiết bị nhúng.

Response (text/plain; charset=utf-8)
1705401234567
cURL
curl https://time.iotlabs.vn/
GEThttps://time.iotlabs.vn/json

Timestamp dạng JSON

Trả về timestamp và timezone dưới dạng JSON object.

Response (application/json)
{
  "timestamp": "1705401234567",
  "timeZone": "Asia/Ho_Chi_Minh"
}
cURL
curl https://time.iotlabs.vn/json
GEThttps://time.iotlabs.vn/?format=json

Timestamp dạng JSON (Query Param)

Cách thay thế để lấy JSON qua query parameter.

Response (application/json)
{
  "timestamp": "1705401234567",
  "timeZone": "Asia/Ho_Chi_Minh"
}
cURL
curl https://time.iotlabs.vn/?format=json

Ví dụ Tích Hợp

ESP32 (Arduino / C++)

Dùng HTTPClient để lấy timestamp text thuần, tiết kiệm bộ nhớ cho ESP32.

#include <WiFi.h>
#include <HTTPClient.h>

void getTime() {
  HTTPClient http;
  http.begin("https://time.iotlabs.vn/");

  int httpCode = http.GET();
  if (httpCode == 200) {
    String timestamp = http.getString();
    unsigned long long timeMs = timestamp.toInt();
    Serial.println("Timestamp: " + String(timeMs));
  }

  http.end();
}

ESP32 (MicroPython)

Dùng urequests để lấy timestamp, chuyển sang seconds.

import urequests

def get_time():
    response = urequests.get('https://time.iotlabs.vn/')
    timestamp_ms = int(response.text)
    timestamp_s = timestamp_ms // 1000
    print('Timestamp (s):', timestamp_s)
    response.close()
    return timestamp_s

current_time = get_time()

Python

import requests

# Lấy timestamp dạng text
response = requests.get('https://time.iotlabs.vn/')
timestamp_ms = int(response.text)
timestamp_s = timestamp_ms / 1000
print(f"Timestamp (ms): {timestamp_ms}")
print(f"Timestamp (s): {timestamp_s}")

# Lấy timestamp dạng JSON
response = requests.get('https://time.iotlabs.vn/json')
data = response.json()
print(f"Timestamp: {data['timestamp']}")
print(f"Timezone: {data['timeZone']}")

Node.js

// Lấy timestamp dạng text
const res = await fetch('https://time.iotlabs.vn/');
const timestampMs = parseInt(await res.text());
console.log('Timestamp (ms):', timestampMs);

// Lấy timestamp dạng JSON
const jsonRes = await fetch('https://time.iotlabs.vn/json');
const data = await jsonRes.json();
console.log('Timestamp:', data.timestamp);
console.log('Timezone:', data.timeZone);

Xử Lý Lỗi

HTTP StatusMô TảGiải Pháp
200 OKRequest thành công
503Vượt quá rate limit (60 req/s)Chờ vài giây và thử lại, giảm tần suất request

Múi Giờ Hỗ Trợ

Service hỗ trợ tất cả múi giờ chuẩn IANA. Dưới đây là một số múi giờ phổ biến:

Asia/Ho_Chi_Minh (mặc định)UTCAmerica/New_YorkEurope/LondonAsia/TokyoAsia/Singapore

Best Practices

  • 1.Thiết bị nhúng (ESP32, Arduino): Dùng endpoint mặc định https://time.iotlabs.vn/ để nhận text thuần, tiết kiệm bộ nhớ.
  • 2.Ứng dụng web/mobile: Dùng /json để nhận đầy đủ thông tin timestamp + timezone.
  • 3.Timestamp milliseconds → seconds: Chia cho 1000.
  • 4.Xử lý timezone: Convert timestamp về UTC hoặc timezone mong muốn ở phía client.
  • 5.Caching: Cache timestamp vài giây để giảm request và tránh rate limit.
  • 6.Rate Limiting: Không gửi quá 60 requests/giây. Nên giãn cách hoặc dùng caching.