why timestamps for two timezones with the same UTC offset are different? - linux

Here is an example:
root#linux:~# timedatectl
Local time: Thu 2016-03-31 08:33:23 CEST
Universal time: Thu 2016-03-31 06:33:23 UTC
RTC time: n/a
Time zone: Africa/Ceuta (CEST, +0200)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
Sun 2016-03-27 01:59:59 CET
Sun 2016-03-27 03:00:00 CEST
Next DST change: DST ends (the clock jumps one hour backwards) at
Sun 2016-10-30 02:59:59 CEST
Sun 2016-10-30 02:00:00 CET
root#linux:~# echo $string
1970 01 01 0 0 0
root#linux:~# awk -v str="$string" 'BEGIN {print mktime(str)}'
0
root#linux:~# timedatectl set-timezone Europe/Berlin
root#linux:~# timedatectl
Local time: Thu 2016-03-31 08:59:01 CEST
Universal time: Thu 2016-03-31 06:59:01 UTC
RTC time: n/a
Time zone: Europe/Berlin (CEST, +0200)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
Sun 2016-03-27 01:59:59 CET
Sun 2016-03-27 03:00:00 CEST
Next DST change: DST ends (the clock jumps one hour backwards) at
Sun 2016-10-30 02:59:59 CEST
Sun 2016-10-30 02:00:00 CET
root#linux:~# echo $string
1970 01 01 0 0 0
root#linux:~# awk -v str="$string" 'BEGIN {print mktime(str)}'
-3600
2 question: And why mktime output (when input 1970-01-01T00:00:00 ) for Europe/Berlin timezone is -1 hour?

timedatectl (apparently) shows the current timezone rules in effect for the currently set timezone. However timezone rules change over time within the same timezone.
As of today, both Africa/Ceuta and Europe/Berlin have the same UTC offset, and follow the exact same rules for daylight savings. But they haven't always done so.
In 1970-01-01 Africa/Ceuta used a 0:00:00 UTC offset and did not use daylight saving.
In 1970-01-01 Europe/Berlin used a 1:00:00 UTC offset and did not use daylight saving.
It wasn't until 1986-03-30 01:00:00 UTC that Africa/Ceuta and Europe/Berlin adopted the same rules. Your computer knows this history, and accurately reflects it when doing conversions between UTC and local time for dates prior to 1986-03-30.

they change to Summer and Winter-times at different dates, which is why the times might be different even though they have the same UTC offset

Related

Converting date in a different timezone with `date`

I am trying to convert dates from different timezones with UNIX date (I am on Ubuntu 20.04).
With current date, it works well:
$ date
dim. 12 déc. 2021 11:59:16 CET
$ TZ=Pacific/Tahiti date
dim. 12 déc. 2021 00:59:32 -10
But when I am working with a string, it fails:
$ export testdate="2021/10/28 17:47:26"
$ date -d "$test"
jeu. 28 oct. 2021 17:47:26 CEST
$ TZ=Pacific/Tahiti date -d "$test"
jeu. 28 oct. 2021 17:47:26 -10
as I am expecting:
jeu 28 oct. 2021 05:47:26 -10
I don't understand why I don't get the proper shift. And of course if I try with a date and time where the day should also change, it doesn't work either:
$ export test="2021/10/28 7:47:26"
$ date -d "$test"
jeu. 28 oct. 2021 07:47:26 CEST
$ TZ=Pacific/Tahiti date -d "$test" "+%F %T %Z"
2021-10-28 07:47:26 -10
while I am expecting:
mer 27 oct. 2021 19:47:26 -10
why I don't get the proper shift
test="2021/10/28 17:47:26"
Is a date in unknown timezone. No one knows what timezone it is in, what the daylight is. GNU date tries to "guess" what offset you meant, it generally traverses TZ database for current timezone and just picks the first offset that matches. Also, specifying timezone is not enough to know what daylight it is, you have to be specific.
Also, because of the daylight time you can "go back" in time, it's now known what the offset to UTC is even when you know the timezone.
Also, you don't have to export it - date does not care about test environment variable.
Converting date in a different timezone with date
If the input is in UTC, tell date that.
$ LC_ALL=C TZ=Pacific/Tahiti date -d "2021/10/28 17:47:26 UTC"
Thu Oct 28 07:47:26 -10 2021
If the input is with any other offset, tell date that.
$ LC_ALL=C TZ=Pacific/Tahiti date -d "2021/10/28 17:47:26 CEST"
Thu Oct 28 05:47:26 -10 2021
Te parsing of GNU date of input format is generally a mystery. The documentation lists 2004-02-29 16:21:42 format as an example input, so I recommend that format. If you want to be exact, I recommend strptime from dateutils (or a real programming language).
One simple way is to convert time first to epoch time :
test="2021/10/28 17:47:26"
TZ=Pacific/Tahiti date -d #$(date -d "$test" +%s)
date -d "$test" +%s converts local time to epoch time.
TZ=Pacific/Tahiti date -d #$(date -d "$test" +%s) prints Tahiti time from epoch time.
I'm not fully confident, but i get the impression you're looking the following syntax:
LC_TIME="es_ES.UTF8" TZ="America/New_York" date --date='TZ="Europe/Amsterdam" 2021/10/28 17:47:26' "+%A %F %T %B"
That takes a predefined datetime (interpreted as being local to Amsterdam), adjusts the datetime (based on the time difference) to New York-time at that same moment; Then it prints that result with Spanish names for the months/weekdays (provided that language' locale is present on your system).

get full date of a specific day of the current week with GNU date

Is there a way to get the date of Friday of the current week regardless whether it is in the future or past from current day ?
For example, today is 2021-07-10 so Friday of the current week would be one day ago. But if today was 2021-07-05 Friday of the current week would be four days in the future.
I am aware that with date utility you can get either last Friday or the coming Friday with :
date --date="last Friday" and
date --date="next Friday"
is there a way to get the date of Friday of the current week without complicated logic ?
The only way I know how to do what you want is to do something like the following
date -d "next monday - $(($(date +%u) -1)) days"
Of course, there are side issues such as which day is the start of the business week (it varies), etc.
Here is a simple script to print out a week's dates:
#!/bin/bash
for DAY in monday tuesday wednesday thursday friday saturday sunday
do
# date -d "next $DAY - $(($(date +%u) - 1)) days"
date -d "next $DAY - $(date +%u) days
done
It produces the following output based on today's date:
Mon Jul 5 12:00:00 AM PST 2021
Tue Jul 6 12:00:00 AM PST 2021
Wed Jul 7 12:00:00 AM PST 2021
Thu Jul 8 12:00:00 AM PST 2021
Fri Jul 9 12:00:00 AM PST 2021
Sat Jul 10 12:00:00 AM PST 2021
Sun Jul 11 12:00:00 AM PST 2021

How can I customise an ISO image of Webconverger with a Linux shell script?

I want to write a simple shell script that downloads the latest ISO image of Webconverger and changes the boot parameters of grub in /boot/live.cfg.
So this is what I have done so far:
#!/bin/bash
# 1. Download the ISO
wget https://build.webconverger.com/latest.iso
# 2. Mount the ISO and create a development folder
sudo mkdir /mnt/webconverger
sudo mount -o loop latest.iso /mnt/webconverger
mkdir devfolder
rsync -av /mnt/webconverger/ devfolder/
sudo umount /mnt/webconverger
sudo rm -r /mnt/webconverger
# 3. Change the file in the development folder.
chmod -R 777 devfolder/
rm devfolder/boot/live.cfg
echo \
"DEFAULT de
label de
menu label Start
kernel /live/vmlinuz
append initrd=/live/initrd.img boot=live skipconfig quiet splash components=gitfs net.ifnames=0 bootfrom=removable locale=de xkb=-layout%20de homepage=https://www.google.de/" \
> devfolder/boot/live.cfg
# 4. Create a new ISO image
mkisofs -o new.iso -l devfolder
rm -r devfolder/
The generated ISO does not boot.
I asume the reasons for the problem are, that I had to run the whole script with root privileges and that I need a more complex mkisofs command.
Does anybody know how to generate a proper ISO image for Webconverger with a shell script? Or is there an alternative method for the whole process? Something like ISO Master but for the command line?
Update
I have replaced the code snippets with the actual shell script. So it is easier to test. And here is the command line output (without the wget part):
mount: /dev/loop0 is write-protected, mounting read-only
sending incremental file list
./
boot/
boot/boot.bin
boot/boot.cat
boot/live.cfg
boot/syslinux/
boot/syslinux/ldlinux.c32
boot/syslinux/libcom32.c32
boot/syslinux/libutil.c32
boot/syslinux/splash.png
boot/syslinux/syslinux.cfg
boot/syslinux/vesainfo.c32
boot/syslinux/vesamenu.c32
live/
live/initrd.img
live/vmlinuz
live/filesystem.git/
live/filesystem.git/HEAD
live/filesystem.git/config
live/filesystem.git/description
live/filesystem.git/packed-refs
live/filesystem.git/shallow
live/filesystem.git/branches/
live/filesystem.git/hooks/
live/filesystem.git/hooks/applypatch-msg.sample
live/filesystem.git/hooks/commit-msg.sample
live/filesystem.git/hooks/post-update.sample
live/filesystem.git/hooks/pre-applypatch.sample
live/filesystem.git/hooks/pre-commit.sample
live/filesystem.git/hooks/pre-push.sample
live/filesystem.git/hooks/pre-rebase.sample
live/filesystem.git/hooks/pre-receive.sample
live/filesystem.git/hooks/prepare-commit-msg.sample
live/filesystem.git/hooks/update.sample
live/filesystem.git/info/
live/filesystem.git/info/exclude
live/filesystem.git/objects/
live/filesystem.git/objects/info/
live/filesystem.git/objects/pack/
live/filesystem.git/objects/pack/pack-1aa978c13266848f4644754d1875dc90ec4bfcdb.idx
live/filesystem.git/objects/pack/pack-1aa978c13266848f4644754d1875dc90ec4bfcdb.pack
live/filesystem.git/refs/
live/filesystem.git/refs/heads/
live/filesystem.git/refs/heads/master
live/filesystem.git/refs/remotes/
live/filesystem.git/refs/remotes/origin/
live/filesystem.git/refs/remotes/origin/HEAD
live/filesystem.git/refs/tags/
live/filesystem.git/refs/tags/initial-revision
sent 598,528,659 bytes received 726 bytes 239,411,754.00 bytes/sec
total size is 598,380,033 speedup is 1.00
I: -input-charset not specified, using utf-8 (detected in locale settings)
1.71% done, estimate finish Tue Jul 10 20:12:54 2018
3.42% done, estimate finish Tue Jul 10 20:12:54 2018
5.13% done, estimate finish Tue Jul 10 20:13:13 2018
6.84% done, estimate finish Tue Jul 10 20:13:08 2018
8.55% done, estimate finish Tue Jul 10 20:13:05 2018
10.26% done, estimate finish Tue Jul 10 20:13:03 2018
11.97% done, estimate finish Tue Jul 10 20:13:02 2018
13.68% done, estimate finish Tue Jul 10 20:13:01 2018
15.39% done, estimate finish Tue Jul 10 20:13:00 2018
17.10% done, estimate finish Tue Jul 10 20:12:59 2018
18.81% done, estimate finish Tue Jul 10 20:12:59 2018
20.52% done, estimate finish Tue Jul 10 20:12:58 2018
22.23% done, estimate finish Tue Jul 10 20:12:58 2018
23.94% done, estimate finish Tue Jul 10 20:12:58 2018
25.65% done, estimate finish Tue Jul 10 20:12:57 2018
27.36% done, estimate finish Tue Jul 10 20:12:57 2018
29.07% done, estimate finish Tue Jul 10 20:12:57 2018
30.78% done, estimate finish Tue Jul 10 20:12:57 2018
32.49% done, estimate finish Tue Jul 10 20:12:57 2018
34.20% done, estimate finish Tue Jul 10 20:12:56 2018
35.91% done, estimate finish Tue Jul 10 20:12:56 2018
37.62% done, estimate finish Tue Jul 10 20:12:56 2018
39.33% done, estimate finish Tue Jul 10 20:12:56 2018
41.04% done, estimate finish Tue Jul 10 20:12:56 2018
42.75% done, estimate finish Tue Jul 10 20:12:56 2018
44.46% done, estimate finish Tue Jul 10 20:12:56 2018
46.17% done, estimate finish Tue Jul 10 20:12:56 2018
47.88% done, estimate finish Tue Jul 10 20:12:56 2018
49.59% done, estimate finish Tue Jul 10 20:12:56 2018
51.30% done, estimate finish Tue Jul 10 20:12:55 2018
53.02% done, estimate finish Tue Jul 10 20:12:55 2018
54.72% done, estimate finish Tue Jul 10 20:12:55 2018
56.44% done, estimate finish Tue Jul 10 20:12:55 2018
58.14% done, estimate finish Tue Jul 10 20:12:55 2018
59.86% done, estimate finish Tue Jul 10 20:12:55 2018
61.56% done, estimate finish Tue Jul 10 20:12:55 2018
63.28% done, estimate finish Tue Jul 10 20:12:55 2018
64.98% done, estimate finish Tue Jul 10 20:12:55 2018
66.70% done, estimate finish Tue Jul 10 20:12:55 2018
68.40% done, estimate finish Tue Jul 10 20:12:55 2018
70.12% done, estimate finish Tue Jul 10 20:12:55 2018
71.82% done, estimate finish Tue Jul 10 20:12:55 2018
73.54% done, estimate finish Tue Jul 10 20:12:55 2018
75.24% done, estimate finish Tue Jul 10 20:12:55 2018
76.96% done, estimate finish Tue Jul 10 20:12:55 2018
78.66% done, estimate finish Tue Jul 10 20:12:55 2018
80.38% done, estimate finish Tue Jul 10 20:12:55 2018
82.08% done, estimate finish Tue Jul 10 20:12:55 2018
83.80% done, estimate finish Tue Jul 10 20:12:55 2018
85.50% done, estimate finish Tue Jul 10 20:12:55 2018
87.22% done, estimate finish Tue Jul 10 20:12:55 2018
88.92% done, estimate finish Tue Jul 10 20:12:55 2018
90.64% done, estimate finish Tue Jul 10 20:12:55 2018
92.34% done, estimate finish Tue Jul 10 20:12:55 2018
94.06% done, estimate finish Tue Jul 10 20:12:55 2018
95.76% done, estimate finish Tue Jul 10 20:12:55 2018
97.48% done, estimate finish Tue Jul 10 20:12:55 2018
99.18% done, estimate finish Tue Jul 10 20:12:55 2018
Total translation table size: 0
Total rockridge attributes bytes: 0
Total directory bytes: 30720
Path table size(bytes): 222
Max brk space used 2b000
292387 extents written (571 MB)
And this what I get in VirtualBox:
Click here!
So the full answer to the question is:
#!/bin/bash
# 1. Download the ISO
wget https://build.webconverger.com/latest.iso
# 2. Mount the ISO and create a development folder
sudo mkdir /mnt/webconverger
sudo mount -o loop latest.iso /mnt/webconverger
mkdir devfolder
rsync -av /mnt/webconverger/ devfolder/
sudo umount /mnt/webconverger
sudo rm -r /mnt/webconverger
# 3. Change the file in the development folder.
echo \
"DEFAULT de
label de
menu label Start
kernel /live/vmlinuz
append initrd=/live/initrd.img boot=live skipconfig quiet splash components=gitfs net.ifnames=0 bootfrom=removable locale=de xkb=-layout%20de homepage=https://duckduckgo.com nobrand" \
| sudo tee devfolder/boot/live.cfg
# 4. Create a new ISO image
sudo mkisofs -r -J -l -V "Webconverger" -b boot/boot.bin -c boot/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o new.iso devfolder
sudo isohybrid new.iso
sudo rm -r devfolder/
For some reason dd did not work for Webconverger images. Even the with the untouched latest.iso it was not possible to create a bootable USB flash drive. So I used Win32DiskImager.
What you want to do is to generate an El Torito bootable CD with an MBR in its first bytes.
mkisofs(8) needs some special options to create an "El Torito" bootable CD:
mkisofs -r -J -l -V "Webconverger" -b boot/boot.bin -c boot/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o new.iso devfolder
Explaination:
-r -J: Enable Joliet and Rock Ridge meta data.
-l: Allow long filenames.
-V: Set volume ID. Optional.
-b: Specify the "El Torito" boot image.
-c: Specify the "El Torito" boot catalog.
-no-emul-boot: Specify that this is an "El Torito" bootable CD.
-boot-load-size: number of 512 bytes sector to load (4 is what usually works)
-boot-info-table: modify the original boot image appending the Boot Info Table
Then, if you want to be able to just dd the file on a USB drive you will need to write an MBR onto the ISO:
isohybrid new.iso

Bash 'date -d' bug?

I am using Ubuntu 10.04 LTS. In bash I am getting error while converting string to date like this:
date -d '20110327 02:00'
date: invalid date `20110327 02:00'
but these work:
date -d '20110327 03:00'
Sun Mar 27 03:00:00 CEST 2011
date -d '20110326 02:00'
Sat Mar 26 02:00:00 CET 2011
date -d '20110328 02:00'
Mon Mar 28 02:00:00 CEST 2011
Any ideas?
Thanks,
Jan
It's the summer time !
In 2011, we get an additionnal hour on the March 27th at 2:00 it was in fact 3:00.
So 27/03/2011 2:00 is not a valid date :-)
Nope, I'm sure there was a transition to/from Daylight Saving Time (DST) at 2:00 that day, so there wasn't 2:00 :)
Well there is no such local time - there was switch to summer time and the clock went from 1:59 to 3:00.

Real-time display of `date` changes on Linux

On an embedded Linux device that does not present /dev/rtc*, how can I set off a console window writing the value of the Real-Time Clock to the console, on the tick, every time it changes?
Results would be like:
$ **someCmd**
Mon Mar 14 16:43:22 UTC 2011
Mon Mar 14 16:43:23 UTC 2011
Mon Mar 14 16:43:24 UTC 2011
Mon Mar 14 16:43:25 UTC 2011
Mon Mar 14 16:43:26 UTC 2011
etc.
The device is armv5tejl running BusyBox v1.13.3.
Use the watch commad, try this is:
watch -n 1 date
I don't know how much the BusyBox shell supports, but in sh you could do something like this:
{ while true ; do date ; sleep 0.1 ; done } | uniq

Resources