0

I am trying to investigate how to find these numbers, but honestly the answers I have been finding around online have been confusing me. I'm trying to figure out how to get the time of the start of today, and of the most recent Monday.

I read about time.time(), but that gives me the time right now.

Luke James Emery
  • 489
  • 4
  • 20
  • What exactly do you mean by "the start of today"? Is it simply midnight local time, or something more complex? – Mark Ransom Aug 24 '15 at 19:46
  • do you mean utime (time in seconds from 1970) ? – Zohar81 Aug 24 '15 at 19:47
  • please, limit your questions to a single issue per question if possible e.g., you could split this question into several independent steps: (1) get start of today (2) this week's monday (3) convert a local time to Unix time i.e., you could have asked 3 questions instead (with the additional context and links between the questions if necessary). – jfs Aug 24 '15 at 23:00
  • @J.F.Sebastian that is simply not a reasonable split up of questions. This is a single comprehensive question for a programming case I need. If you wanted to suggest that I elaborate on what I was asking, that would be something I would comply with. There is no need to obfuscate it what I need as a user for the sake of pages showing up in google. That's not the point of SO. – Luke James Emery Aug 25 '15 at 14:03
  • @LukeJamesEmery: 90%+ visitors are from google. It is the point of SO. You are expecting help from others while actively refusing to help in turn. It is fine by me. I've answered your question, haven't I. My previous comment just shows how you could give back by asking questions that might be useful to others. It is upto to you whether you do it or not. – jfs Aug 25 '15 at 14:19

3 Answers3

0

I prefer to work with datetime, then you can easily convert that to a timestamp: How to convert a Python datetime object to seconds.

To get today's date, with the time set to zero (midnight) in local time:

today = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)

If you do this you will probably want to convert the time zone to UTC before converting to a timestamp. If you'd rather take midnight in UTC:

today_utc = datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)

Now to adjust to Monday, you need to know which day of the week this represents. Handily there's a weekday method that uses 0 for Monday, so you can just subtract:

monday = today - datetime.timedelta(days=today.weekday())
Community
  • 1
  • 1
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • it is not easy to convert a naive datetime object that represents local time to Unix timestamp: : 1st answer is not suitable, 2nd answer is not portable. Read [my comments](http://stackoverflow.com/q/7852855/4279) and [the answer there](http://stackoverflow.com/a/32192894/4279). – jfs Aug 24 '15 at 22:43
  • @J.F.Sebastian I know I did a little hand-waving over the part about converting local time to UTC, but what makes it unsuitable? I do wish the OP would come back and clarify the question. – Mark Ransom Aug 24 '15 at 23:01
  • "unsuitable" is about the 1st answer from the link you've provided that uses formula that works only with utc time without even mentioning that fact. – jfs Aug 24 '15 at 23:04
0

Something like this should get you the unix timestamp for midnight today in your local timezone:

today = datetime.date.today()
time.mktime((today.year, today.month, today.day, 0, 0, 0, 0, 0, 0))

And this should get you midnight on Monday, since Monday is considered the 0th day of the week:

monday = today + datetime.timedelta(days=-today.weekday())
time.mktime((monday.year, monday.month, monday.day, 0, 0, 0, 0, 0, 0))
  • `mktime()` may fail for past dates if it doesn't has access to a historical timezone database on a given platform. A portable solution could [use a `pytz`-based solution](http://stackoverflow.com/a/32192987/4279) – jfs Aug 24 '15 at 22:54
0

Get UNIX timestamp of the start of today in Python

See How do I get the UTC time of “midnight” for a given timezone? to get an aware datetime object that represents midnight. To get Unix timestamp from that object, call its .timestamp() method.

Get UNIX timestamp of this week's monday

The code almost the same for the monday's timestamp:

from datetime import datetime, time, timedelta
import tzlocal # $ pip install tzlocal

local_timezone = tzlocal.get_localzone()
today = datetime.now(local_timezone).date()
monday = today - timedelta(today.weekday())
dt = local_timezone.localize(datetime.combine(today, time.min), is_dst=None)
unix_time = dt.timestamp()

If you need .timestamp() implementation for <3.3 Python version, see Converting datetime.date to UTC timestamp in Python.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670