Im building a bots for some clients but want the bots to activate at the same time based on Eastern time only, since Im in New York, and thats the only time I do know by heart. I don't know what timezone any of my future clients will be in so I'd rather permanently set it contingent upon eastern time. I want them all to be timed to start at 11:00AM EST - US/Eastern
from pytz import timezone
import time, datetime
def Schedule():
tz = timezone('US/Eastern')
today = datetime.datetime.now(tz)
#today = datetime.datetime.now()
Activate = (datetime.datetime(today.year, today.month, today.day, 0, 50, 0) - today).seconds
#Uses Military Time 23, 25, 0 = 11:25:00PM
print('Waiting for ' + str(datetime.timedelta(seconds=Activate)))
time.sleep(Activate)
#Rest of the code will activate at the correct time that you set it
Schedule() #Uncomment this
Related
I'm trying to use Unix Timestamps within a Discord Message, can't find any information about it.
Thank you!
Go to https://www.epochconverter.com/ and get the epoch timestamp for the date and time you need using the Human date to Timestamp button. (eg. 1585890376)
Inside your discord message, format your message as such:
<t:########:#>
Replace ######## with your epoch timestamp,
Replace # with various letters for different options: (f, F, D, t, T or R)
Example:
For the current date, 2 years ago:
<t:1585890376:f>
April 3, 2020 1:06 AM
<t:1585890376:F>
Friday, April 3, 2020 1:06 AM
<t:1585890376:D>
April 3, 2020
<t:1585890376:t>
1:06 AM
<t:1585890376:T>
1:06:16 AM
<t:1585890376:R>
2 years ago
See format_dt in the docs.
Example:
from nextcord.utils import format_dt
from datetime import datetime
dt = datetime.now()
# Short Time (9:18 AM)
short_time = format_dt(dt, "t")
# Long Time (9:18:58 AM)
long_time = format_dt(dt, "T")
# Short Date (04/03/2022)
short_date = format_dt(dt, "d")
# Long Date (April 3, 2022)
long_date = format_dt(dt, "D")
# Default (April 3, 2022 9:18 AM)
default_date_time = format_dt(dt, "f")
# Long Date Time (Sunday, April 3, 2022 9:18 AM)
long_date_time = format_dt(dt, "F")
# Relative Time (in 5 minutes / 1 day ago)
relative = format_dt(dt, "R")
...
# To send this in a command:
await ctx.send(f"{default_date_time} ({relative})")
I am trying to come up with the epoch of the start of the day (12AM midnight UTC) in Python. I found this snippet:
today = datetime.date.today()
time.mktime((today.year, today.month, today.day, 0, 0, 0, 0, 0, 0))
but the resulting epoch leads to today's date at 5AM. The reason I need this is because I have a script executing a SQL command that needs to filter using epoch time (for some reason, casting or extracting an epoch through native SQL results in my query finishing in 5 minutes, as compared to 12 seconds). I'm guessing that the code snippet I put above uses my local machine time.
You need to figure how what date it is currently in UTC, and then replace the time bits:
now = datetime.now(timezone.utc)
midnight = now.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
In order to get the epoch, use midnight.timestamp(). This will ensure that the timezone information is applied when calculating the timestamp.
I'm using python3 on Ubuntu 20.04.
I have a trove of files with naive datetime strings in them, dating back more than 20 years. I know that all of these datetimes are in the Pacific Timezone. I would like to convert them all to UTC datetimes.
However, whether they are relative to PDT or PST is a bigger question. Since when PDT/PST changes has changed over the last 20 years, it's not just a matter of doing a simple date/month threshold to figure out whether to apply the pdt or pst timezone. Is there an elegant way to make this determination and apply it?
Note upfront, for Python 3.9+: use zoneinfo from the standard library, no need anymore for a third party library. Example.
Here's what you can to do set the timezone and convert to UTC. dateutil will take DST changes from the IANA database.
from datetime import datetime
import dateutil
datestrings = ['1991-04-06T00:00:00', # PST
'1991-04-07T04:00:00', # PDT
'1999-10-30T00:00:00', # PDT
'1999-10-31T02:01:00', # PST
'2012-03-11T00:00:00', # PST
'2012-03-11T02:00:00'] # PDT
# to naive datetime objects
dateobj = [datetime.fromisoformat(s) for s in datestrings]
# set timezone:
tz_pacific = dateutil.tz.gettz('US/Pacific')
dtaware = [d.replace(tzinfo=tz_pacific) for d in dateobj]
# with pytz use localize() instead of replace
# check if has DST:
# for d in dtaware: print(d.dst())
# 0:00:00
# 1:00:00
# 1:00:00
# 0:00:00
# 0:00:00
# 1:00:00
# convert to UTC:
dtutc = [d.astimezone(dateutil.tz.UTC) for d in dtaware]
# check output
# for d in dtutc: print(d.isoformat())
# 1991-04-06T08:00:00+00:00
# 1991-04-07T11:00:00+00:00
# 1999-10-30T07:00:00+00:00
# 1999-10-31T10:01:00+00:00
# 2012-03-11T08:00:00+00:00
# 2012-03-11T09:00:00+00:00
Now if you'd like to be absolutely sure that DST (PDT vs. PST) is set correctly, you'd have to setup test cases and verify against IANA I guess...
import pyttsx3
import time
time = time.strftime("%M minutes past %I")
engine = pyttsx3.init()
engine.setProperty('rate',200)
engine.say("Hi Tom");
engine.say("The time is" + time);
engine.runAndWait();
When running this it will say "Hi Tom, the time is 07 minutes past 10" for example and will say a 0 in front of the minutes if its between 0-9 but 10-59 its says it normally. Is there a way to remove the 0 from being said?
Could this be useful
t = time.localtime()
...
engine.say('The time is %d minutes past %s' % (t.tm_min,t.tm_hour))
there's obviously a time module that works in combination with this problem, but I have not found it yet.
I'm simply trying to use Pyephem on a Raspberry Pi to find out what time sunrise and sunset is for my latitude longitude coordinates.
the code is quite simply this:
import ephem
import datetime
import time
now = datetime.datetime.now()
gmNow = time.mktime(time.localtime())
Vancouver = ephem.Observer()
Vancouver.lat = 49.2878
Vancouver.horizon = 0
Vancouver.lon = -123.0502
Vancouver.elevation = 80
Vancouver.date = now
# Vancouver.date = time.localtime()
sun = ephem.Sun()
print("sunrise is at",ephem.localtime(Vancouver.next_rising(sun)))
print("sunset is going to be at ",ephem.localtime(Vancouver.next_setting(sun)))
print("now is ",now)
print("gmNow is",gmNow)
what exports, when that runs is wrong by 8 hours though. so it appears that the
ephem.localtime() is not actually running.
pi#raspberrypi ~ $ sudo python3 vivarium_sun.py
sunrise is at 2014-09-19 12:55:56.000004
sunset is going to be at 2014-09-19 00:52:30.000004
now is 2014-09-19 06:22:24.014859
gmNow is 1411132944.0
It's driving me nuts, and it's obviously one of those simple things once it's figured out, so I'm going to the hive mind here.
EDIT** Just typing 'date' into the command line of the Raspberry Pi returns the following:
pi#raspberrypi ~ $ date
Fri Sep 19 18:41:42 PDT 2014
which is accurate.
You should pass datetime.utcnow() to the observer instead of your local time.
ephem expects latitude and longitude in radians if passed as floats, use strings instead:
from datetime import datetime, timezone
import ephem
now = datetime.now(timezone.utc)
Vancouver = ephem.Observer()
Vancouver.lat = '49.2878'
Vancouver.horizon = 0
Vancouver.lon = '-123.0502'
Vancouver.elevation = 80
Vancouver.date = now
sun = ephem.Sun(Vancouver)
print("sunrise is at", ephem.localtime(Vancouver.next_rising(sun)))
print("sunset is going to be at ",
ephem.localtime(Vancouver.next_setting(sun)))
print("now is ",now.astimezone())
Output
sunrise is at 2014-09-20 06:55:38.000005
sunset is going to be at 2014-09-19 19:16:38.000004
now is 2014-09-19 19:15:04.171486-07:00