• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

time

Set Linux Time Zone

January 27, 2020

Here’s another way of setting Linux time zone.

Check server time.

# check server time
date

# check server time date

Change to local time.

# set to local time
rm /etc/localtime
ln -s /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

# set to local time rm /etc/localtime ln -s /usr/share/zoneinfo/America/Los_Angeles /etc/localtime

Check server time again.

# check server time again
date

# check server time again date

Filed Under: Linux Tagged With: date, localtime, set, time, zone

IAM List Access Keys

January 14, 2020

Here’s how to list all the access keys in AWS via Python.

import logging
import boto3
from botocore.exceptions import ClientError
 
# set aws profile
session = boto3.Session(profile_name="default")
 
# get list of users
 
iam = session.client('iam')
users = iam.list_users()
 
# display results
print("{0:<20} {1:<25} {2:<15} {3:<10}".format('UserName', 'AccessKey', 'Status', 'CreateDate'))
print("----------------------------------------------------------------------------------------")
 
for a in users['Users']:
    user = a['UserName']
    # get keys
    keys = iam.list_access_keys(UserName=user)
    for b in keys['AccessKeyMetadata']:
        username = b['UserName']
        accesskeyid = b['AccessKeyId']
        status = b['Status']
        createdate = str(b['CreateDate'])
        print("{0:<20} {1:<25} {2:<15} {3:<10}".format(username, accesskeyid, status, createdate))

import logging import boto3 from botocore.exceptions import ClientError # set aws profile session = boto3.Session(profile_name="default") # get list of users iam = session.client('iam') users = iam.list_users() # display results print("{0:<20} {1:<25} {2:<15} {3:<10}".format('UserName', 'AccessKey', 'Status', 'CreateDate')) print("----------------------------------------------------------------------------------------") for a in users['Users']: user = a['UserName'] # get keys keys = iam.list_access_keys(UserName=user) for b in keys['AccessKeyMetadata']: username = b['UserName'] accesskeyid = b['AccessKeyId'] status = b['Status'] createdate = str(b['CreateDate']) print("{0:<20} {1:<25} {2:<15} {3:<10}".format(username, accesskeyid, status, createdate))

Filed Under: Cloud, Linux Tagged With: aws, boto3, cli, create, iam, keys, list, python, time, user

Elapse Time on Bash Script

September 24, 2019

Here’s a nice little Bash function in a script to display the elapse time. It’s a nice function for showing how long a process ran.

#!/bin/bash
# pass number of seconds as argument. 
# Example below calculates 1000s.
# elapse.sh 1000
function show_time () {
    num=$1
    min=0
    hour=0
    day=0
    if((num>59));then
        ((sec=num%60))
        ((num=num/60))
        if((num>59));then
            ((min=num%60))
            ((num=num/60))
            if((num>23));then
                ((hour=num%24))
                ((day=num/24))
            else
                ((hour=num))
            fi
        else
            ((min=num))
        fi
    else
        ((sec=num))
    fi
    echo "$day"d "$hour"h "$min"m "$sec"s
}
show_time $1

#!/bin/bash # pass number of seconds as argument. # Example below calculates 1000s. # elapse.sh 1000 function show_time () { num=$1 min=0 hour=0 day=0 if((num>59));then ((sec=num%60)) ((num=num/60)) if((num>59));then ((min=num%60)) ((num=num/60)) if((num>23));then ((hour=num%24)) ((day=num/24)) else ((hour=num)) fi else ((min=num)) fi else ((sec=num)) fi echo "$day"d "$hour"h "$min"m "$sec"s } show_time $1

Filed Under: Linux Tagged With: bash, days, elapse, hours, mins, process, seconds, time

Chrony Service

July 28, 2019

The chrony service actually does not change the time. The misconception is that the chrony service is setting the time given by the NTP server. This is not what’s happening. The chrony service is just telling the system clock to go faster or slower. This is the reason why sometimes even though the time is wrong and the NTP server is working, the time does not get corrected immediately. It takes a little while.

Filed Under: Linux Tagged With: chrony, ntp, service, time

Calculate Epoch Time to Date

June 27, 2019

Here’s how to calculate epoch time using the Linux date command.

# Linux
date --date @1561266781
date -d @1561266781
# MacOS
date -r 1561266781

# Linux date --date @1561266781 date -d @1561266781 # MacOS date -r 1561266781

Output is : Sun Jun 23 05:13:01 UTC 2019

Show current epoch time.

date +%s

date +%s

Output: 1561642643

Filed Under: Cloud Tagged With: calculate, date, epoch, time

Comparing Time

May 9, 2017

Here’s a neat little PHP script that compares a set date/time to the current time. The time() function sets the $now variable to the current Unix timestamp. The strtotime() function converts a date string to a Unix timestamp. The result is assigned to the $setdate variable. The two variables containing Unix timestamps are then compared. A simple if-then statement determines if the date is in the past or future.

date_default_timezone_set('America/Los_Angeles');
$setdate = strtotime("May 9, 2017 12:27PM");
$now = time();
if ( $setdate > $now ) : echo 'Future date'; else : echo 'Past date'; endif;

date_default_timezone_set('America/Los_Angeles'); $setdate = strtotime("May 9, 2017 12:27PM"); $now = time(); if ( $setdate > $now ) : echo 'Future date'; else : echo 'Past date'; endif;

You may have to change the default timezone if your server is in a different timezone.

Filed Under: PHP Tagged With: comparison, date, time, unix timestamp

Run Code At Specific Times

January 20, 2015

If you need to run some code at certain times of the week, a weekly sale for example, PHP gives you that flexibility. In the example below, we will display an image on our website every Wednesday afternoon between 1pm and 5pm to display a sale.

Let’s create a new function called wednesday_afternoon(). We can use this function to check the date and time. The function checks for the current time and compares it if it’s Wednesday afternoon. It returns a true if it is, and a false if it falls outside those times.

function wednesday_afternoon() {
  date_default_timezone_set('America/Los_Angeles');
  $start_time = strtotime('Wednesday 1pm');
  $stop_time = strtotime('Wednesday 5pm');
  $current_time = strtotime('now');
  // return true if between start and stop times
  if ( $current_time > $start_time && $current_time < $stop_time ) {
    return true;
  }
}

function wednesday_afternoon() { date_default_timezone_set('America/Los_Angeles'); $start_time = strtotime('Wednesday 1pm'); $stop_time = strtotime('Wednesday 5pm'); $current_time = strtotime('now'); // return true if between start and stop times if ( $current_time > $start_time && $current_time < $stop_time ) { return true; } }

A simple if statement decides what image to display based if it’s Wednesday afternoon.

if (wednesday_afternoon()) {
  <div class="wednesday_sale">
    <img src="/path/to/our/image.png"/>
  </div>
} else {
  <div class="no_sale">
    <img src="/path/to/our/no/sale/image.png"/>
  </div>
}

if (wednesday_afternoon()) { <div class="wednesday_sale"> <img src="/path/to/our/image.png"/> </div> } else { <div class="no_sale"> <img src="/path/to/our/no/sale/image.png"/> </div> }

I have posted a similar code GitHubGist.

Filed Under: PHP Tagged With: strtotime, time

  • Home
  • About
  • Archives

Copyright © 2023