Unix Timestamp क्या है (और डेवलपर्स इसे क्यों इस्तेमाल करते हैं)?
Unix timestamp एक single integer है: 1 जनवरी 1970, 00:00:00 UTC से अब तक बीते हुए seconds की संख्या। उस समय को Unix epoch कहा जाता है, और उसके बाद हर सेकंड बिना रुके गिना गया है।
इस वक्त का current timestamp लगभग 1,750,000,000 के आसपास होता है। सिर्फ यही संख्या किसी भी computer, database, या API को बता देती है कि आप समय के किस exact पल की बात कर रहे हैं — बिना किसी ambiguity के।
1970 ही क्यों?
Unix का विकास 1960s के आख़िरी वर्षों में Bell Labs में हुआ था। जब engineers को समय के लिए एक fixed reference point चाहिए था, उन्होंने 1 जनवरी 1970 चुन लिया — यह एक नए दशक की शुरुआत थी, समझने में आसान, और उस समय के “recent past” में थी ताकि stored dates के लिए practical मामलों में negative numbers की ज़रूरत न पड़े।
यह एक pragmatic choice थी, कोई “सिद्धांत” वाली नहीं। लेकिन फिर यही standard बन गई।
Unix Timestamps कौन-सी समस्या हल करते हैं
Human-readable strings के रूप में dates को represent करने से हर layer पर ambiguity आती है:
- Format disagreements:
03/04/25का मतलब 3 अप्रैल है या 4 मार्च? यह 1925 है या 2025? - Time zone confusion: “Friday 3pm” London और Tokyo में अलग moment होता है
- Locale differences: कुछ देश day-month-year लिखते हैं, कुछ month-day-year
Unix timestamp इन सबको bypass कर देता है। 1711929600 एक single, unambiguous moment है — आप कहीं भी हों या आपको कोई भी format पसंद हो। Machines सहमत रहती हैं; humans convert कर लेते हैं।
Seconds, Milliseconds, और Microseconds
Original Unix timestamp seconds में होता है। लेकिन अलग-अलग systems अलग resolutions इस्तेमाल करते हैं:
| Format | Unit | Example |
|---|---|---|
| Unix (POSIX) | Seconds | 1711929600 |
JavaScript Date.now() | Milliseconds | 1711929600000 |
Python time.time() | Seconds (float) | 1711929600.123 |
| Database timestamps | अक्सर microseconds | 1711929600000000 |
यही सबसे common bug sources में से एक है जब आप systems के बीच काम करते हैं। अगर JavaScript timestamp को सीधे Python या Go function में दे दिया जाए, तो वह 1,000× ज्यादा बड़ा होगा। System boundaries cross करते समय हमेशा resolution check करें।
Unix Timestamps पढ़ना और लिखना
JavaScript / TypeScript में
// Current timestamp in seconds
Math.floor(Date.now() / 1000)
// Current timestamp in milliseconds
Date.now()
// Convert timestamp (seconds) to Date object
new Date(timestamp * 1000)
// Convert Date to timestamp (seconds)
Math.floor(new Date('2024-04-01').getTime() / 1000)
Python में
import time, datetime
# Current timestamp (float)
time.time()
# Convert to datetime
datetime.datetime.fromtimestamp(1711929600)
# Convert datetime to timestamp
datetime.datetime(2024, 4, 1).timestamp()
SQL (PostgreSQL) में
-- Current timestamp
EXTRACT(EPOCH FROM NOW())::int
-- Convert to timestamp
TO_TIMESTAMP(1711929600)
Time Zones खुद Timestamp को प्रभावित नहीं करते
यह महत्वपूर्ण है और अक्सर गलत समझा जाता है।
Unix timestamp हमेशा UTC में एक moment को represent करता है। जब आप इसे display करते हैं, तो आप local time zone में convert करते हैं। जब आप इसे store करते हैं, तो आप UTC value store करते हैं। Timestamp खुद time-zone neutral होता है।
इसका मतलब:
- अलग-अलग time zones में बैठे दो users “now” store करेंगे तो same timestamp मिलेगा
- display पर हर user इसे अपने local time में देखेगा
- database में offsets adjust करने की ज़रूरत नहीं — सिर्फ presentation layer पर
Year 2038 Problem
32-bit signed integer के रूप में stored Unix timestamps केवल 2,147,483,647 seconds तक गिन सकते हैं — जो 19 जनवरी 2038, 03:14:07 UTC के बराबर है। इसके बाद 32-bit counter wrap होकर एक बड़ा negative number बन जाएगा।
जो systems अभी भी timestamps को 32-bit integers में store करते हैं, वे उस date पर टूट सकते हैं। अधिकतर modern systems 64-bit integers इस्तेमाल करते हैं, जिससे overflow की date साल 292 billion तक चली जाती है — practical purposes के लिए irrelevant।
अगर आप embedded systems, legacy databases, या पुराने C code के साथ काम कर रहे हैं, तो timestamps कैसे store होते हैं, यह check करना worth है।
Unix Timestamp कब इस्तेमाल करें
Unix timestamp इस्तेमाल करें जब:
- आप database में dates store कर रहे हों और एक simple, sortable integer चाहते हों
- आप systems या APIs के बीच dates pass कर रहे हों
- आप events को chronologically compare या sort कर रहे हों
- आप durations compute कर रहे हों (बस दो timestamps घटा दें)
- आप cache या expiry logic बना रहे हों (
expires_at = now + 3600)
Formatted date string इस्तेमाल करें जब:
- आप user को date दिखा रहे हों
- आप human-readable logs लिख रहे हों
- आप spreadsheets या CSV exports के साथ काम कर रहे हों
Quick Conversions
| What | Seconds |
|---|---|
| 1 minute | 60 |
| 1 hour | 3,600 |
| 1 day | 86,400 |
| 1 week | 604,800 |
| 30 days | 2,592,000 |
| 1 year (approx.) | 31,536,000 |
Unix Timestamp Converter किसी भी timestamp को human-readable date में बदल देता है, या किसी date को Unix timestamp में तुरंत convert करता है।