MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Unix Epoch Timestamp Guide for DevOps: Conversions, Commands, and Pitfalls

Originally published on 2012-02-22; fully revised and updated.

Unix timestamps show up everywhere in DevOps: logs, metrics, CI/CD events, audit records, and API payloads. This guide explains what epoch time is, how to convert it correctly across common tools, and what to watch for when timezone or unit mistakes can break debugging and automation.

Epoch and Unix timestamp illustration

What Unix epoch time means

A Unix timestamp (also called Unix time or POSIX time) is the number of seconds elapsed since 1970-01-01T00:00:00Z (UTC), not counting leap seconds. The moment 1970-01-01T00:00:00Z is epoch 0.

  • Epoch time is timezone-independent as a number.
  • Display formatting is timezone-dependent.
  • Many systems still store or exchange timestamps as integer seconds.

Time-unit reference for quick estimates

Human readable timeSeconds
1 hour3600
1 day86400
1 week604800
1 month (30.44 days, average)2629743
1 year (365.24 days, average)31556926

How epoch conversion works

In practice, conversion errors usually come from timezone assumptions or unit confusion (seconds vs milliseconds). A consistent flow helps avoid both.

flowchart TD
    A[Input value] --> B{Type of input}
    B -->|Human date| C[Choose timezone]
    C --> D[Convert to epoch seconds]
    B -->|Epoch number| E[Check unit]
    E -->|Seconds| F[Format as date time]
    E -->|Milliseconds| G[Divide by 1000]
    G --> F
    D --> H[Store compare or transmit]
    F --> H

Get the current Unix timestamp

These are common one-liners used in operations scripts and troubleshooting sessions.

EnvironmentCommandUnit returnedNotes
Unix or Linux shelldate +%sSecondsCommon in automation scripts.
Pythonint(time.time())Secondstime.time() is fractional seconds.
JavaScript or Node.jsMath.floor(Date.now()/1000)SecondsDate.now() is milliseconds.
PostgreSQLSELECT EXTRACT(EPOCH FROM now());SecondsUseful for SQL-side comparisons.
MySQLSELECT UNIX_TIMESTAMP(NOW());SecondsBehavior depends on DB timezone context.
PowerShell[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()SecondsExplicit UTC source.
# Shell
date +%s

# UTC shell output (same epoch value, explicit display intent)
date -u +%sCode language: PHP (php)
import time
print(int(time.time()))Code language: JavaScript (javascript)
console.log(Math.floor(Date.now() / 1000));Code language: JavaScript (javascript)

Convert human-readable date and time to epoch

Always decide timezone first. If the input string does not include an offset, tools may assume local timezone or session timezone.

EnvironmentExampleTimezone behavior
GNU datedate -d “2024-01-01 00:00:00 UTC” +%sUTC explicitly provided in input.
macOS BSD datedate -j -u -f “%Y-%m-%d %H:%M:%S” “2024-01-01 00:00:00” +%s-u forces UTC interpretation.
Pythonint(datetime(2024,1,1,0,0,0,tzinfo=timezone.utc).timestamp())Explicit UTC via tzinfo.
JavaScriptMath.floor(new Date(“2024-01-01T00:00:00Z”).getTime()/1000)Z means UTC.
PostgreSQLSELECT EXTRACT(EPOCH FROM TIMESTAMPTZ ‘2024-01-01 00:00:00+00’);Offset included in value.
MySQLSELECT UNIX_TIMESTAMP(‘2024-01-01 00:00:00’);Typically interpreted using session timezone.
# GNU date example
date -d "2024-01-01 00:00:00 UTC" +%s

# macOS BSD date example
date -j -u -f "%Y-%m-%d %H:%M:%S" "2024-01-01 00:00:00" +%sCode language: PHP (php)
from datetime import datetime, timezone
print(int(datetime(2024, 1, 1, 0, 0, 0, tzinfo=timezone.utc).timestamp()))Code language: JavaScript (javascript)
-- PostgreSQL
SELECT EXTRACT(EPOCH FROM TIMESTAMPTZ '2024-01-01 00:00:00+00');

-- MySQL
SELECT UNIX_TIMESTAMP('2024-01-01 00:00:00');Code language: JavaScript (javascript)

Convert epoch to human-readable date and time

When converting back, choose whether you want UTC output or local timezone output for operators and dashboards.

EnvironmentExampleOutput style
GNU datedate -u -d @1704067200 “+%Y-%m-%d %H:%M:%S UTC”UTC text output.
macOS BSD datedate -u -r 1704067200 “+%Y-%m-%d %H:%M:%S UTC”UTC text output.
Pythondatetime.fromtimestamp(1704067200, tz=timezone.utc)Timezone-aware datetime object.
JavaScriptnew Date(1704067200 * 1000).toISOString()ISO 8601 UTC string.
PostgreSQLSELECT to_timestamp(1704067200);Timestamp value based on epoch input.
MySQLSELECT FROM_UNIXTIME(1704067200);Rendered in DB timezone context.
# GNU date
date -u -d @1704067200 "+%Y-%m-%d %H:%M:%S UTC"

# macOS BSD date
date -u -r 1704067200 "+%Y-%m-%d %H:%M:%S UTC"Code language: PHP (php)
from datetime import datetime, timezone
print(datetime.fromtimestamp(1704067200, tz=timezone.utc).isoformat())Code language: JavaScript (javascript)
console.log(new Date(1704067200 * 1000).toISOString());Code language: JavaScript (javascript)

Seconds vs milliseconds: quick comparison

Timestamp unitTypical length todayCommon sourcesConvert to seconds
Seconds10 digitsdate +%s, many SQL functionsUse as-is
Milliseconds13 digitsJavaScript Date.now(), many APIsDivide by 1000

If an epoch value looks too large, confirm unit before parsing. A millisecond value treated as seconds usually produces dates far in the future.

Y2038 and integer-size considerations

Systems that store epoch seconds in a signed 32-bit integer can overflow in January 2038 (the Y2038 issue). Modern platforms commonly use wider integer types, but old schemas, legacy binaries, and embedded systems should still be reviewed.

Reference and original links

Original article: How to Understand Epoch and Unix Timestamp

Common pitfalls and best practices

  • Store canonical timestamps in UTC-based epoch seconds or milliseconds, then format for users at the edge.
  • Document timestamp unit in every API and schema field name where possible.
  • When parsing date strings, include timezone offsets explicitly instead of relying on defaults.
  • In SQL, verify session and server timezone settings before comparing epoch and datetime fields.
  • For cross-platform shell scripts, account for GNU date and BSD date option differences.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Common UNIX Commands and Utilities: Practical Reference for DevOps and Sysadmin Work

A structured UNIX command reference organized by real operational tasks: discovery, files, processes, networking, text processing, and job control. Includes C-shell notes, comparison tables, and practical command examples.

Read More

UNIX sed Quick Reference: Print and Delete Lines with Address and Pattern Ranges

Use this sed quick reference to print or delete exact lines, ranges, and regex matches without opening an editor. It also highlights GNU vs POSIX differences and safer in-place editing patterns.

Read More

Unix grep Quick Reference: Patterns, Options, and Real Command Examples

grep is the core Unix command for searching text by pattern across files or standard input. This revised quick reference covers syntax, high-value options, and practical examples you can use immediately.

Read More

Shell Scripting Interview Questions, Explained: Bash Fundamentals, UNIX Processes, and Practical Commands

A modernized, interview-focused guide to shell scripting and core UNIX concepts for DevOps engineers. Learn command execution flow, redirection, permissions, process control, IPC, and safe scripting patterns with practical examples.

Read More

Unix Basics for DevOps Engineers: Commands, Permissions, Pipes, and Workflow

This revised guide turns a legacy Unix slide embed into a practical reference for beginners and DevOps engineers. Learn core Unix concepts, essential commands, and command execution flow with hands-on examples.

Read More

How to Install CollabNet SVN Command Line Client on Solaris

CollabNet provides a Subversion (SVN) command-line client for version control operations. If you’re using Solaris, follow these steps to install the CollabNet SVN client. 1. Prerequisites Before…

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Puja kushwaha
Puja kushwaha
27 days ago

Epoch timestamps are useful for comparing dates and tracking events across different systems. One common mistake is mixing seconds and milliseconds, which can produce dates far in the future or past. It is also important to convert timestamps using the correct time zone when displaying them to users. When debugging logs, keeping the original timestamp alongside the formatted date can make checks easier. These small habits help prevent time-related errors in applications and reports.

1
0
Would love your thoughts, please comment.x
()
x