Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

How do I reformat this date variable?

If you have a question or need help, this is the place to be.
Post Reply
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

How do I reformat this date variable?

Post by Mastiff »

I have a bunch of temp sensors tied into the webserver, and there's only one annoyance. My dates comes out like this:

Code: Select all

2013-12-07 11:30:48
I would very much like to unclutter it and reformat it to this:

Code: Select all

11:30, 07.12.
Can somebody please tell me how? Thanks in advance!
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: How do I reformat this date variable?

Post by Pako »

If it is certain, that timestring is always the same length (for example as 2013-12-07 00:00:01), then you can do it like this ("t" is old timestring):

Code: Select all

newT = "%s:%s, %s.%s." % (t[11:13], t[14:16], t[8:10], t[5:7])
Pako
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Re: How do I reformat this date variable?

Post by Mastiff »

Thank you very much for the answer! Unfortunately my thick skull is messing it up, it seems... I get these errors:

Code: Select all

13:42:49            Traceback (most recent call last):
13:42:49              Python script "44", line 15, in <module>
13:42:49                Ute_time_formatert = "%s:%s, %s.%s." % (Ute_time[11:13], Ute_time[14:16], Ute_time[8:10], Ute_time[5:7])
13:42:49            NameError: name 'Ute_time' is not defined
I thought I had definded the Ute_time in line 11 of the the script, here in bold. This is the little script that's run:

Code: Select all

import time
eg.event.payload_copy = eg.event.payload
my_data = eg.event.payload_copy.split(' ')

td = my_data[2] #temperature
tf = my_data[6] #humidity

timeStamp = str(
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
)
[b]eg.globals.Ute_time = timeStamp[/b]
eg.globals.Ute_temp = td
eg.globals.Stua_fukt = tf

Ute_time_formated = "%s:%s, %s.%s." % (Ute_time[11:13], Ute_time[14:16], Ute_time[8:10], Ute_time[5:7])
I'm guessing that I've misunderstood something about how the variables are logged. I tried to look for a place in EG that shows all variables, but I couldn't find it.
jonib
Plugin Developer
Posts: 1328
Joined: Thu Mar 26, 2009 9:33 pm
Location: Sweden

Re: How do I reformat this date variable?

Post by jonib »

Mastiff wrote:I thought I had definded the Ute_time in line 11 of the the script, here in bold. This is the little script that's run:
You need to use the full name of the variable "eg.globals.Ute_time" or it won't work.

jonib
XBMC2 plugin to control XBMC. If you want to flatter me Image
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Re: How do I reformat this date variable?

Post by Mastiff »

Thank you! Got it working. :D
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: How do I reformat this date variable?

Post by krambriw »

Would this not be a bit simpler?

Code: Select all

import time
eg.event.payload_copy = eg.event.payload
my_data = eg.event.payload_copy.split(' ')

td = my_data[2] #temperature
tf = my_data[6] #humidity

timeStamp = str(
time.strftime("%H:%M, %d.%m.", time.localtime())
)
eg.globals.Ute_temp = td
eg.globals.Stua_fukt = tf


Post Reply