Page 1 of 2

MicroPython Time Display

Posted: Wed Apr 07, 2021 12:17 pm
by ChasL001
Hello everyone,

Newbie to Micropython so apologies if this post is in the wrong place, please feel free to tell me off! ;)

I'm just beginning to build a weather monitor using a Pico (yes another weather station) but it's cheaper than putting a fully fledged Pi outside.

I have 'borrowed' code to read from a BME280 module and can get it to display Temperature, Pressure and Humidity just using a Pintout at the moment, with plans to send the data to a remote display.

I'd like to print the Date and Time and can get this to happen using the built in RTC and

Code: Select all

print(utime.localtime()) 
What I can't seem to fathom in micropython is how to format this to give a more human readable display, it gives me:

(2021, 4, 7, 12, 49, 6, 2, 97)

Which is Year, Month, Day, Hour, Min, Sec & Millisec.

I just want this to display UK style date and time like 07/04/2021 12:49

Have tried various Python formatting to no avail.

Can anyone give me a link to how to better format it in micropython?

Many thanks.

Re: MicroPython Time Display

Posted: Wed Apr 07, 2021 3:29 pm
by ChasL001
I've actually done what I wanted now so maybe this might help others.

In my original printout in Thonny, I got the following result:

Code: Select all

(2021, 4, 7, 12, 40, 44, 2, 97)
Temperature is 24.51 degrees
Pressure is 1009.448 mbar
Humidity is 37.90723 %
What I wanted was to take the string of numbers from the time and display it as in the OP.

I brought in the time and gave it a variable:

Code: Select all

from utime import localtime
dateTimeObj = localtime()
When I wanted to printout the output I split the dateTimeObj variable into individual variables for each value

Then I created another variable Ddateandtime and used the Python format method for the string

Code: Select all

    Dyear, Dmonth, Dday, Dhour, Dmin, Dsec, Dweekday, Dyearday = (dateTimeObj)
    Ddateandtime = "{}/{}/{} {}:{}"
    print(DdateandTime.format(Dday, Dmonth, Dyear, Dhour, Dmin,))
Result:

Code: Select all

7/4/2021 16:25
Temperature is 25.04 degrees
Pressure is 1009.521 mbar
Humidity is 38.6377 %
Yes I know it's HOT here but the wife has thin blood! :D

Hope this helps.

PS, might create a lookup for the Weekday for prettiness. ;)

Re: MicroPython Time Display

Posted: Wed Apr 07, 2021 4:14 pm
by pythoncoder
You might prefer this format string

Code: Select all

"{:02d}/{:02d}/{} {:02d}:{:02d}"
particularly for the time display as times like 1:2 arguably look better rendered as 01:02.

Re: MicroPython Time Display

Posted: Mon Apr 12, 2021 12:56 pm
by ChasL001
pythoncoder wrote:
Wed Apr 07, 2021 4:14 pm
You might prefer this format string

Code: Select all

"{:02d}/{:02d}/{} {:02d}:{:02d}"
particularly for the time display as times like 1:2 arguably look better rendered as 01:02.
Many thanks pythoncoder, much appreciated. :)

Re: MicroPython Time Display

Posted: Mon Apr 26, 2021 1:12 pm
by ChasL001
Hi me again,

Now writing the data to a file but cannot get the data format to write correctly

Tried modifying:

Code: Select all

print(DdateandTime.format(Dday, Dmonth, Dyear, Dhour, Dmin,))
to this

Code: Select all

file.write(str(Ddateandtime) + "\n")
had to drop the

Code: Select all

.format(Dday, Dmonth, Dyear, Dhour, Dmin,
bit as it just gave FORMATTING ERROR

But I get this

{:02d}/{:02d}/{} {:02d}:{:02d}

Heylp!

Re: MicroPython Time Display

Posted: Mon Apr 26, 2021 4:37 pm
by pythoncoder
This works here:

Code: Select all

DateForFile = "{}/{}/{} {}:{}\n"
with open('foo.txt', 'w') as f:
    f.write(DateForFile.format(Dday, Dmonth, Dyear, Dhour, Dmin,))
Note that you'll need to consider Python file writing modes - look up append 'a' and write 'w' modes.

Re: MicroPython Time Display

Posted: Mon Apr 26, 2021 5:23 pm
by ChasL001
Thanks again Peter, brilliant, I'm learning lots.

I use the file = open method as I have the Pico cycling and appending the file:

Code: Select all

DateForFile = "{}/{}/{} {}:{}\n"
file = open('foo.txt', 'w')
file.write(DateForFile.format(Dday, Dmonth, Dyear, Dhour, Dmin,))
My next puzzle is formatting the filename with a date. I've tried dozens of methods and none seem to work, though they do in Python, so I assume MicroPython is not there yet. Any ideas?

Thanks
Chas

Re: MicroPython Time Display

Posted: Mon Apr 26, 2021 5:30 pm
by pythoncoder
All these things are straightforward and are just a matter of learning Python. I suggest you either buy a book on Python or do an online course on your PC. MicroPython is highly compatible with standard Python, at least until you get involved with advanced features of the language.

A filename is just a string. You can format a string as you wish and then use it in place of "foo.txt" when opening the file.

Re: MicroPython Time Display

Posted: Mon Apr 26, 2021 6:14 pm
by ChasL001
pythoncoder wrote:
Mon Apr 26, 2021 5:30 pm
All these things are straightforward and are just a matter of learning Python. I suggest you either buy a book on Python or do an online course on your PC. MicroPython is highly compatible with standard Python, at least until you get involved with advanced features of the language.

A filename is just a string. You can format a string as you wish and then use it in place of "foo.txt" when opening the file.
Thanks Peter,

I've been doing Python coding since I bought a Raspberry Pi Zero whilst in hospital a couple of years ago, and am getting there slowly.

I've tried formatting different string variables to make the filename include the date but the filename always just ends up as the variable name not the content of it. Anyway, it's not that important now that I can include the date within the file properly formatted, thanks to your help.

Cheers once again
Chas

Re: MicroPython Time Display

Posted: Tue Apr 27, 2021 6:19 pm
by hippy

Code: Select all

import time
year, month, day, hour, mins, secs, weekday, yearday = time.localtime()   

# Print a date - YYYY-MM-DD
print("{}-{:02d}-{:02d}".format(year, month, day))

# Set a variable to a date
today = "{}-{:02d}-{:02d}".format(year, month, day)
print(today)

# Create a dated file - YYYY-MM-DD.txt
filename = today + ".txt"
print(filename)
with open(filename, "w") as f:
    f.write(" Created " + today + "\n")