Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Get query result from MySQL for last month:

SELECT * FROM users WHERE registration_date >= DATE_SUB(NOW(), INTERVAL 1 MONTH)

#mysql #query #interval #now #date_sub
It is always a best practice to store dates in UTC. Do not ever store dates in your local timezone in which you will go to hell in your programming experience!

Given that you have saved your dates in UTC and would like to convert them into Asia/Tehran date in Python do like below:

date_format = '%Y-%m-%d %H:%M:%S'
local_tz = pytz.timezone('Asia/Tehran')
submit_time = dateutil.parser.parse(utc_date)
submit_time = submit_time.replace(tzinfo=pytz.utc).astimezone(local_tz)
output = submit_time.strftime(date_format)

#python #date #utc #strftime #dateutil #timezone #pytz
To get time in epoch in linux`/`OS X you can use date with formatting like below:

date +%s


The output of the command is similar to the following output:

1518517636


#linux #osx #date #epoch
It is best practice to always set server date/time to UTC. In debian in order to configure your time and set it to UTC, do as follow:

sudo dpkg-reconfigure tzdata

Now select None of the above or Etc and then press OK. Now select UTC. You're done.

To check your server date/time issue date command:

root@serv01:~# date
Mon Mar 5 11:20:59 UTC 2018


#date #dpkg-reconfigure #tzdata #UTC #timezone
Get specific date like 2 days ago by bash script:

#!/bin/bash
specific_date=`date --date="3 day ago" +%Y%m%d`
echo $specific_date

#linux #date #bash_script #bash
We have talked before about how to get current month using the below line of code:

echo $(date +%m)

It prints out 01, 02, etc.

As per the GNU date manpage:

By default, date pads numeric fields with zeroes. The following
optional flags may follow '%':
- (hyphen) do not pad the field

So you can remove leading zero by hyphen as below:

echo $(date +%-m)

It prints out 1, 2, etc.

#linux #bash #date