A package for converting between dates and epoch days
Project description
The eday package provides functions for converting between dates and epoch days, a concept where dates are represented as the number of days since a reference epoch. This is useful for date arithmetic and conversion.
Installation
Install eday using pip:
pip install eday
Simple Usage
Basic examples:
import eday
eday.from_date(<datetime.datetime>) # -> float
eday.to_date(<float>) # -> datetime.datetime
eday.now() # -> eday <float>
About
The eday package features the Eday class, which represents “epoch days” (Unix seconds in days). It inherits from float, providing conversions to/from datetime, and supports arithmetic operations for quick date calculations.
Main Functions:
from_date: Converts a date object or ISO format string to the number of days since the epoch.
to_date: Converts a number of days since the epoch to a datetime object in UTC.
now: Returns the current UTC time as a number of days since the epoch.
However, you can call the imported eday directly (see below) to use it with minimal typing to do time and calendar computations.
Advanced Usage
The package allows advanced manipulations using eday objects:
import eday
# Create from Epoch days
eday(12345.67890)
# Create from ISO dates
eday('2003-10-20 09:17:36.96-07:00')
# Date arithmetic
eday('2024-10-04') - eday.now()
# Time arithmetic
eday('25:50') + eday('-0:05') # (25:50 translates into 25 hours 50 minutes)
# Unrestricted float numbers of hours, minutes, seconds
eday('100.5:100.15:100.125') # (100.5 hours, 100.15 minutes, 100.125 seconds)
# Out-of-range dates
eday(-2440587.5) # -2440587.5 <-4713-11-24 12:00:0.000000 UTC>
Using Epoch Days without this package (Python 2 & Python 3)
If you don’t need these extra features, and just need to convert dates to/from edays, you could simply use:
import time, datetime
def d2e(date): # datetime.datetime -> float
return time.mktime(date.utctimetuple()) / 86400.
def e2d(eday): # datetime.datetime -> float
return datetime.datetime.utcfromtimestamp(eday * 86400.)
def eday():
return d2e(datetime.datetime.utcnow())
Using Epoch Days from Terminal
Linux users can use these zsh functions:
function d2e { # isodate -> eday
local n=$((($(date -u --date="$1" +%s%9N)/864)*1000))
local day=${n:0:-14}; local hour=${n:(-14)}
echo $day.${hour} | sed 's/\.\?0*$//'
}
function e2d { # eday -> isodate
local second=$(printf "%f" $(($1*86400)))
echo $(date -u +"%Y-%m-%dT%H:%M:%S.%N%:z" -d "@$second")
}
Save these functions in eday.sh and source it or add to /usr/local/bin/eday.
#!/bin/bash
function eday { # eday now
local n=$((($(date +%s%9N)/864)*1000))
local day=${n:0:-14}; local hour=${n:(-14)}
echo $day.${hour:0:${1-11}} # $1: precision
}
eday
Compatibility
The package is compatible with Python 2 (up to version 1.0.1) and Python 3 (from version 1.0.2). Python 2 users will need the dateutil module for parsing ISO format strings.
License
This package is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Feel free to open an issue or submit a pull request on GitHub.
GitHub Repository
You can find the source code and contribute to the development of this package on GitHub: https://github.com/mindey/eday
More Information
For more information on epoch days and their applications, you can visit the following link:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file eday-1.0.8-py3-none-any.whl
.
File metadata
- Download URL: eday-1.0.8-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27c76846ccc3bc0df6385980c0ec420e9192f1ecf102597c9232a9af34f83e91 |
|
MD5 | 15c38682a8b03bc74fe1878f7abaf186 |
|
BLAKE2b-256 | c66fd6ff5ef4f55c195a2f86a83de84bd50b8289b9d01bda447ba531b15e2c67 |