knowledge/technology/linux/UNIX Timestamp.md

36 lines
1.9 KiB
Markdown

---
obj: concept
wiki: https://en.wikipedia.org/wiki/Unix_time
rev: 2024-02-08
---
# UNIX Timestamp
A Unix timestamp is a way to represent a point in time, defined as the number of seconds that have elapsed since the Unix epoch. The Unix epoch is defined as 00:00:00 UTC on January 1, 1970.
## Usage
Unix timestamps are commonly used in computing systems to record and calculate time-based events. They are particularly useful for tasks such as:
- [Logging](../dev/Log.md) events: Recording the time when an event occurs.
- Date arithmetic: Calculating time differences between two Unix timestamps.
- Scheduling tasks: Determining when a task should be executed based on a Unix timestamp.
## Conversion
Unix timestamps can be converted to human-readable date and time formats using various programming languages and tools. For example, in Unix-based systems, the `date` command can be used to convert a Unix timestamp to a formatted date:
```shell
date -d @<timestamp>
```
Replace `<timestamp>` with the Unix timestamp you want to convert.
## Examples
- The Unix timestamp `0` represents the Unix epoch, which is January 1, 1970, at 00:00:00 UTC.
- The current Unix timestamp can be obtained using various programming languages and tools. For example, in [Python](../dev/programming/languages/Python.md):
```python
import time
current_timestamp = int(time.time())
print(current_timestamp)
```
## Considerations
- **Leap seconds**: Unix timestamps do not account for leap seconds, which are occasionally added to Coordinated Universal Time (UTC) to keep it in sync with the Earth's rotation. As a result, there may be slight discrepancies between Unix timestamps and true time.
- **Range limitations**: The range of Unix timestamps is limited by the number of bits used to represent them. For a 32-bit signed integer, the range is approximately from 1901-12-13T20:45:52 UTC to 2038-01-19T03:14:07 UTC. However, many systems now use 64-bit integers, which extend the range significantly.