Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 21-30 of 68 total

Convert MySQL DATETIME to JS Date() object

This little function parses mysql datetime and returns Date() object

  function mysqlTimeStampToDate(timestamp) {
    //function parses mysql datetime string and returns javascript Date object
    //input has to be in this format: 2007-06-05 15:26:02
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1],parts[2],parts[3],parts[4],parts[5]);
  }

Change system date of photos to EXIF date

This script takes a list of files as arguments.
It set the system datetime (both modifitation and access) to the date
the photo was taken.

It use the EXIF module for that, that needs to be in the same folder as the script :
You can find it here :
http://home.cfl.rr.com/genecash/digital_camera/EXIF.py

#!/usr/bin/python

import sys
import EXIF
from datetime import *
import time
import os

# Loop on arguments (files)
for arg in sys.argv[1:] :
    
    # Do nothing of dirs
    if os.path.isdir(arg) :
        continue

    # Open the file
    f=open(arg, 'rb')
 
    # Read exif data
    tags = EXIF.process_file(f)
    
    # Ensure date is present 
    if not tags.has_key("Image DateTime") :
        continue 
        
    # Get date of photo
    date = tags["Image DateTime"]
    date = datetime(*(time.strptime(date.values, "%Y:%m:%d %H:%M:%S")[0:6]))
    timestamp = int(time.mktime(date.timetuple()))
    
    # Some traces
    print "File:%s - Time:%s " % (arg, timestamp)

    # Change the date
    os.utime(arg, (timestamp, timestamp))


Convert Jalali to Gregorian Date & vice versa

#Jalali Date function by Ali Khalili (ali1k AT yahoo DOT com)
#convert Gregorian to Jalali
def gregorianToJalali(year,month,day)
  gDaysInMonth=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  jDaysInMonth=[31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29]
  gy=(year.to_i)-1600
  gm=(month.to_i)-1
  gd=(day.to_i)-1
  jm=0
  jd=0
  jy=0
  gDayNo=365*gy + (gy+3)/4 - (gy+99)/100 +(gy+399)/400
  i=0
  0.upto(gm-1) do |i|
    gDayNo += gDaysInMonth[i]
  end
  if (gm>1 && (gy%4==0 && gy%100!=0) || (gy%400==0))
    #leap and after Feb
    gDayNo=gDayNo+1
  end
  gDayNo +=gd
  jDayNo= gDayNo-79
  #12053=365*33 + 32/4
  jNp=jDayNo/12053
  jDayNo=jDayNo % 12053
  #1461=365*4 +4/4
  jy=979 + 33*jNp + 4*(jDayNo/1461)
  jDayNo %=1461
  if(jDayNo >= 366)
  jy +=(jDayNo-1)/365
  jDayNo =(jDayNo-1)%365
  end
  i=0
  while (i<11 && jDayNo>=jDaysInMonth[i])
    jDayNo -= jDaysInMonth[i]
    i=i+1
  end
   jm=i+1
  jd=jDayNo+1
  puts " #{jy} - #{jm} - #{jd}"
end
#Convert Jalali to Gregorian
def jalaliToGregorian(year,month,day)
  gDaysInMonth=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  jDaysInMonth=[31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29]
  jy=(year.to_i)-979
  jm=(month.to_i)-1
  jd=(day.to_i)-1
  gm=0
  gd=0
  gy=0
  jDayNo=365*jy + (jy/33)*8 + ((jy%33)+3)/4
  0.upto(jm-1)do |i|
  jDayNo +=jDaysInMonth[i]
end
jDayNo +=jd
gDayNo=jDayNo + 79
#146097=365*400 +400/4 - 400/100 +400/400
gy=1600+400*(gDayNo/146097)
gDayNo = gDayNo%146097
leap=true
if(gDayNo >= 36525)
gDayNo =gDayNo-1
#36524 = 365*100 + 100/4 - 100/100
gy +=100* (gDayNo/36524)
gDayNo=gDayNo % 36524
if(gDayNo>=365)
gDayNo =gDayNo+1
else
leap=false
end
end
#1461 = 365*4 + 4/4
gy += 4*(gDayNo/1461)
gDayNo %=1461
if(gDayNo>=366)
leap=false
gDayNo=gDayNo-1
gy +=gDayNo/365
gDayNo=gDayNo %365
end
  i=0
  tmp=0
  while (gDayNo>= (gDaysInMonth[i]+tmp))
    if(i==1 && leap==true)
    tmp=1
    else
    tmp=0
    end
    gDayNo -=gDaysInMonth[i]+tmp
    i=i+1
  end
  gm=i+1
  gd=gDayNo+1
  puts " #{gy} - #{gm} - #{gd}"
end
#Run Program
#gregorianToJalali(*ARGV)
jalaliToGregorian(*ARGV)

Easter date - NASM

Compute Easter date in NASM.

segment .text
  global easter_asm

;typedef struct {
;  unsigned short d;
;  unsigned short m;
;} Date;

;void easter_asm(unsigned short Y, Date *d)
;   computes the Easter date for the year Y and stores it in d
;
;Parameters
;   Y - the year
;   d - the Date struct in which to store the result

%define d [ebp + 12]
%define Y [ebp + 8]
easter_asm:
  enter 0, 0
  sub esp, 28
  %define G [ebp - 4]
  %define C [ebp - 8]
  %define X [ebp - 12]
  %define Z [ebp - 16]
  %define D [ebp - 20]
  %define E [ebp - 24]
  %define N [ebp - 28]

  ; int G = (Y % 19) + 1;
  xor edx, edx
  mov eax, Y
  mov ebx, 19
  div ebx
  inc edx
  mov G, edx

  ; int C = (int)(Y / 100) + 1;
  xor edx, edx
  mov eax, Y
  mov ebx, 100
  div ebx
  inc eax
  mov C, eax

  ; int X = 3 * C / 4 - 12;
  xor edx, edx
  mov eax, C
  mov ebx, 4
  div ebx
  mov ebx, 3
  mul ebx
  sub eax, 12
  mov X, eax

  ; int Z = (8 * C + 5) / 25 - 5;
  xor edx, edx
  mov eax, C
  mov ebx, 8
  mul ebx
  add eax, 5
  mov ebx, 25
  div ebx
  sub eax, 5
  mov Z, eax

  ; int D = 5 * Y / 4 - X - 10;
  xor edx, edx
  mov eax, Y
  mov ebx, 5
  mul ebx,
  mov ebx, 4
  div ebx
  sub eax, X
  sub eax, 10
  mov D, eax

  ; int E = (11 * G + 20 + Z - X) % 30;
  mov eax, G
  mov ebx, 11
  mul ebx
  add eax, 20
  add eax, Z
  sub eax, X
  xor edx, edx
  mov ebx, 30
  div ebx
  mov E, edx

  cmp dword E, 24
  jne after_e1
  inc dword E
  jmp after_e2
after_e1:
  cmp dword E, 25
  jne after_e2
  cmp dword G, 11
  jle after_e2
  inc dword E
after_e2:

  ; int N = 44 - E;
  mov eax, 44
  sub eax, E
  mov N, eax

  cmp dword N, 21
  jge after_n
  add dword N, 30
after_n:
  ; N = N + 7 - ((D + N) % 7);
  mov eax, N
  add eax, D
  xor edx, edx
  mov ebx, 7
  div ebx
  add dword N, 7
  sub N, edx

  mov eax, d

  ; N is the nuber of day from March 1 to easter. Check if easter is in April.
  cmp dword N, 31
  jle in_march
  mov bl, N
  sub bl, 31
  mov [eax], bl
  mov word [eax + 2], 4
  jmp quit
in_march:
  mov bl, N
  mov [eax], bl
  mov word [eax + 2], 3
quit:
  leave
  ret

Easter date - C

Easter_c computes the easter date for year Y. It uses the standard (as used by most western churches) algorithm developed by the Napolitan astronomer Aloysius Lilius and the Jesuit mathematician Christopher Clavius. It works for any year after 1582.

typedef struct {
  unsigned char d;
  unsigned char m;
} Date;

void easter_c(unsigned short Y, Date *d) {
  int G = (Y % 19) + 1;
  int C = (int)(Y / 100) + 1;
  int X = 3 * C / 4 - 12;
  int Z = (8 * C + 5) / 25 - 5;
  int D = 5 * Y / 4 - X - 10;
  int E = (11 * G + 20 + Z - X) % 30;
  if (((E == 25) && (G > 11)) || (E == 24))
    ++E;
  int N = 44 - E;
  if (N < 21)
    N += 30;
  N = N + 7 - ((D + N) % 7);

  if (N > 31) {
    d->d = N - 31;
    d->m = 4;
  } else {
    d->d = N;
    d->m = 3;
  }
}

Try'n'Go: The Last Date

Extends Date class with a method that return the last Date of a month.
Date#last_of_month takes either a Fixnum or a Time as argument.

require 'date'

class Date
  def self.last_of_month( arg = Time.now )
    year = ( arg.is_a? Fixnum ) ? Time.now.year : arg.year
    mon  = ( arg.is_a? Fixnum ) ? arg : ( arg.mon rescue Time.now.mon )
    
    raise ArgumentError unless mon.between?( 1, 12 )

    begin; Date.new year, mon, mday ||= 31
    rescue ArgumentError; mday -= 1; retry
    end
  end
end

Get the Unix Epoch time in one line of C#

int epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;

Install DateBocks

Install engines plugin

./script/plugin source http://svn.rails-engines.org/plugins/
./script/plugin install engines


Install datebocks

./script/plugin source http://svn.toolbocks.com/plugins
./script/plugin install datebocks_engine

Formatting a date with or without the time

Ruby on Rails

This helper returns the date (and possibly the time) formatted the way you want

def format_date(date, use_time = false)
    if use_time == true
        ampm = date.strftime("%p").downcase
        new_date = date.strftime("%B %d, %Y at %I:%M" + ampm)
    else
        new_date = date.strftime("%B %d, %Y")
    end
end

days-up-to-month - returns the number of days in a year, preceding the given month.

days-in-months: [31 28 31 30 31 30 31 31 30 31 30 31]
days-in-months-leap: head change at copy days-in-months 2 29

days-up-to-month: func [month /in year] [
    sum copy/part either leap-year?/with any [year now] [days-in-months-leap] [days-in-months] month - 1
]
« Newer Snippets
Older Snippets »
Showing 21-30 of 68 total