over my head in bash
Ladies & Gentlemen,
Thanks for the help in a previous post. I am trying to write a script. I have come to a part that is over my head. This script is dependent on the hdate command which returns dates and times for specific events on the Hebrew calender. The problem is that hdate does not account for daylight savings time. One of the things the script does is to schedule events via 'at' to occur at #certain times of the day, hence the need to have the script adjust for daylight #savings time. I have the following code started but I am at a loss as to how to continue. What I need is some help to understand how to get where I need to go, not just #write the code for me. $ date returns day-of-week, month, day, hour:min:sec, time zone indicator, year the format of the tzdump is the same. It occurred to me this morning that I can narrow the test because I can eliminate #all months except March and November (for my area) as one side or the other. With that in mind.... Code:
function daylightSavings # Zone data is in /usr/share/zoneinfo |
SO I am at a bit of loss trying to understand what is going on. So I have a questions instead.
You mention in your explanation about how hdate does not handle daylight saving and yet your example only uses date which based on setting the correct time zone I was of the understanding that daylight saving was factored in? Hence if your machine is aligned to /usr/share/zoneinfo/US/Michigan does it not perform the necessary daylight savings when required? |
date +%z will give you your offset from UTC. I don't live in a country with daylight savings time though, so I don't know if this gets adjusted for the shift.
The date info page says this, however: Quote:
And like grail, I'd like to know more about how hdate displays its output, and what you really want your script to do. Not in code, but in terms of human function. |
Quote:
|
Code:
elif [ date +%m=={$daylightOn[*]} ]; # kmails color formating indicators tell me there is a problem with this line but I can't find it. |
grail,
my example only references date and not hdate because it is the current date that I am using to adjust the time given by hdate. Quote:
David the H and MTK358, I do like the idea of adjusting the time output by hdate by using the time zone. I think that will be much simpler in the script over all because hdate uses a time zone variable that is simpler to change in my script than the actual time. So I will rewrite with that in mind. Thanks. Now it is just the last part with all the logic. More on the general purpose later when I have some time. |
Quote:
|
Thanks for the help.
A brief example of what I am trying to do with the script: Test the date to see if it is a Friday or Saturday. If Friday then find out via hdate what time sundown is (18 min before actually) and schedule a shofar (rams horn) blast to be played at that time to anounce the beginnig of the Sabbath. Then the same basic thing for the end of the Sabbath and the Jewish Holidays. So if the shofar blast is scheduled without accounting for daylight savings time it will be at the wrong time. Hdate uses the time zone to get the correct offset from Jeruselem for your local location. So if I set my time zone varible early in the script no mater when I access hdate ater that the time will be correct and I won't have to continually test the date every time I want to schedule an event. Code:
function daylightSavings # Zone data is in /usr/share/zoneinfo Code:
daylightOff=( 12 01 02 03 ); Thanks |
You shouldn't need to calculate this manually.
I do not have hdate so I don't know what the output format is, but if it's compatible with date, you can use date to automatically correct for DST using the -d option. For example, as I write this, the current time is (I'm in the UK): Quote:
Tue Aug 16 13:13:32 GMT 2011 Feeding this back into date will print the specified date but corrected for local time: Quote:
Of course, if hdate's output format differs, you may need to modify the string somewhat. For example, if you make it print just the time, you could add this to the rest of the date string, something like: Quote:
|
Also for the array tests ... you were close but of course one month is not equal to several, but several can contain one:
Code:
daylightOff=( 12 01 02 03 ) |
According to the man pages the -c option will output Shabat start/stop times but will assume it will only display on Fri and Sa so I can not see the output format. It also states the start times are based on 20 min vs 18 before sunset.
As already stated you can easily get the UTC offset value adjusted for daylight saving time for your time zone from date and then plug it into hdate to get the sunset times based on your location (latitude, longitude). Example sunrise/sunset times for Kansas City on 16 Aug 2011 date +%:::z -05 hdate -s -l 39 -L 94 -z -05 6:29 - 20:11 Please post your script when you get it working. |
Thanks
So starting again. lej Code:
:~$ hdate -c -L-85 -l44 -z-5 19 08 2011 grail thanks for the pointer on having the parameters in the right order. I think you my be missing that there are 2 different tests one for those months that are before daylight savings time starts and one for those months after it ends. In one case I need make no change and the other increment the $tz variable by one. It does occur to me that at some point I will need to do some error checking to be sure that the variable returns to the normal state when daylight savings time ends. What is the difference between == and =~ ? At one point I was looking at a page that listed some of those kinds of things but can't find it now. Someone on irc mentioned that using date +%Z to get the time zone may be a better way to go. But I want to work on the logic as a learning experience for now. working on my code. thanks |
Quote:
== - test if the left and right of the expression are equal =~ - regular expression test, left matches expression on the right |
Another way to think about it:
== matches globbing patterns, and the entire string must be compared as a whole. =~ matches regex patterns, and so can match substrings. It can even extract them for later use with the BASH_REMATCH array variable. The first is simpler to use and faster to process, while the latter is more powerful and flexible. I personally mentally translate them as "X equals Y (==)", and "X contains Y (=~)". This page shows the various options in [ and [[: http://mywiki.wooledge.org/BashFAQ/031 It doesn't seem to me that +%Z would be appropriate at all. DST is a very region-specific thing, and just because a person is in X time zone does not mean he's in an area that uses DST. Not to mention that even when they do use it, different countries apply it at different times. Since DST is already factored into +%z, it should be a much more accurate measurement of the local time. Not that I understand exactly what you're doing, but one possibility, if you want to avoid the "radically different outputs", is to just set up a single hdate function that always outputs everything it's capable of producing in a consistent fashion. Then you can parse that output for the actual data you need in your script's subsequent operations. |
Thanks guys
First let me apologize for not listening and understanding. I have changed the tz variable in my script form being statically set to -5 to being computed at runtime via `date +%:::z` and hence all the times given by hdate will be corrected for daylight saving time. Sorry I am so thick headed David the H and MTK358. I guess I just needed the example shown by michaelk to understand what you were trying to tell me. But the process did get me to where I needed to go. So as requested here is the full code of the script as it now stands. Bare in mind that it is a work in progress and needs much work to be really usable. Code:
#!/bin/bash |
All times are GMT -5. The time now is 12:47 AM. |