How to set the correct local time zone in git bash? - windows

I am using git-bash on a Windows system.
The Windows clock shows local time, but inside git-bash everything is in GMT time:
$ date
Mon Mar 31 16:08:57 GMT 2014
Also setting TZ will not change things:
$ TZ="Europe/Berlin" date
Mon Mar 31 16:09:01 GMT 2014
Similarly all times it git log are GMT only.
Is there a way to set the correct timezone in git-bash?

On Windows the TZ variable seems to work differently.
To get the German timezone you have to write:
TZ=GST-1GDT date
If you set it to some "invalid" value like "Europe/Berlin" it will default to GMT.
The same seems to happen on my system when TZ is not set at all.
With the above setting I get Thu Apr 17 16:23:23 GDT 2014 which is not exactly the same as Thu Apr 17 16:23:23 CEST 2014, but at least the time looks right.

Same problem here in my script. Windows was showing 15:47 and the "date" command in gitbash was answering 13:47.
export TZ="CEST-2"
This fixed the problem for me. I wanted Paris time.

Related

Printing end-of-line field in awk causing formatting issues

I have a log file that contains output from
/bin/df -h| /usr/bin/grep p_log|/usr/bin/awk -v date="$(date)" '{print date,$4,$5}'
which is later sent out using mailx. It arrives in my PC's Outlook as desired with a line per entry and displays with a line end in cat -A:
Wed Mar 16 10:29:01 EDT 2022 291G 95%$
Wed Mar 16 11:29:01 EDT 2022 290G 95%$
Wed Mar 16 12:29:02 EDT 2022 290G 95%$
Adding an additional field to the awk - $6 happens to be the last field in the df output - still displays the same with cat:
Wed Mar 16 11:29:01 EDT 2022 290G 95%$
Wed Mar 16 12:29:02 EDT 2022 290G 95%$
Wed Mar 16 13:29:01 EDT 2022 290G 95% /.p_log$
Wed Mar 16 14:29:02 EDT 2022 290G 95% /.p_log$
But lines are now concatenated when read in Windows/Outlook:
Wed Mar 16 10:29:01 EDT 2022 291G 95%
Wed Mar 16 11:29:01 EDT 2022 290G 95%
Wed Mar 16 12:29:02 EDT 2022 290G 95%
Wed Mar 16 13:29:01 EDT 2022 290G 95% /.p_log Wed Mar 16 14:29:02 EDT 2022 290G 95% /.p_log
I found another post at explains that cat -e (which I have tried, and is encompassed by -A) "displays Unix line endings (\n or LF) as $ and Windows line endings (\r\n or CRLF) as ^M$". Why then are two lines that display the same control characters in cat being displayed differently in Windows/how best to get the line feed back when printing $6 without messing up the formatting of the log? I presume there are more hidden control characters that cat -A does not display, i.e. that 'all' does not actually mean all.
Further testing: There are header and footer lines - all ending in the same "$"- that do not get concatenated. I tried attaching the content from the end of one of the concatenated lines to a header line and that would indicate that it's the "/" that's causing the problem, but only for mailx.
Looks like I've been barking up the wrong tree; not sure if I should delete this question and open a new one for mailx?
Adding two spaces to the start of each line - https://stackoverflow.com/a/22098987/8823709 - resolved the issue. I don't know why, but it did.

bash variable doubles in value - why?

I have a simple shell script set up to capture images every X seconds. For some reason the value of X seems to double each time through the loop.
#!/bin/bash
# basic setup for time-lapse
SECONDS=1
while true
do
DATE=$(date +"%Y-%m-%d_%H%M%S")
filename=${DATE}_img.jpg
# fswebcam -r 1280x720 --timestamp "%a %Y-%b-%d %H:%M (%Z)" /home/pi/JPGS/$filename
date
echo "pausing for ${SECONDS} seconds"
sleep $SECONDS
date
echo "====="
done
This is the output I get. The value of SECONDS is not manipulated inside the loop, so I'm confused with what is happening here. Also, the original interval was 30 seconds, I changed it to 1 seconds for testing purposes, and the date calls are for testing/debugging too.
Sun Mar 3 17:51:57 CST 2019
pausing for 1 seconds
Sun Mar 3 17:51:58 CST 2019
=====
Sun Mar 3 17:51:58 CST 2019
pausing for 2 seconds
Sun Mar 3 17:52:00 CST 2019
=====
Sun Mar 3 17:52:00 CST 2019
pausing for 4 seconds
Sun Mar 3 17:52:04 CST 2019
=====
Sun Mar 3 17:52:04 CST 2019
pausing for 8 seconds
Sun Mar 3 17:52:12 CST 2019
=====
Sun Mar 3 17:52:12 CST 2019
pausing for 16 seconds
Sun Mar 3 17:52:28 CST 2019
=====
Sun Mar 3 17:52:28 CST 2019
pausing for 32 seconds
Sun Mar 3 17:53:00 CST 2019
=====
Sun Mar 3 17:53:00 CST 2019
pausing for 64 seconds
Sun Mar 3 17:54:04 CST 2019
=====
Sun Mar 3 17:54:04 CST 2019
pausing for 128 seconds
What am I missing here?
This is under a Raspberry Pi
Pick a different name for $SECONDS.
$SECONDS is a built-in shell variable. It expands to the number of seconds since the shell was started.
From the Bash manual:
'SECONDS'
This variable expands to the number of seconds since the shell was
started. Assignment to this variable resets the count to the value
assigned, and the expanded value becomes the value assigned plus the
number of seconds since the assignment.
$SECONDS is actually a special Bash Variable for timing the number of seconds a script has been running. Because it's a timer, it increments automatically every second without the script doing anything. Just change the variable name to something else and you should be fine.

Ruby unix date incorrect

I have the following Unix timestamp: 1478698378000
And I'm trying to show this as a datetime in Ruby, e.g.
<%= Time.at(#timestamp).to_datetime %>
Which should be returning a date of: Wed, 09 Nov 2016 13:32:58 GMT but the above code actually returns a date of: 48828-02-01T13:26:40+00:00 Ignore formatting!
As you can see it thinks that timestamp is 2nd Feb 48828 13:26:40.
Why is the datetime coming out completely incorrect and the year so far into the future like that? Checking the timestamp on http://www.epochconverter.com/ reveals the timestamp to be correct, so it's Ruby that's returning it incorrectly.
Time.at expects seconds as an argument and your timestamp is an amount of milliseconds. See documentation on Time.at
Why won’t you check the unix timestamp correctness against “Fashion Week Magazine” or “Cosmopolitan” Site?
Unix timestamp is an amount of seconds lasted since 1970-01-01 UTC:
date --date='#1478698378000'
mar feb 1 14:26:40 CET 48828
BTW, dropping last three zeroes gives you back what you’ve expected:
date --date='#1478698378'
mié nov 9 14:32:58 CET 2016

The right ISO 8601 format

I am refactoring some code for a Ruby library. This code includes a Date parser.
One of the tests was to parse this string "2008-02-20T8:05:00-010:00" which is supposed to be ISO 8601.
The previous code would actually output: "Wed Feb 20 18:05:00 UTC 2008".
My new code outputs that: "Wed Feb 20 16:05:00 UTC 2008".
My question is: which one is the right one?
Time.parse in Ruby gives the second one. But again, I want to be 100% sure that the previous code AND test were buggy.
Which one is right? (By maybe parsing the string with a library in another language? - I only know Ruby.)
The correct UTC time is 1805. The time group indicates 0805 in zone -10, so to get UTC add the 10 to the given time. Thus 1805. Since 1805 is less than 2400 it's the same day.
If your code is giving 1605, then you almost certainly have the timezone set incorrectly to zone -8, which happens to be Pacific Standard Time.
Aha, looks like your input format is messed up. Observe:
irb(main):003:0> Time.parse("2008-02-20T8:05:00-010:00")
=> Wed Feb 20 08:05:00 -0700 2008
I happen to be in zone -7 so it's suiting that to my locale. But
irb(main):004:0> t=Time.parse("2008-02-20T8:05:00-010:00")
=> Wed Feb 20 08:05:00 -0700 2008
irb(main):005:0> t
=> Wed Feb 20 08:05:00 -0700 2008
irb(main):006:0> t.getutc
=> Wed Feb 20 15:05:00 UTC 2008
I'm getting an unexpected result. Now observe:
irb(main):007:0> t=Time.parse("2008-02-20T8:05:00-10:00")
=> Wed Feb 20 11:05:00 -0700 2008
irb(main):008:0> t.getutc
=> Wed Feb 20 18:05:00 UTC 2008
There's the expected result. See the difference? First example vs second:
irb(main):004:0> t=Time.parse("2008-02-20T8:05:00-010:00")
irb(main):007:0> t=Time.parse("2008-02-20T8:05:00-10:00")
I took the spurious extra 0 out (which I certainly didn't notice either) and whoosh, it works.
I know this is pretty old, but I just ran across it.
I'll bet that something somewhere is interpreting 010 as an octal number with the value 8. Perhaps it's a bug in the implementation of Time.parse()?

How can I format date -u so that the results include timezone offset in a Mac OSX terminal session?

In a terminal session I can use date -u to get
Mon Mar 16 03:34:39 2009 UTC
However, I'd like to include the offset. I'm modifying a TextMate tab trigger so that I can insert the full date including the local offset, in standard UTC format. I believe that would be in the following form:
Mon Mar 16 03:34:39 2009 UTC -0500
So, as you can see, I don't know how to get the timezone offset and combine that with formatted date results.
Try this:
echo `date -u` `date +%z`

Resources