Re: Problem saving datetime to file and reading it back for a calculation

2020-10-11 Thread Cameron Simpson
On 11Oct2020 20:39, Steve  wrote:
>Still, I enjoyed the kluge I created making it work based on discovery...

Poking around in the datetime module will definitely make you aware of
its power, and its pitfalls. Well worth doing. At the very least you'll
usually want it when printing times out for humans.

But seconds is generally simpler and reliable, particularly as it tosses
timezones straight out the window - it is all just arithmetic.

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Problem saving datetime to file and reading it back for a calculation

2020-10-11 Thread Steve
Thanks for the responses.  Somehow, all of my python messages were shifted
into the deleted folder so I missed all of them until I caught the one from
MRAB.

I will sift through them and probably update my technique to use seconds as
suggested.

Still, I enjoyed the kluge I created making it work based on discovery...



Footnote:
If 666 is evil then
25.8 is the square root of all evil.


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem saving datetime to file and reading it back for a calculation

2020-10-11 Thread MRAB

On 2020-10-11 20:25, Steve wrote:

Thanks for the response.

I must have spent hours looking on-line for a method to treat datetime
variables yet not one site mentioned the "pickle" module you indicatged.  I
did, however solve my problem.  It may be a kluge but it seems to work.

I learned that I cannot use print() to display the value of datetime but
once I saved it to a file, I could see it.  If I used "d3 = d2.isoformat" it
could be sent to a file with a write statement.  Apparently, it gives a
write/read format and places a T between the date and time as a separator.

In trying to read it back into the program and work the calculation, I had
to replace the T with a space and some formatting. It all worked.


[snip]

Given:

 import datetime

To convert a datetime d to an ISO-format string, you can use:
s = d.isoformat()

To convert an ISO-format string s to a datetime, you can use:

d = datetime.datetime.fromisoformat()
--
https://mail.python.org/mailman/listinfo/python-list


RE: Problem saving datetime to file and reading it back for a calculation

2020-10-11 Thread Steve
Thanks for the response.

I must have spent hours looking on-line for a method to treat datetime
variables yet not one site mentioned the "pickle" module you indicatged.  I
did, however solve my problem.  It may be a kluge but it seems to work.

I learned that I cannot use print() to display the value of datetime but
once I saved it to a file, I could see it.  If I used "d3 = d2.isoformat" it
could be sent to a file with a write statement.  Apparently, it gives a
write/read format and places a T between the date and time as a separator. 

In trying to read it back into the program and work the calculation, I had
to replace the T with a space and some formatting. It all worked.
#===

LBD = "LBD"
d2 =  datetime.now()
d2i = d2.isoformat()

with open("TimeDate.txt", 'r') as infile:
 for BottleInfo in infile: # loop to find each line in the file for that
dose
   BottleInfo = BottleInfo.strip()

   if ((BottleInfo[0:3]== "LBD")):
BottleData = BottleInfo[0:43].strip()

BottleDataA = BottleData[4:14].strip()
BottleDataB = BottleData[16:30].strip()
BottleDataC = BottleDataA + " " + BottleDataB
print("BottleDataC = <" + BottleDataC + ">")
# I guess I could have searched for the "T" and replaced it.
print()
d1 = BottleDataC

import datetime
dto = datetime.datetime.strptime(d1, '%Y-%m-%d %H:%M:%S.%f')
dti = dto.isoformat()

HoursDiff = int((d2-dto).total_seconds()/3600)
print("HoursDiff = " + str(HoursDiff))
print()

TimeDateInfo=open("TimeDate.txt", "a") 
TimeDateInfo.write("{0:>5} {1:>25} {2:>5}\n".format (LBD, d2i, HoursDiff))
TimeDateInfo.close()

# ===

Granted, there may be other ways to do this but I actually enjoy the
exploration...
Still, I would like to see other methods.
Steve

-----Original Message-
From: Dieter Maurer  
Sent: Sunday, October 11, 2020 12:48 PM
To: Steve 
Subject: Re: Problem saving datetime to file and reading it back for a
calculation

Steve wrote at 2020-10-10 18:17 -0400:
>I would like to use the line:
>HoursDiff = int((d2-d1).total_seconds()/3600) to determine the 
>difference in hours between two timedate entries.
>
>The variable d2 is from datetime.now()
>and d1 is read from a text file.
>
>I can save d2 to the file only if I convert it to string and, at a later
>date, it gets read back in as d1 as string.   The variable d1 as string
will
>not work in the HoursDiff statement.

Python's "pickle" module provides support for storing (most) objects to
files and read them back.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem saving datetime to file and reading it back for a calculation

2020-10-10 Thread Chris Angelico
On Sun, Oct 11, 2020 at 12:57 PM Cameron Simpson  wrote:
> Personally I strongly dislike using datetimes for computation or as the
> basis for time record keeping, essentially because of the timezone issue
> but also because the human calendar is a complex disaster of illfitting
> units (days in a year? variable; days in a month? variable; that table
> of days per unit? variable depending on your time in history).
>
> Instead, I always try to work in POSIX timestamps, an absolute number of
> seconds since midnight, 1 January 1970 GMT. You can always do arithmetic
> directly between these in seconds, then convert for presentation
> purposes whenever.
>

Absolutely agree, with the annoying exception of recurring events. If
a human says "this happens every Monday at 2pm", then the human
expects it to recur every Monday at 2pm, not every 604800 seconds. The
difference shows up when the conversion between UTC and local time
changes - most commonly when Daylight Saving Time starts or ends...

(And I guess if you care about leap seconds, then Unix time would be
inappropriate there too. But I rather doubt that most of us are
bothered by that.)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem saving datetime to file and reading it back for a calculation

2020-10-10 Thread Cameron Simpson
On 10Oct2020 18:17, Steve  wrote:
>I would like to use the line:
>HoursDiff = int((d2-d1).total_seconds()/3600)
>to determine the difference in hours between two timedate entries.
>
>The variable d2 is from datetime.now()
>and d1 is read from a text file.
>
>I can save d2 to the file only if I convert it to string and, at a later
>date, it gets read back in as d1 as string.

That is the nature of text files.

>The variable d1 as string will
>not work in the HoursDiff statement.

Because it is a string.

Peter has described a way to transcribe a datetime in an arbitrary string 
format and back. Note that his example formats are "naive" - they don't know 
what you timezone is, and you want that if that ever varies (not just 
datetimes in contexts from different places, but also at different times of 
the year if you run different summer and winter times, particularly 
troublesome around the transition from one to the other).

>To me, it looks like a problem in formatting.
>How do I fix this?

Personally I strongly dislike using datetimes for computation or as the 
basis for time record keeping, essentially because of the timezone issue 
but also because the human calendar is a complex disaster of illfitting 
units (days in a year? variable; days in a month? variable; that table 
of days per unit? variable depending on your time in history).

Instead, I always try to work in POSIX timestamps, an absolute number of 
seconds since midnight, 1 January 1970 GMT. You can always do arithmetic 
directly between these in seconds, then convert for presentation 
purposes whenever.

Because you're now working in seconds directly, you go:

HoursDiff = int((t2-t1)/3600)

or:

HoursDiff = (t2-t1) // 3600

When you first collect your datetime, convert it to a POSIX timestamp 
immediately. If you're _starting_ from datetime.now(), do not do that! Just 
start with time.time() and no conversion is needed at all.

Just _present_ in datetime formats if you need to. DO NOT try to work 
with them as your basic type - they are a source of pitfalls.

As far as storing timestamps in a file, they're ints or floats; just 
write them out.

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem saving datetime to file and reading it back for a calculation

2020-10-10 Thread Peter Pearson
On Sat, 10 Oct 2020 18:17:26 -0400, Steve  wrote:
> I would like to use the line:
> HoursDiff = int((d2-d1).total_seconds()/3600)
> to determine the difference in hours between two timedate entries.
>
> The variable d2 is from datetime.now()
> and d1 is read from a text file.
>
> I can save d2 to the file only if I convert it to string and, at a later
> date, it gets read back in as d1 as string.   The variable d1 as string will
> not work in the HoursDiff statement.
>
> To me, it looks like a problem in formatting.
> How do I fix this?

datetime.datetime.strftime and datetime.datetime.strptime ?

>>> t = datetime.datetime.now()
>>> t.strftime("%Y-%m-%d %H:%M:%S")
'2020-10-10 18:02:33'
>>> b = datetime.datetime.strptime("2020-09-09 12:34:56", "%Y-%m-%d %H:%M:%S")
>>> b.strftime("%Y-%m-%d %H:%M:%S")
'2020-09-09 12:34:56'
>>> 

-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list


Problem saving datetime to file and reading it back for a calculation

2020-10-10 Thread Steve
I would like to use the line:
HoursDiff = int((d2-d1).total_seconds()/3600)
to determine the difference in hours between two timedate entries.

The variable d2 is from datetime.now()
and d1 is read from a text file.

I can save d2 to the file only if I convert it to string and, at a later
date, it gets read back in as d1 as string.   The variable d1 as string will
not work in the HoursDiff statement.

To me, it looks like a problem in formatting.
How do I fix this?

Steve


-- 
https://mail.python.org/mailman/listinfo/python-list