It is currently April 20th, 2024, 5:44 am

Getting time differences with RegExp / Timestamp

Get help with creating, editing & fixing problems with skins
User avatar
Mordasius
Posts: 1171
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Getting time differences with RegExp / Timestamp

Post by Mordasius »

I'm updating my sun/moon/prayer time skins so that the user can search for his/her location with http://wxdata.weather.com/wxdata/search/search?where=#SearchLocation# and it will return the latitude, longitude and time zone of the #SearchLocation#

The problem is that what is returned by wxdata.weather.com is not always the correct time zone. Check Istanbul and New Delhi as current examples.

So I tiddled around with various options but still found http://wxdata.weather.com/wxdata/search/search?where=#SearchLocation# to be the most user friendly way to get a #WeatherCode# that could be used to grab the latitude and longitude of a location using http://wxdata.weather.com/wxdata/weather/local/#WeatherCode#.

So after a little more thought = glasses of wine, it came to me that I don't actually need the time zone of the #SearchLocation# but rather the difference in time between my OS and the time returned when I get the latitude and longitude of the search location using http://wxdata.weather.com/wxdata/weather/local/#WeatherCode#.

So, if the time returned by <tm>(.*)</tm> from http://wxdata.weather.com/wxdata/weather/local/#WeatherCode# is 4:21 PM and the time on my OS is 12:21 (24 hour clock) how and what do I use to calculate the difference (-4 hr)? Note that anything to the nearest 30 minutes is good enough.

I'm only marginally better with RegExp than I am with Timestamp so any and all suggestions would be welcome. Thanks in advance.
User avatar
balala
Rainmeter Sage
Posts: 16147
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Getting time differences with RegExp / Timestamp

Post by balala »

Could you please post the code you have so far? Because I'm trying to use the posted URLs, but can't get nor latitude and longitude, nor the time zone, just the weather code of the entered location. I'd be curious how do you get those information. Here just mention that I am interested into this not just to can answer you, but also for another skin of mine, I'm working with. But with a somehow working code, I probably would deliver some solution (I hope!).
User avatar
SilverAzide
Rainmeter Sage
Posts: 2604
Joined: March 23rd, 2015, 5:26 pm

Re: Getting time differences with RegExp / Timestamp

Post by SilverAzide »

Mordasius wrote: November 2nd, 2018, 5:38 pmSo after a little more thought = glasses of wine, it came to me that I don't actually need the time zone of the #SearchLocation# but rather the difference in time between my OS and the time returned when I get the latitude and longitude of the search location using http://wxdata.weather.com/wxdata/weather/local/#WeatherCode#.

So, if the time returned by <tm>(.*)</tm> from http://wxdata.weather.com/wxdata/weather/local/#WeatherCode# is 4:21 PM and the time on my OS is 12:21 (24 hour clock) how and what do I use to calculate the difference (-4 hr)? Note that anything to the nearest 30 minutes is good enough.

I'm only marginally better with RegExp than I am with Timestamp so any and all suggestions would be welcome. Thanks in advance.
Well, sir, I've actually taken your sun/moon Lua scripts and modified them very slightly to do exactly as you suggest to create my Weather skin. (See my Gadgets link in my sig if you want the whole code.) I used your code to calculate sun and moon rise/set times.

Basically, you'll need a Time measure to grab your local time without any DST (so the UTC conversion works properly).

Code: Select all

[MeasureLocalTime]
Measure=Time
TimeZone=local
DaylightSavingTime=0
Then, pass the numeric timestamp value (i.e., [MeasureLocalTime:Timestamp]) along with the lat/long and timezone bias from TWC to your Lua function. The next challenge is to convert the Windows timestamp to a Lua one. There is a snippet here that does this, but I will warn you it does not really work for what you need (it is off by a few minutes, which is not suitable for what you need).

nTimeZone = the tz bias from TWC (includes any DST offset from UTC)
nLocalTz = this is YOUR tz bias from UTC
nTimestamp = the Windows timestamp from your Time measure

Code: Select all

local nLocalTz = (getTimeOffset() / 3600)
if nTimeZone == nLocalTz then
  tDate = os.date("!*t", nTimestamp)
else
  tDate = os.date("!*t", nTimestamp - getTimeOffset() + (nTimeZone * 3600))
end

-- convert Windows timestamp (0 = 1/1/1601) to Unix/Lua timestamp (0 = 1/1/1970)
tDate.year = tDate.year - (1970 - 1601)

....

function getTimeOffset()
  return (os.time() - os.time(os.date('!*t')) + (os.date('*t')['isdst'] and 3600 or 0))
end
My conversion of your original Lua code is mostly to revise the code to allow calling the code on demand with inline Lua instead of using the Update function. The rest of the changes are to allow calculation of the sun/moon times for locations with TZ biases different from the users.

I found your Lua and skins super helpful and educational, so I hope I am able to return the favor!
Gadgets Wiki GitHub More Gadgets...
User avatar
Mordasius
Posts: 1171
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: Getting time differences with RegExp / Timestamp

Post by Mordasius »

Apologies for the delay in replying.
SilverAzide wrote: November 2nd, 2018, 10:09 pm Basically, you'll need a Time measure to grab your local time without any DST (so the UTC conversion works properly).
Yup.. that's what I was doing but I was scuppered by Weather.com which provides accurate Latitude and Longitude but sometimes gives dubious Time Zone data. Take a look at http://wxdata.weather.com/wxdata/weather/local/TUXX0014?cc=*&unit=C&dayf=4 which suggests the TimeZone for Istanbul is +2 GMT. It should be +3 GMT if you check with Google or https://www.timeanddate.com/time/zone/turkey/istanbul. I use www.timeanddate.com as my reference for sun and moon times and it gives results very close to the sunmoon.lua scripts I use.

So the problem I have is that weather.com is not always providing correct time zone information and I can't find a better source of latitute, longitude and timezone that can be easily searched/scraped without an API.
balala wrote: November 2nd, 2018, 6:32 pm I'm trying to use the posted URLs, but can't get nor latitude and longitude, nor the time zone, just the weather code of the entered location. I'd be curious how do you get those information.
Try this http://wxdata.weather.com/wxdata/weather/local/TUXX0014?cc=*&unit=C&dayf=4 And note that, as above, while the Latitude and longitude are correct, the <zone>(.*)</zone> and <tm>(.*)</tm> are both wrong (as compared with www.timeanddate.com).

I'm still playing around with the skins but while both the time of reporting and reported time zone are wrong it is impossible to use the data in a skin calculating sun and moon times.
User avatar
balala
Rainmeter Sage
Posts: 16147
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Getting time differences with RegExp / Timestamp

Post by balala »

Mordasius wrote: November 3rd, 2018, 6:07 pm Try this http://wxdata.weather.com/wxdata/weather/local/TUXX0014?cc=*&unit=C&dayf=4 And note that, as above, while the Latitude and longitude are correct, the <zone>(.*)</zone> and <tm>(.*)</tm> are both wrong (as compared with www.timeanddate.com).

I'm still playing around with the skins but while both the time of reporting and reported time zone are wrong it is impossible to use the data in a skin calculating sun and moon times.
Well, I thought there is a way to get the coordinates and the time zone from the search results (http://wxdata.weather.com/wxdata/search/search?where=#SearchLocation#). I know how to get them from the set up location page (http://wxdata.weather.com/wxdata/weather/local/TUXX0014?cc=*&unit=C&dayf=4). This is clear.
However I'm not sure I get that difference. For me everything looks to be all right. Am I wrong somewhere?
User avatar
Mordasius
Posts: 1171
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: Getting time differences with RegExp / Timestamp

Post by Mordasius »

balala wrote: November 3rd, 2018, 8:19 pm However I'm not sure I get that difference. For me everything looks to be all right. Am I wrong somewhere?
The problem is that the wrong TimeZone is returned for places like New Delhi (http://wxdata.weather.com/wxdata/weather/local/INXX0096?cc=*&unit=C&dayf=4 ). wxdata gives the TimeZone as +5 when the actual TimeZone for New Delhi is +5.5.

The following skin corrects the TimeZone for most places but still doesn't work for places like Kathmandu (WeatherCode=NPXX0002) where wxdata gives +5, my skin gives +6 and the actual TimeZone is +5.75. This may seem like a small difference but it throws the times for moonrise, moonset, sunrise and sunset off by quite a few minutes and sometimes hours.

Code: Select all

[Rainmeter]
AccurateText=1
MiddleMouseUPAction=!Refresh

[Variables]
; WeatherCode=USHI0026
; WeatherCode=USCA0638
; WeatherCode=CAXX0420
; WeatherCode=USFL0481
; WeatherCode=UKXX0085
; WeatherCode=TUXX0014
WeatherCode=INXX0096  
; WeatherCode=NPXX0002  
; WeatherCode=ASXX0001
 ; WeatherCode=NZXX0003
 
;----------------------------------------
[sText]
FontColor=eeeeee
SolidColor=111111
X=15
AntiAlias=1

[MtPanelBack]
Meter=Shape
Shape=Rectangle 0,0, 180, 140 , 18 | StrokeWidth 0 | Fill Color 111111
AntiAlias=1
UpdateDivider=-1

; ----- WXData -----#WeatherCode# obtained from a search using 
; http://wxdata.weather.com/wxdata/search/search?where=#SearchLocation#

[mWeatherData]
Measure=WebParser
Url=http://wxdata.weather.com/wxdata/weather/local/#WeatherCode#?cc=*&unit=C&dayf=4
UpdateRate=120
RegExp=(?siU)^(.*)$
FinishAction=[!EnableMeasureGroup LocTime][!UpdateMeasureGroup LocTime] [!UpDateMeter MtWeatherDataSet] [!ShowMeter MtWeatherDataSet]

[mWeatherDataLocation]
Measure=WebParser
URL=[mWeatherData]
RegExp=(?siU)<loc id="(.*)".*<dnam>(.*),.*</dnam>.*<tm>(.*)</tm>.*<lat>(.*)</lat>.*<lon>(.*)</lon>.*<sunr>(.*)</sunr>.*<suns>(.*)</suns>.*<zone>(.*)</zone>.*</loc>.*<lsup>(.*)</lsup>
Group=LocTime
Disabled=1

[mLocationName]
Measure=WebParser
URL=[mWeatherDataLocation]
StringIndex=2
Group=LocTime
Disabled=1

[mTimeAtLocation]
Measure=WebParser
URL=[mWeatherDataLocation]
StringIndex=3
Group=LocTime
Disabled=1

[mLocHours]
Measure=Time
TimeStamp=[mTimeAtLocation]
TimeStampFormat=%I:%M %p
Format=%H hrs
Group=LocTime
Disabled=1
DynamicVariables=1

[mLocMinutes]
Measure=Time
TimeStamp=[mTimeAtLocation]
TimeStampFormat=%I:%M %p
Format=%M mins
Group=LocTime
Disabled=1
DynamicVariables=1

[mWeatherTimeZone]
Measure=WebParser
URL=[mWeatherDataLocation]
StringIndex=8
Group=LocTime
Disabled=1

[mWeatherDate]
Measure=WebParser
URL=[mWeatherDataLocation]
RegExp=(?siU)(.*)\s
StringIndex=9

[mLocDate]
Measure=Time
TimeStamp=[&mWeatherDate]
TimeStampFormat= %#m/%d/%y
Group=LocTime
Disabled=1
DynamicVariables=1

; ----- additional measures -----

[mGMTtime]
Measure=Time
TimeZone=0

[mLocalTime]
Measure=Time

[mLocalTimeZone]
Measure=Calc
Formula=( mLocalTime -mGMTTime )/3600

[mLocalHours]
Measure=Time
Format=%#H

[mLocalMinutes]
Measure=Time
Format=%#M

[mLocalTimeNow]
Measure=Calc
Formula=(( ( mLocalHours *3600)+ ( mLocalMinutes *60) )/3600)

[mLocationTimeNow]
;-- add 5 minutes to account for delayed reporting
Measure=Calc
Formula=(( ( mLocHours *3600)+ ( (mLocMinutes+5) *60) )/3600)
DynamicVariables=1

[mSameDay]
Measure=Calc
Formula= ((( mLocalTime - mLocDate )/3600)-24) > 0? 48 : 0
DynamicVariables=1

[mLocationTimeZone]
Measure=Calc
Formula= mLocalTimeNow > mLocationTimeNow ? Round(( (mLocalTimeZone*2)-(Trunc(mLocalTimeNow*2)-Trunc(mLocationTimeNow*2)-  (mSameDay)))/2,1) : Round(( (mLocalTimeZone*2)+(Trunc(mLocationTimeNow*2)-Trunc(mLocalTimeNow*2)-  (mSameDay)))/2,1)
Group=LocTime
Disabled=1

;-----------------------------------------------------------------

[MtTitle]
Meter=String
MeterStyle=sText
Y=30
Text=Fetching data.....

[MtWeatherDataSet]
Meter=String
MeterStyle=sText
Y=-15R
Text=Location: [mLocationName]#CRLF#Time: [mTimeAtLocation]#CRLF#Weather Time Zone:   [mWeatherTimeZone]#CRLF#--------------------#CRLF#Calc Time Zone:   [mLocationTimeZone]
DynamicVariables=1
Hidden=1  
It's not elegant and I'm sure it could be done much better using Timestamp rather than a RegExp on measures like [mWeatherDate] but I couldn't get it to accept a TimeStampFormat.

I'm still playing around to get the TimeZone to the closest 15 minutes but that is tricky when the time or reporting from wxdata.weather.com can be 5-15 minutes earlier than the actual time at the location. I'll probably have to go off to another site to get the time at the location but I was trying to avoid that.
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting time differences with RegExp / Timestamp

Post by jsmorley »

I find https://www.timeanddate.com/ to be a pretty nice one-stop-shop for world times.

https://www.timeanddate.com/worldclock/nepal/kathmandu
User avatar
Mordasius
Posts: 1171
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: Getting time differences with RegExp / Timestamp

Post by Mordasius »

jsmorley wrote: November 4th, 2018, 11:58 am I find https://www.timeanddate.com/ to be a pretty nice one-stop-shop for world times.
Yup. www.timeanddate.com is what I use to check all my sun and moon skins. But is there any easy way to let a skin user just type in his/her location and then select the location from a list of likely places like we can do with http://wxdata.weather.com/wxdata/search/search?where=SearchLocation ?
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Getting time differences with RegExp / Timestamp

Post by jsmorley »

Mordasius wrote: November 4th, 2018, 12:07 pm Yup. www.timeanddate.com is what I use to check all my sun and moon skins. But is there any easy way to let a skin user just type in his/her location and then select the location from a list of likely places like we can do with http://wxdata.weather.com/wxdata/search/search?where=SearchLocation ?
https://www.timeanddate.com/worldclock/results.html?query=kathmandu maybe...
User avatar
Mordasius
Posts: 1171
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: Getting time differences with RegExp / Timestamp

Post by Mordasius »

Looks promising but a fair amount of scraping would be needed. Something to play with though...