Skip to content

Instantly share code, notes, and snippets.

@bruno-uy
Created May 18, 2021 15:29
Show Gist options
  • Save bruno-uy/fd447fdf0e749117f7e266d8c894c2aa to your computer and use it in GitHub Desktop.
Save bruno-uy/fd447fdf0e749117f7e266d8c894c2aa to your computer and use it in GitHub Desktop.
Round up UTC now timestamp
import pandas as pd
utc_now_pd = pd.Timestamp.utcnow()
# The function round(freq="D") is magic behind the round up
# Use replace(tzinfo=None) to remove timezone information
utc_now_ceil = utc_now_pd.round(freq="D").to_pydatetime().replace(tzinfo=None)
# Convert to ISO format
utc_now_str = utc_now_ceil.strftime("%Y-%m-%dT%H:%M:%S")
@bruno-uy
Copy link
Author

bruno-uy commented Jun 5, 2021

If you want to subtract some days just do:

from datetime import timedelta

utc_now_ceil - timedelta(days=7)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment