So, the setup is that I have the time, in seconds, since the epoch and I want to turn this into a date I can actually understand.
How do I do this in Haskell? If it is possible, how would I extract days/hours/minutes out of this new encoding?
Data.Time.Clock.POSIX has posixSecondsToUTCTime (you can convert another numeric type to the input expected POSIXTime with realToFrac).
Data.Time.Calendar has a lot of the things you need to extract day, month, etc from the Day that forms a part of UTCTime, the rest of the package has other useful utilities.
Example:
Prelude> import Data.Time.Format.ISO8601
Prelude Data.Time.Format.ISO8601> import Data.Time.Clock.POSIX
Prelude Data.Time.Format.ISO8601 Data.Time.Clock.POSIX> iso8601Show $ posixSecondsToUTCTime $ 100
"1970-01-01T00:01:40Z"
Use time library which is installed by default:
import Data.Time.Clock.POSIX
import Data.Time.Format
import System.Locale
main = print $ formatTime defaultTimeLocale "%c" $ posixSecondsToUTCTime 10
The library has wide range of date manipulation functions (e.g. date subtraction, getting components such as day-month etc). If you want to extract components just for converting them to string you can use formatTime.
Related
I am fetching data from an API that comes in iso formated strings, for ex - example 2022-07-27T00:00:00.0000000+01:00
end_date=item['RunDate']
start_time=portion['StartTimeWTT']
I am trying to format the date to look like: yyyy-mm-dd and time hh:mm:ss
I have tried different solutions but none of them works
end_date1=item['RunDate']
end_date=datetime.date().strftime(end_date1,'%Y-%M-%d')
or datetime.fromisoformat
I would be grateful for any suggestions.
For me, the easiest way to handle date strings is by using the dateutil library. For example in your cited case:
from dateutil import parser
from dateutil.utils import default_tzinfo
from dateutil.tz import tzoffset
tx = '2022-07-27T00:00:00.0000000+01:00'
tz_data = tzoffset("EST", 0)
print(default_tzinfo(parser.parse(tx) , tz_data))
yields
2022-07-27 00:00:00+01:00
I have datetime in string needing to be converted in datetime format. Below is my code but it returns error. what I am missing here.
from datetime import datetime
LocalStartTime='2020-09-17T10:55:06.4000000+1000'
datetime_object = datetime.strptime(LocalStartTime, '%Y-%m-%dT%H:%M:%S.%f%z')
Required output shd be date converted in current timezone to format like: '2020-09-17 20:55:06' whatever will be the actual value.
returns below error:
ValueError: time data '2020-09-17T10:55:06.4000000+1000' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'
from datetime documentation:
When used with the strptime() method, the %f directive accepts from one to six digits and zero pads on the right. %f is an extension to the set of format characters in the C standard (but implemented separately in datetime objects, and therefore always available).
you have one too many zeros in the float part after the seconds part.
the limitation is 6 digits.
from datetime import datetime
LocalStartTime='2020-09-17T10:55:06.400000+1000'
datetime_object = datetime.strptime(LocalStartTime, '%Y-%m-%dT%H:%M:%S.%f%z')
should work
Edit:
after the OP edited and asked about converting to a different timestamp:
seems like what you're looking for is timestamp() and fromtimestamp()
you can get the timestamp which is a posix timestamp represented as float, and convert it back to datetime object with fromtimestamp() if you want to remove the float part after the seconds you can convert the time stamp to int.
datetime.fromtimestamp(int(datetime_object.timestamp()))
Does anyone know where I can find a good function that converts a string like "15 mins" or "1hr 20mins" into an integer denoting the number of seconds?
I think you can find your solution here , and you can write your own function which does this, by writing a formal way to parse your time, you can transform your string into a datetime and the rest is straight forward.
from datetime import datetime, timedelta
# we specify the input and the format...
t = datetime.strptime("05:20:25","%H:%M:%S")
# ...and use datetime's hour, min and sec properties to build a timedelta
delta = timedelta(hours=t.hour, minutes=t.minute, seconds=t.second)
print(delta)
assert(5*60*60+20*60+25 == delta.total_seconds())
is there a readily-available command in Python's datetime to understand a discrete time range given as HH:MM-HH:MM or HH:MM:ss-HH:MM:ss (e.g. 07:30-12:45)? Such a range would be entered like that in a single cell from a CSV file that the script would access.
Or, might specifying just the start time and then a timedelta value be a better idea?
You can just use split() to separate the two time values, then parse each as a datetime.datetime type and then calculate the timedelta.
Example:
from datetime import datetime
time_string = "07:30-12:45"
separate_times = time_string.split("-")
parsed_times = [datetime.strptime(t, "%H:%M") for t in separate_times]
difference = parsed_times[1] - parsed_times[0]
Calling difference.total_seconds() will return the total seconds between the two times and if you aren't interested in the direction of the difference between the times, you can use abs(difference.total_seconds()).
In http://codeworld.info/, you can only draw/print/display data in types of Text and Number (and you need Show to convert Number to Text). So what can I do to let codeworld draw something that is neither a Text or Number?
Chris recently added the ability to import the Haskell base library into CodeWorld, so you can do this:
import Prelude hiding (show)
import HaskellPrelude (show)
showT(x) = fromString(show(x))
main = pictureOf(text(showT([1,2,3,4,5])))